atan2() function!
Displaying 1-5 of 5 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Overkill

Fixed since its previous release for increased accuracy. Use it wherever need be.


// Overkill's atan2()
// Finds the angle from (x1,y1) to (x2,y2).
//
// Does all the bitshifting and such for you.
// Accuracy of angles has improved greatly!
//
// 9 0
// |
// 0 -- -- 1 8 0
// |
// 2 7 0

int abs(int a)
{
if (a<0) return 0-a;
else return a;
}

int atan2(int x1, int y1, int x2, int y2)
{
int x, y;
int r, a;
x=x2-x1;
y=y2-y1;
if (x)
{
r=y*65536/x;
}
else
{
r=y*65536*1000000; // Same as dividing by 0.0000001, instead of 0 which gives an infinite number.
}
a = atan(abs(r));
if(x<0 && y>0)
{
a=180-a;
}
if(x<0 && y<0)
{
a=180+a;
}
if(x>0 && y<0)
{
a=0-a;
}
//This part doesn't mean anything, really...
if(x>0 && y>0)
{
a=0+a;
}

if (a<0) a=360+a;
if(x>0-2 && x<2)
{
if(y>0) a=90;
else if(y<=0) a=270;
}
if(y>0-2 && y<2)
{
if(x>0) a=0;
else if(x<=0) a=180;
}
return a;
}

Posted on 2004-04-25 01:21:17

mcgrue

Cool.

But how does it work? I mean, er, don't you need another point for the vertex? Isn't the angle between any two points without an outside frame of reference 180? Am I daft here?

Posted on 2004-04-25 03:38:36

vecna

The frame of reference is the absolute orientation of the screen. It's implied.

Posted on 2004-04-25 04:15:07

mcgrue

It's been forever since I did any real geometry. Examples plz? :(

Posted on 2004-04-25 05:20:46

Overkill

It should only need two points, as it figures out the x difference and ydifference which is used to find the angle to get from point A to point B. It works fine as far as I know.

Here's a test for it:


int screenx=ImageWidth(screen);
int screeny=ImageHeight(screen);
void Autoexec()
{
int done;
while(!done)
{
RectFill(0,0,screenx,screeny,rgb(0,0,0),screen);
Printstring(0,0,screen,0,str(atan2(mouse.x,mouse.y,screenx/2,screeny/2) ) );
Rect(mouse.x,mouse.y,mouse.x+8,mouse.y+8,rgb(255,255,255),screen);
Line(mouse.x,mouse.y,screenx/2,screeny/2,rgb(20,102,255),screen);
Line(mouse.x+8,mouse.y,screenx/2,screeny/2,rgb(20,102,255),screen);
Line(mouse.x,mouse.y+8,screenx/2,screeny/2,rgb(20,102,255),screen);
Line(mouse.x+8,mouse.y+8,screenx/2,screeny/2,rgb(20,102,255),screen);
Showpage();
}
exit("");
}


---

Edit: Crap! The example shows that it goes from x2, y2 to x1, y1. I think I screwed up there, but that's easily fixable with a little tweaking.

Also: I note that this function doesn't do that much, really. If I did anything more, it'd be in a demo of some sort.

Posted on 2004-04-25 05:25:56 (last edited on 2004-04-25 05:41:32)


Displaying 1-5 of 5 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.