Code Monkey home page Code Monkey logo

trigonometry's Introduction

A C++ library for strongly typed trigonometric values and utilities

This library provides data types and utilities for trigonometric calculations.
The strong types for radians, degrees and gradians prevents programming errors when doing trigonometric calculations. For example errors such as forgetting to convert a variable to degrees or passing a degrees value to the std::sin function.
The trigonometric types have overloaded mathematical and logic operators to simplify calculations. This library also includes wrappers for the standard C++ trigonometric functions to support types that are defined with this library.

Features

  • Strongly typed trigonometric types for radians, degrees and gradians.
  • Explicit converstions between types.
  • Trigonomentric fuctions using the trigonometric types.
  • User defined trigonometric types.

Requirements

A compiler with support for C++14 or newer.

Examples

Trigonometric types and literals

pg::math::rad  radians( 2.0 );
pg::math::deg  degrees( 360.0 );
pg::math::grad gradians( 400.0 );

using namespace pg::math::trigonometric_litherals;

radians  = 1_rad;
degrees  = 180_deg;
gradians = 200_grad;

Arithmetic

using namespace pg::math::trigonometric_litherals;

auto deg1 = 45.0_deg;
deg1 *= 10;

auto deg2 = deg1 - 90.0_deg;
auto deg3 = deg2 / 2;

assert( deg3.angle() == 180.0 );

Comparison

pg::math::rad rad1( std::numbers::pi );
pg::math::rad rad2( 0 );
pg::math::rad rad3( 2.0 * std::numbers::pi );

assert( rad1 > rad2 );
assert( rad1 < rad3 );
assert( rad1 != rad2 );

Trigonometric functions

auto radians  = pg::math::asin( 0.5 );
auto degrees  = pg::math::asin< pg::math::deg >( 0.5 );
auto gradians = pg::math::asin< pg::math::grad >( 0.5 );

std::cout << "radians  = " << radians.angle() << '\n'
          << "degrees  = " << degrees.angle() << '\n'
          << "gradians = " << gradians.angle() << '\n';

auto sin_radians  = pg::math::sin( radians );
auto sin_degrees  = pg::math::sin( degrees );
auto sin_gradians = pg::math::sin( gradians );

std::cout << "sin( radians )  = " << sin_radians << '\n'
          << "sin( degrees )  = " << sin_degrees << '\n'
          << "sin( gradians ) = " << sin_gradians << '\n';

The output is:

radians = 0.523599
degrees = 30
gradians = 33.3333
sin( radians ) = 0.5
sin( degrees ) = 0.5
sin( gradians ) = 0.5

Converstion between radians, degrees and gradians

auto radians  = pg::math::asin( 1 );
auto degrees  = pg::math::angle_cast< pg::math::deg >( radians );
auto gradians = pg::math::angle_cast< pg::math::grad >( degrees );

std::cout << "radians  = " << radians.angle() << '\n'
          << "degrees  = " << degrees.angle() << '\n'
          << "gradians = " << gradians.angle() << '\n';

The output is:

radians = 1.5708
degrees = 90
gradians = 100

Custom trigonometric type

struct conversion
{
    constexpr static int16_t semicircle = 128;
}

using my_ideg = pg::math::basic_angle< int16_t, conversion >;

my_ideg ideg1 = pg::math::acos< my_ideg >( 0 );
my_ideg ideg2 = pg::math::acos< my_ideg >( 1 );
my_ideg ideg3 = pg::math::acos< my_ideg >( -1 );

std::cout << "acos( 0 )  = " << ideg1.angle() << '\n'
          << "acos( 1 )  = " << ideg2.angle() << '\n'
          << "acos( -1 ) = " << ideg3.angle() << '\n';

auto ideg_cos_x = pg::math::cos( my_ideg( 42 ) );
auto ideg_cos_y = pg::math::cos( my_ideg( -128 ) );
auto ideg_cos_z = pg::math::cos( my_ideg( 0 ) );

std::cout << "ideg_cos_x = " << ideg_cos_x << '\n'
          << "ideg_cos_y = " << ideg_cos_y << '\n'
          << "ideg_cos_z = " << ideg_cos_z << '\n';

The output is:

acos( 0 ) = 64
acos( 1 ) = 0
acos( -1 ) = 128
ideg_cos_x = 0.514103
ideg_cos_y = -1
ideg_cos_z = 1

trigonometry's People

Contributors

pg1003 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.