A line formed by two points greater than 45 degrees from the horizontal
I'm trying to figure out if a line defined by two points is greater than or equal to 90 degrees compared to horizontal. Here is the code I used
bool moreThan90 = false;
double angle = Math.Atan((double)(EndingLocation.Y - Location.Y) / (double)(EndingLocation.X - Location.X));
if (angle >= Math.PI / 2.0 || angle <= -Math.PI / 2.0)
moreThan90 = true;
Did I do it right or is there a better built-in function in .Net that will find this?
EDIT. Actually, I messed up my question to say 45 horizontal, not 90. However, the answers got me to the point where I can figure it out (in fact, I just had to point to Atan2).
a source to share
A line more than 90 degrees from the horizontal will have an EndLocation.x value with an x lower value than Location.x.
So you don't need all the nonsense of ananta, this should be enough:
if (EndingLocation.X < Location.X)
moreThan90 = true;
EDIT:
It seems that the OP stands for 45 degrees and not 90, which means the above simplification no longer works. It would be better to use atan2 for this (as Slaks pointed out). But in the spirit of not using tan:
if (Math.Abs(EndingLocation.X - Location.X) > Math.Abs(EndingLocation.Y - Location.Y) &&
EndingLocation.X < Location.X)
moreThan45 = true;
Note that you only need the second check if you only want the lines pointing to the right
a source to share
You should call Math.Atan2
for example:
double angle = Math.Atan2(EndingLocation.Y - Location.Y,
EndingLocation.X - Location.X);
if (Math.Abs(angle) >= Math.PI / 2.0)
moreThan90 = true;
a source to share
I would not have thought there was a library method for finding the angle between two vectors, you are doing it right (the math is correct) and a quick look around msdn and google gave me nothing. I would use the SLaks version to call the method Math.Atan
.
It is interesting to note that you are using "horizontal", like your plane, to determine if the angle is greater than 90 degrees. If endLocation.x <Location.X your angle will always be "greater" than 90 degrees if you are measuring the positive X-axis.
Edit: The original question has been changed to 45 degrees.
In the next section, we'll discuss how to do this without doing the floating point division by the OP's comment.
To find out if you have a 45 degree angle, we know a few things without actually naming ATan
on the dots.
first, the slope of the 45 degree angle is 1. Therefore, if
Math.Abs((EndLocation.y - location.y)/(EndLocation.X - Location.X)) > 1
You have an angle> 45 degrees, however, because the 45 degrees angle permutes 4 times in a circle. We need to check a few things.
If EndLocation.X < Location.X
, then the angle is greater than 45 degrees. This means all angles remaining from the Y-axis (90 - 270). To determine if the angle is greater than 45 degrees, we only need to know if the absolute value of the slope will be greater than 1. This will always be true for the following.
Math.Abs(EndLocation.Y - Location.Y) > Math.Abs(EndLocation.X - Location.X)
...
So, with an if statement following something like
If (EndLocation.X < Location.X) OrElse (Math.Abs(EndLocation.Y - Location.Y) > Math.Abs(EndLocation.X - Location.X) Then AngleGreaterThan45 = True.
We can determine if an angle is greater than 45 degrees without having to do floating point calculations.
a source to share