Difference between revisions of "CPP/Math/Trig"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Trig.cpp== <source lang="cpp"> #include <cmath> #include <iostream> int main() { double x1 = 0; double y1 = 0; double x2 = 100; double y2 = 100; std::cout << 180.…') |
Daviddoria (Talk | contribs) |
||
| Line 3: | Line 3: | ||
#include <cmath> | #include <cmath> | ||
#include <iostream> | #include <iostream> | ||
| + | |||
| + | void TestAtan2(); | ||
int main() | int main() | ||
{ | { | ||
| + | TestAtan2(); | ||
| + | |||
double x1 = 0; | double x1 = 0; | ||
double y1 = 0; | double y1 = 0; | ||
| Line 13: | Line 17: | ||
std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl; | std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl; | ||
return 0; | return 0; | ||
| + | } | ||
| + | |||
| + | void TestAtan2() | ||
| + | { | ||
| + | // syntax is atan2(y,x) | ||
| + | |||
| + | std::cout << "right: " << atan2(0,1) << std::endl; | ||
| + | std::cout << "up right: " << atan2(1,1) << std::endl; | ||
| + | std::cout << "up: " << atan2(1,0) << std::endl; | ||
| + | std::cout << "up left: " << atan2(1,-1) << std::endl; | ||
| + | std::cout << "left: " << atan2(0,-1) << std::endl; | ||
| + | std::cout << "down left: " << atan2(-1,-1) << std::endl; | ||
| + | std::cout << "down: " << atan2(-1,0) << std::endl; | ||
| + | std::cout << "down right: " << atan2(-1,1) << std::endl; | ||
| + | |||
| + | /* Output: | ||
| + | right: 0 | ||
| + | up right: 0.785398 | ||
| + | up: 1.5708 | ||
| + | up left: 2.35619 | ||
| + | left: 3.14159 | ||
| + | down left: -2.35619 | ||
| + | down: -1.5708 | ||
| + | down right: -0.785398 | ||
| + | */ | ||
| + | // -pi is left (-x direction), then moving counter clockwise, it sweeps a full circle back to pi | ||
} | } | ||
</source> | </source> | ||
Latest revision as of 20:01, 5 June 2012
Trig.cpp
#include <cmath> #include <iostream> void TestAtan2(); int main() { TestAtan2(); double x1 = 0; double y1 = 0; double x2 = 100; double y2 = 100; std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl; return 0; } void TestAtan2() { // syntax is atan2(y,x) std::cout << "right: " << atan2(0,1) << std::endl; std::cout << "up right: " << atan2(1,1) << std::endl; std::cout << "up: " << atan2(1,0) << std::endl; std::cout << "up left: " << atan2(1,-1) << std::endl; std::cout << "left: " << atan2(0,-1) << std::endl; std::cout << "down left: " << atan2(-1,-1) << std::endl; std::cout << "down: " << atan2(-1,0) << std::endl; std::cout << "down right: " << atan2(-1,1) << std::endl; /* Output: right: 0 up right: 0.785398 up: 1.5708 up left: 2.35619 left: 3.14159 down left: -2.35619 down: -1.5708 down right: -0.785398 */ // -pi is left (-x direction), then moving counter clockwise, it sweeps a full circle back to pi }