# Triangle Formula for alternative Points

With this formula I tried to calculate the third point in a triangle:

https://math.stackexchange.com/a/1553606/126977

I know the length between the two point points and the angles at this corners:

Known points are:

 point1 = { x: 4925.707458496093, y: 281.38922119140636}
point2 = { x: 5065.31622314453,  y: 423.47991943359386}


The known angles are:

angle1 = 137.76476741527148
angle2 = 40.725761857792335


I wrote a javascript programm to test it:

var oo = ( Math.atan2(point2.y - point1.y, point2.x - point1.x));



Then I calculate the points with:

var x = (point1.x + d  * Math.cos(oo));
var y = (point1.y + d  * Math.sin(oo));


But somehow I don't get 0,0 as output I get: 5250.0599801529925, 5204.454252468392

Even after changing some variables, I cant figure out the correct result What do I wrong? Thanks

Here you can run the code:http://jsfiddle.net/hacjk7yr/

• Your jsfiddle has the values of angle1 and angle2 swapped from what you show here, but not their uses in the source code you're showing. – Henning Makholm Dec 2 '15 at 21:51

Although I do not know JavaScript (but do know multiple other languages), I see two errors in your code.

1. In lines 12 and 13 in your linked code, you swapped Angle1 and Angle2. Strangely, you have it correct in your question above. You should have

angle1 = 137.76476741527148

angle2 = 40.725761857792335

angle1 must be the given angle at point p1, and angle2 for p2, where p1 is the "base point" and p2 is the other given point.

1. In line 19 in your code, where you calculate d, the length of the side opposite point p2, the angle in the numerator should be angle2, but you have angle1.

There are other points to be careful of. Make sure the atan2 function in JavaScript takes the $y$-coordinate before the $x$-coordinate. I believe it does, but some languages have $x$ before $y$.

With those changes, and me emulating the code both in Geogebra and in my graphing calculator, the correct answer appears. Make those changes and see if the problem is resolved for you.

I should probably point out that there are two triangles that satisfy your conditions. The algorithm I gave you and the code you wrote find the one where the three vertices, in the order First-Second-Unknown are in counter-clockwise order as seen from above the Cartesian plane. To get the other triangle, in clockwise order, you can either (a) call the same code again, switching the two points, or (b) in the line where you add the angle at the base point (angle1 in your code) instead subtract that angle.

Also note that your code has no error-checking and has some other weak coding practices. It should still work if the data is good, however.

• Thanks for your help, Math.atan2 takes the y parameter first. As you can see I corrected the code jsfiddle.net/hacjk7yr/3 But it stills returns the wrong result? Could you maybe have a look? Or send me the geogebra code? (By the way when you click update in jsfiddle you can save the code and paste me the url) Thanks again from germany! – John Smith Dec 2 '15 at 22:28
• now I calculated another result. jsfiddle.net/hacjk7yr Its near to the 0,0 point but not 100% how can this be? Thanks – John Smith Dec 2 '15 at 23:00
• @JohnSmith: The link in your last comment just before this one has your original code for which I pointed out two error. The link in the comment two before this one corrected the error #1 but did not correct error #2. Change the first occurrence of angle1 to angle 2, but leave the second occurrence alone. I just made those changes: the URL of the updated code is now jsfiddle.net/hacjk7yr/4. Try that code. – Rory Daulton Dec 3 '15 at 1:26