English 中文(简体)
2D Mathematics Geographical Directions
原标题:
  • 时间:2009-11-29 00:56:02
  •  标签:
  • math
  • 2d

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

最佳回答
  • 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.

问题回答

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.





相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

I am having a problem handling large numbers. I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签