mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-19 05:39:29 +02:00
TAChart: Add RotatePoint utility function and various point operators
git-svn-id: trunk@26769 -
This commit is contained in:
parent
692a85e7f1
commit
e46c3429be
@ -209,6 +209,7 @@ function PointDistY(const A, B: TPoint): Integer; inline;
|
||||
function RectIntersectsRect(
|
||||
var ARect: TDoubleRect; const AFixed: TDoubleRect): Boolean;
|
||||
|
||||
function RotatePoint(const APoint: TPoint; AAngle: Double): TPoint;
|
||||
function RoundChecked(A: Double): Integer; inline;
|
||||
|
||||
function SafeInRange(AValue, ABound1, ABound2: Double): Boolean;
|
||||
@ -222,10 +223,16 @@ procedure UpdateMinMax(AValue: Double; var AMin, AMax: Double);
|
||||
operator +(const A: TPoint; B: TSize): TPoint; overload; inline;
|
||||
operator +(const A, B: TPoint): TPoint; overload; inline;
|
||||
operator +(const A, B: TDoublePoint): TDoublePoint; overload; inline;
|
||||
operator -(const A: TPoint): TPoint; overload; inline;
|
||||
operator -(const A, B: TPoint): TPoint; overload; inline;
|
||||
operator -(const A, B: TDoublePoint): TDoublePoint; overload; inline;
|
||||
operator div(const A: TPoint; ADivisor: Integer): TPoint; inline;
|
||||
operator *(const A: TPoint; AMultiplier: Integer): TPoint; inline;
|
||||
operator =(const A, B: TMethod): Boolean; overload; inline;
|
||||
|
||||
operator :=(const APoint: TPoint): TSize; inline;
|
||||
operator :=(const ASize: TSize): TPoint; inline;
|
||||
|
||||
implementation
|
||||
|
||||
function PointLineSide(AP, A1, A2: TPoint): TValueSign; forward;
|
||||
@ -607,6 +614,15 @@ begin
|
||||
RangesIntersect(a.Y, b.Y, AFixed.a.Y, AFixed.b.Y, a.Y, b.Y);
|
||||
end;
|
||||
|
||||
function RotatePoint(const APoint: TPoint; AAngle: Double): TPoint;
|
||||
var
|
||||
sa, ca: Extended;
|
||||
begin
|
||||
SinCos(AAngle, sa, ca);
|
||||
Result.X := Round(ca * APoint.X - sa * APoint.Y);
|
||||
Result.Y := Round(sa * APoint.X + ca * APoint.Y);
|
||||
end;
|
||||
|
||||
function RoundChecked(A: Double): Integer;
|
||||
begin
|
||||
Result := Round(EnsureRange(A, -MaxInt, MaxInt));
|
||||
@ -655,23 +671,53 @@ begin
|
||||
Result.Y := A.Y + B.Y;
|
||||
end;
|
||||
|
||||
operator - (const A: TPoint): TPoint;
|
||||
begin
|
||||
Result.X := - A.X;
|
||||
Result.Y := - A.Y;
|
||||
end;
|
||||
|
||||
operator - (const A, B: TPoint): TPoint;
|
||||
begin
|
||||
Result.X := A.X - B.X;
|
||||
Result.Y := A.Y - B.Y;
|
||||
end;
|
||||
|
||||
operator - (const A, B: TDoublePoint): TDoublePoint; overload; inline;
|
||||
operator - (const A, B: TDoublePoint): TDoublePoint;
|
||||
begin
|
||||
Result.X := A.X - B.X;
|
||||
Result.Y := A.Y - B.Y;
|
||||
end;
|
||||
|
||||
operator div(const A: TPoint; ADivisor: Integer): TPoint;
|
||||
begin
|
||||
Result.X := A.X div ADivisor;
|
||||
Result.Y := A.Y div ADivisor;
|
||||
end;
|
||||
|
||||
operator * (const A: TPoint; AMultiplier: Integer): TPoint;
|
||||
begin
|
||||
Result.X := A.X * AMultiplier;
|
||||
Result.Y := A.Y * AMultiplier;
|
||||
end;
|
||||
|
||||
operator = (const A, B: TMethod): Boolean;
|
||||
begin
|
||||
Result := (A.Code = B.Code) and (A.Data = B.Data);
|
||||
end;
|
||||
|
||||
operator := (const APoint: TPoint): TSize;
|
||||
begin
|
||||
Result.cx := APoint.X;
|
||||
Result.cy := APoint.Y;
|
||||
end;
|
||||
|
||||
operator := (const ASize: TSize): TPoint;
|
||||
begin
|
||||
Result.X := ASize.cx;
|
||||
Result.Y := ASize.cy;
|
||||
end;
|
||||
|
||||
{ TIntervalList }
|
||||
|
||||
procedure TIntervalList.AddPoint(APoint: Double); inline;
|
||||
|
@ -45,10 +45,12 @@ type
|
||||
TGeometryTest = class(TTestCase)
|
||||
private
|
||||
procedure AssertEquals(const Expected, Actual: TDoublePoint); overload;
|
||||
procedure AssertEquals(const Expected, Actual: TPoint); overload;
|
||||
published
|
||||
procedure TestLineIntersectsLine;
|
||||
procedure TestLineIntersectsRect;
|
||||
procedure TestPointOnLine;
|
||||
procedure TestPointOperations;
|
||||
procedure TestPointInPolygon;
|
||||
procedure TestPolygonIntersectsPolygon;
|
||||
end;
|
||||
@ -131,6 +133,12 @@ begin
|
||||
AssertEquals(Expected.Y, Actual.Y);
|
||||
end;
|
||||
|
||||
procedure TGeometryTest.AssertEquals(const Expected, Actual: TPoint);
|
||||
begin
|
||||
AssertEquals(Expected.X, Actual.X);
|
||||
AssertEquals(Expected.Y, Actual.Y);
|
||||
end;
|
||||
|
||||
procedure TGeometryTest.TestLineIntersectsLine;
|
||||
var
|
||||
p1, p2: TPoint;
|
||||
@ -218,6 +226,13 @@ begin
|
||||
AssertFalse(IsPointOnLine(Point(0, 1), Point(-1, 0), Point(1, 0)));
|
||||
end;
|
||||
|
||||
procedure TGeometryTest.TestPointOperations;
|
||||
begin
|
||||
AssertEquals(Point(1, 0), RotatePoint(Point(1, 0), 0.0));
|
||||
AssertEquals(Point(0, 1), RotatePoint(Point(1, 0), Pi / 2));
|
||||
AssertEquals(Point(14, 0), RotatePoint(Point(10, 10), -Pi / 4));
|
||||
end;
|
||||
|
||||
procedure TGeometryTest.TestPolygonIntersectsPolygon;
|
||||
|
||||
function OffsetPolygon(AP: array of TPoint; AOffset: TPoint): TPointArray;
|
||||
|
@ -2,7 +2,7 @@
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<PathDelim Value="\"/>
|
||||
<Version Value="7"/>
|
||||
<Version Value="8"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
|
Loading…
Reference in New Issue
Block a user