From: no DOT spam DOT please AT thanks DOT com Newsgroups: comp.os.msdos.djgpp Subject: Re: Tank Movement Date: 7 Oct 1998 01:32:55 GMT Organization: Texas Instruments Lines: 52 Message-ID: <6vegc7$4cq$1@superb.csc.ti.com> References: <36147a32 DOT 10039817 AT ct-news DOT iafrica DOT com> <6vebp7$sap$1 AT fir DOT prod DOT itd DOT earthlink DOT net> NNTP-Posting-Host: lenny.dseg.ti.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: superb.csc.ti.com 907723975 4506 172.25.38.136 (7 Oct 1998 01:32:55 GMT) X-Complaints-To: abuse AT ti DOT com NNTP-Posting-Date: 7 Oct 1998 01:32:55 GMT X-Newsreader: knews 0.9.8 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com In article <6vebp7$sap$1 AT fir DOT prod DOT itd DOT earthlink DOT net>, "JEFF" writes: > > Chris Brooker wrote in message <36147a32 DOT 10039817 AT ct-news DOT iafrica DOT com>... >>Hi, >>Sorry if my code sucks, I a mquite new to C. >>I am busy writing a Real Time Strategy ... > >>double dir_degrees(int x1, int y1, int y2, int x2) >>{ >> int v1, v2; >> double dir; >> v1 = y1 - y2; >> v2 = x1 - x2; >> if (v2 == 0) >> v2++; //Stops division by zero errors >> dir = 57 * atan(v1 / v2); >> return dir; >>} >> > > The atan() function can only return values between -90 and 90 degrees, which > is two out of four quandrants. You need to check the signs of v1 and v2 to > determine if the angle is between -180 and -90 or between 90 and 180 > degrees and add or subtract 90 degrees accordingly. > > If x and y coordinates are standard cartesian, with "right" as positive x > and "up" as positive y: > > if x>0 and y>0, the angle must be 0 to 90 degrees. No adjustment. > if x>0 and y<0, angle is -90 to 0. No adjustment. > if x<0 and y>0 angle is 90 to 180. Add 180 degrees to return value. > if x<0 and y<0 angle is -180 to -90. Subtract 180 degrees from return > value. > > You may need to experiment to get the right adjustment as I have not tested > it. Good luck. > > -Jeff A much easier approach - use atan2(), which takes care of the quadrant mapping for you, e.g., double dir_degrees(int x1, int y1, int y2, int x2) { int v1, v2; double dir; v1 = y1 - y2; v2 = x1 - x2; dir = 57 * atan( v1, v2 ); return dir; }