 Getting angle using tangent | SoloLearn: Learn to code for FREE!

+2

# Getting angle using tangent

here's the code (https://code.sololearn.com/WY6NL88keDO6/?ref=app) . The red circle is at a known angle of 130°, then I want to draw the navy line from the center to 130° using x and y of the red circle but it looks like I missed the calculation. Currently, the angle of the Navy line is a reflection to the angle of the red line and if I add minus sign ➖ to **diffX* * at line13, it'll work as expected but Why do I need to do that by myself, why can't the Calculations at line 10 and 13 figured out if x shouod be minus ➖ or plus I couldn't figure out where I was wrong

+3

let diffX = (W/2) - x; let diffY = (H/2) - y; let dist = Math.hypot(diffX, diffY); // pythagoras let unknownAngle = Math.atan2(y - H/2, x - W/2); let newX = (W/2) + Math.cos(unknownAngle) * dist; let newY = (H/2) + Math.sin(unknownAngle) * dist; something like this probably? (im bad at math..so,if this wrog,just ignore it😅)

+3

this works for me. let diffX = x - (W/2); let diffY = (H/2) - y; let dist = Math.hypot(diffX, diffY); // pythagoras let unknownAngle = -Math.atan2(diffY, diffX); in cartesian coordinates (-, +) | (+, +) ———————— (-, -) | (+, -) Explanation: (subtraction with W/2 thing is abit tricky here) your diffX is giving positive value where it should be negative and vice versa and as the result we are getting symmetric in them.

+2

do you mean you want to center the end of the navy line to the circle's center?

+2

Lily Mea your solution would behave like the angle is a reflection to the angle. However, if I use your solution and then add "-" to diffX at line13, it'll work.. Now, ik why that works but I'm still confused why do I need to add "-" by myself and the calculations on line10 won't

+2

but why do I need to add it manually, I'm expecting line10 to output a plus or minus result edit: RKK , now I know why it works, calculating the difference should be (final - initial) not (initial - final), if the angle is below the center of the graph then second angle *y* should be y - (H/2)