Path: chuka.playstation.co.uk!news From: "Nick Slaven" Newsgroups: scee.yaroze.freetalk.english Subject: Re: Arc tangent Date: Thu, 30 Aug 2001 22:16:11 +0100 Organization: IO Productions Ltd Lines: 116 Message-ID: <9mmahd$ijv2@www.netyaroze-europe.com> References: <9mm95d$h6i12@www.netyaroze-europe.com> NNTP-Posting-Host: host213-123-222-24.in-addr.btopenworld.com X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6700 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 top banana! "Jon Prestidge (Jon@surfed.to)" wrote in message news:9mm95d$h6i12@www.netyaroze-europe.com... > OK everyone. There have been a couple of threads that could do with this > routine of mine I think. I am a mean get and don't usually give much source > code away, but I was starting to feel a bit too guilty on this one. I did > spend many hours writing this so keep it to yourselves and give me a mention > if you use it. It's not too accurate however (I do have a more up-to-date > version that I've developed that is more accurate, but it is also slower so > I think this is the one best suited). > > Just find dx and dy and pass `em to this routine and sit-back and have this > routine do all the work for you. Get it while it's hot. > > Jon > > > long auto_arc_tan( long v_dx, long v_dy ) > { > // (c) Jon Prestidge 1997 Do not copy, do not distribute. > // Version 1.0 > // automatic arc tangent routine. Just plug-in dx and dy and > // get back a full angle between 0 and 4096. > // > long grad, ang, abs_dx, abs_dy, half; > > abs_dx = v_dx; > if ( abs_dx < 0 ) abs_dx *= -1; > abs_dy = v_dy; > if ( abs_dy < 0 ) abs_dy *= -1; > > // find the gradient between 0 and one (or 0 and 4096 in this > impelmentation).... > if ( abs_dy <= abs_dx ) > { > if ( abs_dx == 0 ) > { // exact angle can be returned... also this prevents divide by 0 > error... > if ( v_dy >= 0 ) > return 0; > else > return 2048; > } > grad = abs_dy; > grad <<= 12; > grad /= abs_dx; > half = 1; > } > else > { > if ( abs_dy == 0 ) > { // exact angle can be returned... also this prevents divide by 0 > error... > if ( v_dx >= 0 ) > return 1024; > else > return 3072; > } > grad = abs_dx; > grad <<= 12; > grad /= abs_dy; > half = 0; > } > > ang = ( grad * 2409 ) >> 12; > ang *= 4096 - ang; > ang += grad * 3097; > ang >>= 15; > > if ( v_dx >= 0 ) > { > if ( v_dy >= 0 ) > { > if ( half ) ang = 1024 - ang; > } > else > { > if ( half ) > ang += 1024; > else > ang = 2048 - ang; > } > } > else > { > if ( v_dy >= 0 ) > { > if ( half ) > ang += 3072; > else > ang = 4096 - ang; > } > else > { > if ( half ) > ang = 3072 - ang; > else > ang += 2048; > } > } > > return ang; > } > > >