coddding coddding - 9 days ago 5
C++ Question

c++ calculating 2 points based off 2 other points

Ok, so I'm making this fun little game in c++, unfortunately I'm stuck on making a "radar", I'm stuck on this part

Example

(EDIT, i already have

A
&
B
, the only thing i do not have is
C
&
D
)

So what i need to do is calculate the two points (
2d vector
) of
C
&
D
, they need to be in the placement like in the lil pic i made shown above (sorry, i know it's bad).

B
will rotate around
A
so i need to calculate the position of
C
&
D
based on the rotation of
B
. I am going to create a triangle (e.g. draw a line from
C
to
D
,
D
to
B
&
B
to
A
)

A
could be thought of as the center of the bottom line of the triangle, this is like the base of everything, everything rotates around
A
, and the position of
C
&
D
need to be calculated according to how much
B
has rotated around
A
.

What would be like the best way to make a lil function for this calculation?
e.g.

inline float returnDPoint(float A, float B)
{
float dPoint;
//calculate point of D based off A & B
return dPoint;
}


inline float returnCPoint(float A, float B)
{
float cPoint;
//calculate point of C based off A & B
return cPoint;
}


Hopefully i have worded my question well enough, thanks for the read!

Answer

Since you are looking for rotations by 90°, you don't need to use expensive functions such as atan2, sqrt, etc. A simple alternative would be:

diffx = bx - ax;
diffy = by - ay;

cx = ax - diffy = ax - by + ay;
cy = ay + diffx = ay + bx - ax;

dx = ax + diffy = ax + by - ay;
dy = ay - diffx = ay - bx + ax;
Comments