Question

I am trying to check the location of a point(px, py) on 2D graph in relation to a line segment (lx1, ly1) (lx2, ly2), using logic of North South East West directions. The logic I have implemented is to draw a perpendicular on the line segment from the point.

if perpendicular is on line that means its south.

If point on right side means its East.

If point on left side means West.

If perpendicular is away from line in forward direction will mean North.

If perpendicular is away from line in backward direction will mean South.

My problem is that this logic looks good on paper, but its getting really hard to decide whether its a NW, NE, SW or SE case. Can anyone suggest me how to compute this logic?? I am using C++, but algorithm in any language will be a great help.

I am using end point of line segment to calculate North South East West relation.

Cheers

Was it helpful?

Solution

  • delta_x = x2 - x1
  • delta_y = y2 - y1
  • distance = sqrt (delta_x^2 + delta_y^2)
  • tan (theta) = delta_y / delta_x
  • theta = arctan (delta_y / delta_x) ;; but don't divide by zero!
  • multiply theta by 180/PI to get degrees

The degrees are counter-clockwise from the positive side of the x-axis. Eventually you'll need to do a small amount of algebra to reorient the degrees so that 0 is up (instead of to the right) and run clockwise. But before that:

A problem is that arctan (1 / -1) is the same as arctan (-1 / 1). I.e., you'll get -PI/4 radians or -45 degrees, for both upper left (needs 180 degree offset) and lower right (ok as is). You'll have to do tests on the sign of delta_y vs. delta_x to see if the result of arctan needs to be adjusted.

Before you code your solution, be sure to code tests as well to make sure the functions you are calling produce expected values.

OTHER TIPS

I sympathise with ndim. Calculating direction from one point to another is easy. I don't understand which context would require you to want direction from a line. Is this a mapping application? Is there a road, and about halfway along a road segment you've got some point off to the side?

The semantics of what "north" or "NE" or "east" of a line segment actually means is unclear.

Directions like "north" or "east" or "NE" are usually used to describe the location of one point relative to another (base) point. What is the point on the line segment you are using as that base point?

EDIT: Now that you say you want to use the end point (x2,y2) as the center point for the compass, a point (x,y) will be located relative to the compass by examining the delta vector (x-x2,y-y2).

The easy to reason about method uses atan2() on the delta vector, and considers the angle returned by atan2().

However, it might also be a good idea to just compare the arguments to atan2() to each other and determine the resulting angle ranges from that. This basically avoids calling atan2() at runtime at the cost of you needing to do some calculations at (or before) compile time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top