Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Finding distance between two points.


How can I find the distance between two points?   

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

    Example:

    funcTion Distance(Pt : TPoint; Pt2 : TPoint) : Double; var dx : LongInt; dy : LongInt; begin dx:= pt .x - pt2.x; dy:= pt .y - pt2.y; Result := Sqrt((Dx * Dx) + (Dy * Dy)); end;

    procedure TForm .FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Pt : TPoint; Pt2 : TPoint; Dist : Double; begin Pt .x := Form .Width div 2; Pt .y := Form .Height div 2; Pt2.x := x; Pt2.y := y; Dist := Distance(Pt , Pt2); Form .Caption := IntToStr(Pt2.x - Pt .x) + #32 + IntToStr(Pt .y - Pt2.y) + ' = ' + FloatToStr(Dist); 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