Skip to main content

[JAVA2D] Angle between two lines

2 replies [Last post]
Anonymous

Give three points on a plane, P0, P1 and P2, form a line from P0 to P1
and a line from P0 to P2.

Is there a Java API to find the angle between these lines?

Thank you,

Ted Hill

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Aaron Mulder

Yeah -- java.lang.Math.atan

Aaron

int l1x = x2 - x1;
int l1y = y2 - y1;
int l2x = x3 - x1;
int l2y = y3 - y1;
float tan1 = (float)l1y/(float)l1x;
float tan2 = (float)l2y/(float)l2x;
double ang1 = Math.atan(tan1);
double ang2 = Math.atan(tan2);
return Math.abs(Math.toDegrees(ang2-ang1));

On Wed, 2 Jun 2004, Ted Hill wrote:
> Give three points on a plane, P0, P1 and P2, form a line from P0 to P1
> and a line from P0 to P2.
>
>
>
> Is there a Java API to find the angle between these lines?
>
>
>
> Thank you,
>
>
>
> Ted Hill
>
>
>
>
>
>
>
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Jim Graham

> int l1x = x2 - x1;
> int l1y = y2 - y1;
> int l2x = x3 - x1;
> int l2y = y3 - y1;
> float tan1 = (float)l1y/(float)l1x;
> float tan2 = (float)l2y/(float)l2x;
> double ang1 = Math.atan(tan1);
> double ang2 = Math.atan(tan2);

This will fail because Math.atan(tangent) does not know which
quadrant the line is in and returns angles that only span
180 degrees (-pi/2 to +pi/2 radians). Thus your answer might
be off by 180 degrees depending on how the individual angles
were rounded.

A more accurate answer would be given by using the alternate
methods Math.atan2(l1y, l1x) and Math.atan2(l2y, l2x) which
avoid the division and can return an answer accurate throughout
360 degrees (output range is -pi to +pi radians).

...jim

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".