i have got three sets of x an y coordinates , where one of the x y coordinates keeps on changing all the time.
lets say i have first set where the coordinates are int x=100 and int y=100;
second set would be : int x=100; int y=150; but the last set of coordinates would always change.

so if we would draw a line from the first set to the second set and from second set to the changing third set and back again to the first set of coordinates i would get a triangle.

what i want is to get the angel of the triangle which would show the degrees from the first set of points to the changing points.

i know i have to use one of the following: sin, cos or tan, but my trigonometry skills are very rusty.

i hope someone could help me or suggest me anything that would lead me to the right direction of solving this issue.

any help is greatly appreciated!!


Recommended Answers

All 11 Replies

well i am going to know the line length for all the lines.
the shape would look something like this (shape b)

left side of the triangle will keep on changing (slightly) due to the change of the coordinates. how could i calculate the angel of the top side of the triangle(like show in the image above).

Thank you

Since they're not right triangles, using the dot product of the vectors may be easier.
Given the vectors (x1,y1) and (x2,y2):
angle in radians = arccos((x1*x2 + y1*y2) / ( sqrt(x1*x1 + y1*y1) * sqrt(x2*x2 + y2*y2) )

More wiki info here: http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation

Pretty decent vector algebra reference here:

Edit: You may need to twiddle the signs a bit. I'm not a vector math genius and always have to "play" with the numbers a little.

thank you!!! this formula looks like it would work,but my amateur programming skills are stopping me on implementing "arccos".
i have tried Math.arccos(......); but it gives me error, that the symbol can not be found
what should i do to get rid of this error??

thanks again for your reply

The method is Math.acos() Remember that the angle is in radians. You can use Math.toDegrees(double) to convert to degrees if you need to.

sorry for the previous post,ignore it please i have worked it out and it is simply Math.acos();
but the calculation gives me wrong values i convert everything back to degrees

degrees = (radians * 180 / Math.PI)

but for some reason the output i receive varies from 0- 15 instead of 0-360 degrees. i am not sure what would be wrong in the calculation...
maybe im doing something wrong.

thank you for your help!!

It may depend on how you represented your input vectors. For that upper point on figure b that you refered to, you would need to calculate the vectors from that point of origin such that they were pointing out and down from that position using (x2-x1,y2-y1).

Here's a small spreadsheet of the results I got with that formula. I calc'd the angle between each vector pair on an irregular triangle I just made up. The total of all calculated inner angles was 180 and each one seemed to be right by visual estimation (the lower row is degrees, I forgot a label)

You could take a look at Law of Cosines as well, if the vector method isn't working out for you. It's just another way to skin the same cat.

Thanks a lot for all the help it really did help me !!
i managed to solve my problem from the example in the wiki.
i knew all the length of all the sizes so all i had to do is work out the angel.

hypotenuse  = Math.sqrt(Math.pow(xb-xa,2) + Math.pow(yb-ya,2));
       adjacent    = Math.sqrt(Math.pow(xa-xa,2) + Math.pow(ya+50-ya,2));
       oposite     = Math.sqrt(Math.pow(xb-xa,2) + Math.pow(yb-(ya+50),2));
       radians = Math.acos((Math.pow(adjacent,2) + Math.pow(hypotenuse,2)- Math.pow(oposite,2)) /(2*adjacent*hypotenuse));

thank you once again, you have been really helpful !

me to working in the same area...i couldnot understand how to set the angle between three points in java...could u plz help me...

thank u...

@saranyabaskaran: No, do not hijack other posters threads to ask your own questions. Please start a new thread of your own and explain more clearly what you are trying to do when you say "set the angle".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.