English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

for example, I have the following points:
P1(1,1)
P2(1,2)
P3(2,1)
P4(2,2)

I want a formula that can give out the direction:
GetDirection(P1,P2)=North
GetDirection(P1,P3)=East
GetDirection(P1,P4)=North East

2007-12-05 21:03:43 · 2 answers · asked by I need answers 1 in Computers & Internet Programming & Design

Thank you very much for your help!

2007-12-05 21:04:02 · update #1

2 answers

Be P1(x1,y1), P2(x2,y2):
tan alpha = (y2 - y1) / (x2 - x1)
This is the angle formed by the horizontal and the line passing through the two points.
With the tan, you get ARCTAN ( tan (alpha) ) to get the angle with the horizontal. (depends on your programming language).
Warning: if x2 = x1, it is a vertical line and tan (alpha) will be infinite (division by zero).
So, if 0º = EAST:
if (x1 = x2) // vertical
{
if (y2 > y1) : going north (90º)
else going south (270º)
}
else
{
tan a = (y2 - y1) / (x2 - x1)
angle = arctan (a)
}
Adapt this to your programming language.
Add a table to select a word, ie:
if angle < 45 and -45 then "EAST"
if angle ... etc
(I would prefer an EXACT angle)

2007-12-05 22:14:35 · answer #1 · answered by just "JR" 7 · 0 0

my programming is a little rusty from the college days, but it seems you could do this very easily with two IF loops. you will need the correct syntax but this is the general idea:

input= (x1,y1)
input=(x2,y2)

If x1>x2 then xDirection="West"
else if x1 else if x1=x2 then xDirection=""
end if

If y1>y2 then yDirection="south"
elseif y1 elseif y1=y2 then yDirection=""
end if

display="yDirection xDirection"

2007-12-05 21:20:39 · answer #2 · answered by Justin W 1 · 0 0

fedest.com, questions and answers