ID:156485
 
How would I make an Atan2() proc for DM
http://www.byond.com/developer/forum/?id=455650&view=0

Oh, and Arctan:

proc/arctan(m) return arcsin(m/sqrt(1+m*m))
I assume you're trying to find an angle between two points? I'm sure there are more efficient ways of doing this but I used some stuff I learned from Calculus to develop a Get_Angle() proc which uses inverse cosine and some vector math to determine the angle between point (x0,y0) and point (x1,y1).

Anyways here it is:
/*
Angle returns the angle between the origin (x0, y0) and a point (x1, y1).
The angle will be a number in degrees between 0 and 359
*/

proc/Angle (x0, y0, x1, y1)
{
var/quadrant_modifier = 0
var/list/vector1 = list(x1-x0, y1-y0)
var/list/vector2
if(vector1[2] < 0)
{
vector2 = list(-1,0)
quadrant_modifier = 180
}
else vector2 = list(1,0)
var/angle = (arccos (Dot_Product (vector1, vector2) / (Magnitude (vector1) * Magnitude (vector2)))) + quadrant_modifier
return angle
}

/*
Dot_Product performs an equation from vector calculus. This equation is
essential for determining the angle between two vectors.
*/

proc/Dot_Product (vector1[2], vector2[2])
{
var/product = (vector1[1] * vector2[1]) + (vector1[2] * vector2[2])
return product
}

/*
Magnitude determines the magnitude of a vector. Magnitude is essential
for determining the angle between two vectors
*/

proc/Magnitude (vector[2])
{
return sqrt((vector[1]**2)+(vector[2]**2))
}
In response to Cody123100
Ily Cody.
ZOMG rage in the cage, I just found out jt_vectors already does this.

I'm a highschool freshman, smd.
In response to DarkCampainger
atan2() (or arctan2() or arctangent2() or whatever you want to call it) is different from just the arctangent function. atan() takes just one argument, which is the ratio y/x. However, the issue here is that the domain and range is then limited to half of the unit circle: y/x = -y/-x, and -y/x = y/-x. The range of the function, therefore, is only -pi/2 to pi/2. The atan2() function solves this by taking two arguments - y and x - and then using that to give a full range.

Granted, it's only a small difference in terms of lines of code (as shown by Lummox's example) but it really makes things neater and less prone to error.

Also that's a really poor explanation in retrospect. Anybody who's actually a mathematician able to say things properly?
In response to Garthor
Yep, that's why I linked to Lummox's snippet. I just threw the arctan() function in because his code relies on it, and most people don't know it by heart.
In response to Garthor
Not a mathematician, but yeah, we basically have the atan2 function because atan can't tell between opposite directions. Atan2 properly accounts for signs (it's a four-quadrant arctan).