Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Calculations with points

Title: Calculations with points Question: 1) Calculating a point using angles and distance 2) Calculating an angle from two points 3) Finding distance between two points Answer: 1) How do I calculate the x and y coordinates of a point that is at some distance and angle away? The following example shows how to convert a polar coordinate to a rectangular coordinate: procedure TForm1.Button1Click(Sender: TObject); var Angle : Double; x : Double; y : Double; Distance : Double; Radians : Double; begin Distance := 100; Angle := 270; Radians := Angle * DegToRad; x := Round(Distance * Cos(Radians)); y := Round(Distance * Sin(Radians)); ShowMessage(FloatToStr(x) + ' ' + FloatToStr(y)); end; 2) 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 = 180 / Pi; function AngleOfPt(pt : TPoint) : double; begin Result := 0; if ((Pt.x = 0) and (Pt.y 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)) then Result := 180 - (ArcTan(Pt.y / Abs(Pt.x))* RADTODEG) else if ((Pt.x Result := 180 + (ArcTan(Pt.y / Pt.x) * RADTODEG) else if ((Pt.x 0) and (Pt.y Result := 360 - (ArcTan(Abs(Pt.y) / Pt.x) * RADTODEG) else Result:=0; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var XYMove : TPoint; RotateAngle : Double; begin XYMove := (Point(x - (Form1.Width div 2), (Form1.Height div 2) - y)); RotateAngle := AngleOfPt(XYMove); Form1.Caption := IntToStr(XYMove.x) + #32 + IntToStr(XYMove.y) + ' = ' + FloatToStr(RotateAngle); end; procedure TForm1.FormPaint(Sender: TObject); begin Form1.Canvas.MoveTo(Form1.Width div 2, 0); Form1.Canvas.LineTo(Form1.Width div 2, Form1.Height); Form1.Canvas.MoveTo(0, Form1.Height div 2); Form1.Canvas.LineTo(Form1.Width, Form1.Height div 2); end; procedure TForm1.FormResize(Sender: TObject); begin Invalidate; end; 3) 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(Pt1 : TPoint; Pt2 : TPoint) : Double; var dx : LongInt; dy : LongInt; begin dx:= pt1.x - pt2.x; dy:= pt1.y - pt2.y; Result := Sqrt((Dx * Dx) + (Dy * Dy)); end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Pt1 : TPoint; Pt2 : TPoint; Dist : Double; begin Pt1.x := Form1.Width div 2; Pt1.y := Form1.Height div 2; Pt2.x := x; Pt2.y := y; Dist := Distance(Pt1, Pt2); Form1.Caption := IntToStr(Pt2.x - Pt1.x) + #32 + IntToStr(Pt1.y - Pt2.y) + ' = ' + FloatToStr(Dist); end; procedure TForm1.FormPaint(Sender: TObject); begin Form1.Canvas.MoveTo(Form1.Width div 2, 0); Form1.Canvas.LineTo(Form1.Width div 2, Form1.Height); Form1.Canvas.MoveTo(0, Form1.Height div 2); Form1.Canvas.LineTo(Form1.Width, Form1.Height div 2); end; procedure TForm1.FormResize(Sender: TObject); begin Invalidate; end;