Last active
October 30, 2024 05:24
-
-
Save darzo27/d25161f13686633bd52162044b30b7db to your computer and use it in GitHub Desktop.
Calculate Angle of Line between two points - Java, C#, Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==== J A V A ================================== | |
/** | |
* Determines the angle of a straight line drawn between point one and two. The number returned, which is a double in degrees, tells us how much we have to rotate a horizontal line clockwise for it to match the line between the two points. | |
* If you prefer to deal with angles using radians instead of degrees, just change the last line to: "return Math.atan2(yDiff, xDiff);" | |
*/ | |
public static double GetAngleOfLineBetweenTwoPoints(Point.Double p1, Point.Double p2) | |
{ | |
double xDiff = p2.x - p1.x; | |
double yDiff = p2.y - p1.y; | |
return Math.toDegrees(Math.atan2(yDiff, xDiff)); | |
} | |
==== C # ================================== | |
/** | |
* Determines the angle of a straight line drawn between point one and two. The number returned, which is a float in degrees, tells us how much we have to rotate a horizontal line clockwise for it to match the line between the two points. | |
* If you prefer to deal with angles using radians instead of degrees, just change the last line to: "return Math.Atan2(yDiff, xDiff);" | |
*/ | |
public static float GetAngleOfLineBetweenTwoPoints(PointF p1, PointF p2) | |
{ | |
float xDiff = p2.X - p1.X; | |
float yDiff = p2.Y - p1.Y; | |
return Math.Atan2(yDiff, xDiff) * (180 / Math.PI); | |
} | |
==== P Y T H O N ================================== | |
/** | |
* Determines the angle of a straight line drawn between point one and two. The number returned, which is a double in degrees, tells us how much we have to rotate a horizontal line clockwise for it to match the line between the two points. | |
* If you prefer to deal with angles using radians instead of degrees, just change the last line to: "return atan2(yDiff, xDiff)" | |
*/ | |
from math import atan2,degrees | |
def GetAngleOfLineBetweenTwoPoints(p1, p2): | |
xDiff = p2.x - p1.x | |
yDiff = p2.y - p1.y | |
return degrees(atan2(yDiff, xDiff)) | |
REFERENCE: WikiCode | |
Available: http://wikicode.wikidot.com/get-angle-of-line-between-two-points | |
----------------------------------------------------------------------------- | |
Calc angle between two points, converging at a center(intermediary) point | |
public static double angleBetweenTwoPointsWithFixedPoint(double point1X, double point1Y, | |
double point2X, double point2Y, | |
double fixedX, double fixedY) { | |
double angle1 = Math.atan2(point1Y - fixedY, point1X - fixedX); | |
double angle2 = Math.atan2(point2Y - fixedY, point2X - fixedX); | |
return angle1 - angle2; | |
} | |
And call it with three points (using Math.toDregrees to transform resulting angle from radians to degrees): | |
System.out.println(Math.toDegrees( | |
angleBetweenTwoPointsWithFixedPoint(0, 0, // point 1's x and y | |
1, 1, // point 2 | |
1, 0 // fixed point | |
))); | |
Output: 90.0 | |
REFERENCE: StackOverflow | |
Available: http://stackoverflow.com/questions/26076656/calculating-angle-between-two-points-java |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment