corners of a triangle form degrees

the code looks like this
const double PI = 3.1415926535897;

struct mStruct
{
double A_towerx;
double A_towery;
double B_towerx;
double B_towery;
double C_towerx;
double C_towery;
};

void modify(mStruct *v,double build_radius)
{
double x_theta = 210 * PI/180,y_theta = 330 * PI/180,z_theta = 90 * PI/180;

v->A_towerx = build_radius * cos(x_theta);
v->A_towery = build_radius * sin(x_theta);
v->B_towerx = build_radius * cos(y_theta);
v->B_towery = build_radius * sin(y_theta);
v->C_towerx = build_radius * cos(z_theta);
v->C_towery = build_radius * sin(z_theta);

the out put is

A corner x= -86.6025
A corner y= -50
B corner x= 86.6025
B corner y= -50
C corner x= 4.66906e-012
C corner y= 100

the build radius I used was 100

every thing is correct except the C corner x, that should = 0

anyone know where I am going wrong?.

thanks gary

every thing is correct except the C corner x, that should = 0


It is zero, just that the representation of double is not exact. If you print the value with std::fixed, and a std::precision less than 12 it will print appropriately.

Some other improvements:

constexpr is stronger than const ; PI / 180.0 is also a constant value:

1
2
constexpr double PI = 3.1415926535897; 
constexpr DTR = PI /180.0; // DTR is Degrees To Radians 


Avoid declaring more than 1 variable per line of code, it will save you problems later.

Instead of 6 variables in your struct, consider having a struct Point, then have a std::vector<Point>.

Pass this std::vector to the function by reference, thus avoiding a raw pointer.


In C++, a struct is almost the same as a class - the only difference being the default access is public with struct, and private with class. So alternatively, your struct could be a class with a constructor and an interface of functions.

Edit: This will be better than having your data public.

Good Luck !!

Edit: I have to go out for awhile, I may not be able to reply to any questions.
Last edited on
In PI, the 14th digit to the right of the decimal point is 9, so if you're going to use 13 digits, then you should round the last one up from 7 to 8.
out put now with added

std::cout.precision(4);
cout <<"A corner x= "<<std::fixed<<towers.A_towerx<<endl;

A corner x= -86.6025
A corner y= -50.0000
B corner x= 86.6025
B corner y= -50.0000
C corner x= 0.0000
C corner y= 100.0000

looks correct not sure how to setup std::vector haven't used yet

Thanks for your help at least I am on the right track and have a working function.

gary
Topic archived. No new replies allowed.