From e46c3429becd0e37a13e0226f443de5c8323f880 Mon Sep 17 00:00:00 2001 From: ask Date: Thu, 22 Jul 2010 04:39:30 +0000 Subject: [PATCH] TAChart: Add RotatePoint utility function and various point operators git-svn-id: trunk@26769 - --- components/tachart/tachartutils.pas | 48 ++++++++++++++++++++++++++- components/tachart/test/UtilsTest.pas | 15 +++++++++ components/tachart/test/test.lpi | 2 +- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/components/tachart/tachartutils.pas b/components/tachart/tachartutils.pas index 467f8b45e1..fa3d5662c9 100644 --- a/components/tachart/tachartutils.pas +++ b/components/tachart/tachartutils.pas @@ -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; diff --git a/components/tachart/test/UtilsTest.pas b/components/tachart/test/UtilsTest.pas index 93ef516e4d..339e9b6bdf 100644 --- a/components/tachart/test/UtilsTest.pas +++ b/components/tachart/test/UtilsTest.pas @@ -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; diff --git a/components/tachart/test/test.lpi b/components/tachart/test/test.lpi index f95e949520..41d0459d25 100644 --- a/components/tachart/test/test.lpi +++ b/components/tachart/test/test.lpi @@ -2,7 +2,7 @@ - +