直接上代码: 需要引用 Math 单元。
procedure CenterRadiusPoint(const PX, PY: Integer; const Angle: Double; const Radius: Integer; var X, Y: Integer); var AngleHude: Double; begin AngleHude := Angle * PI / 180; // 角度变成弧度 X := Round(Radius * Cos(AngleHude)) + PX; Y := Round(Radius * Sin(AngleHude)) + PY; end;
C#版:
/// 根据中心点、半径、角度,求半径另一端的坐标。注意用的是笛卡尔坐标系 /// </summary> /// <param name="center">中心点</param> /// <param name="angle">半径角度</param> /// <param name="radius">半径长度</param> /// <returns>半径另一端的坐标</returns> public static Point CenterRadiusPoint(Point center, double angle, double radius) { Point p = new Point(); double angleHude = angle * Math.PI / 180;/*角度变成弧度*/ p.X = (int)(radius * Math.Cos(angleHude)) + center .X; p.Y = (int)(radius * Math.Sin(angleHude)) + center .Y; return p; }
使用示例:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TForm1 = class(TForm) Timer1: TTimer; procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure Timer1Timer(Sender: TObject); private { Private declarations } AX, AY: Integer; R: Integer; public { Public declarations } procedure Clear(); procedure CenterRadiusPoint(const PX, PY: Integer; const Angle: Double; const Radius: Integer; var X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} uses Math; procedure TForm1.CenterRadiusPoint(const PX, PY: Integer; const Angle: Double; const Radius: Integer; var X, Y: Integer); var AngleHude: Double; begin AngleHude := Angle * PI / 180; // 角度变成弧度 X := Round(Radius * Cos(AngleHude)) + PX; Y := Round(Radius * Sin(AngleHude)) + PY; end; procedure TForm1.Clear; begin Canvas.FillRect(Canvas.ClipRect); end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin AX := X; AY := Y; R := 0; Clear(); end; procedure TForm1.Timer1Timer(Sender: TObject); var I: Integer; X, Y: Integer; T: Cardinal; begin Inc(R, 8); T := GetTickCount; for I := 0 to 360 - 1 do begin CenterRadiusPoint(AX, AY, I, R, X, Y); Canvas.Pixels[X, Y] := 0; end; T := GetTickCount - T; Canvas.TextOut(10, 10, IntToStr(T)); end; end.