Tricky.
Most trig maths assumes that 0,0 is at the bottom, and that positive co-eds go up.
This code seems to work, but will need some tweaking: I’ve left it simple to explain the principle.
The first assumption is that the origin is 0,0, and you know the co-ords of two other points.
(Since that is unlikely, when you use this for real, you need to translate the x,y of the two points by the x,y of the origin)
So the code below assumes 0,0 but if your origin is 10,30 you need to subtract 10 and 30 from the x,y of the other two co-eds before you begin.
Anyway, let’s go…
X1,Y1 is point 1 x2,y2 is point 2
In this example, I am setting up a situation where origin is 0,0
One point is at the top right corner of a square of 100 x 100
The second point is at the bottom right corner of the same square
(Remember that this math is ‘upside down’ to a computer screen. But the maths still works)
The angle between these two points should be 45 degrees, making it easy to check!
--Point 1
x1 = 100
y1 = 100
--Point 2
x2 = 100
y2 = 0
--two temporary variables to establish the slopes
n1 = math.sqrt(x1\*x1+y1\*y1);
n2 = math.sqrt(x2\*x2+y2\*y2);
--now the maths: all math is expressed in radians, so the bit at the end converts to degrees
angle = math.acos((x1\*x2+y1\*y2)/(n1\*n2)) \* 180 / math.pi;
print (angle) -- 45! Woohoo!
[import]uid: 108660 topic_id: 21074 reply_id: 83300[/import]