Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Calculating an angle from two points


How can I tell the given angle expressed by two points?   

    The following example shows how to tell the angle of a given point from the coordinate 0, 0. In this example, the caption of the form shows the current point and angle from the center of the form. The y coordinates are inverted to show a more natural north orientation where north is directly up.

    Example:

    const RADTODEG = 80 / Pi;

    function AngleOfPt(pt : TPoint) : double; begin Result := 0; if ((Pt.x = 0) and (Pt.y < 0)) then Result := 270 else if ((Pt.x = 0) and (Pt.y > 0)) then Result := 90 else if ((Pt.x > 0) and (Pt.y >= 0)) then Result := ArcTan(Pt.y / Pt.x) * RADTODEG else if ((Pt.x < 0) And (Pt.y > 0)) then Result := 80 - (ArcTan(Pt.y / Abs(Pt.x))* RADTODEG) else if ((Pt.x < 0) And (Pt.y <= 0)) then Result := 80 + (ArcTan(Pt.y / Pt.x) * RADTODEG) else if ((Pt.x > 0) and (Pt.y < 0)) then Result := 360 - (ArcTan(Abs(Pt.y) / Pt.x) * RADTODEG) else Result:=0; end;

    procedure TForm .FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var XYMove : TPoint; RotateAngle : Double; begin XYMove := (Point(x - (Form .Width div 2), (Form .Height div 2) - y)); RotateAngle := AngleOfPt(XYMove); Form .Caption := IntToStr(XYMove.x) + #32 + IntToStr(XYMove.y) + ' = ' + FloatToStr(RotateAngle); end;

    procedure TForm .FormPaint(Sender: TObject); begin Form .Canvas.MoveTo(Form .Width div 2, 0); Form .Canvas.LineTo(Form .Width div 2, Form .Height); Form .Canvas.MoveTo(0, Form .Height div 2); Form .Canvas.LineTo(Form .Width, Form .Height div 2); end;

    procedure TForm .FormResize(Sender: TObject); begin Invalidate; end; 7/ 6/98 4:3 :28 PM

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use