Changeset 167 for trunk/Packages/Common


Ignore:
Timestamp:
Nov 23, 2017, 9:57:48 AM (7 years ago)
Author:
chronos
Message:
  • Fixed: Generate map only once on application start.
  • Modified: Use const function parameter modifier in Geometry unit.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UGeometry.pas

    r166 r167  
    2020    P1: TPoint;
    2121    P2: TPoint;
    22     function Create(P1, P2: TPoint): TLine;
     22    function Create(const P1, P2: TPoint): TLine;
    2323    function GetMiddle: TPoint;
    2424    function GetAngle: Double;
     
    2626    function ToRect: TRect;
    2727    function DotProduct: Double;
    28     procedure Rotate(Angle: Double);
    29     class operator Equal(A, B: TLine): Boolean;
     28    procedure Rotate(const Angle: Double);
     29    class operator Equal(const A, B: TLine): Boolean;
    3030    property Distance: Double read GetDistance write SetDistance;
    3131  end;
     
    3535  TPolygon = record
    3636    Points: TPointArray;
    37     function IsPointInside(P: TPoint): Boolean;
    38     function Create(Points: TPointArray): TPolygon; overload;
    39     function Create(Rect: TRect): TPolygon; overload;
    40     procedure AddPoint(P: TPoint);
     37    function IsPointInside(const P: TPoint): Boolean;
     38    function Create(const Points: TPointArray): TPolygon; overload;
     39    function Create(const Rect: TRect): TPolygon; overload;
     40    procedure AddPoint(const P: TPoint);
    4141    procedure Clear;
    42     procedure CutLine(Vector: TLine; PointInside: TPoint);
    43   end;
    44 
    45 function Distance(P1, P2: TPoint): Integer;
     42    procedure CutLine(const Vector: TLine; const PointInside: TPoint);
     43  end;
     44
     45function Distance(const P1, P2: TPoint): Integer;
    4646function Dot(const P1, P2: TPoint): Double;
    4747function AddPoint(const P1, P2: TPoint): TPoint;
    4848function SubPoint(const P1, P2: TPoint): TPoint;
    4949function PointToLineDistance(const P, V, W: TPoint): Integer;
    50 function ComparePoint(P1, P2: TPoint): Boolean;
    51 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
    52 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
    53 function LineIntersect(LineA, LineB: TLine; out Intersection: TPoint): Boolean;
    54 function ArcTan2Point(Point: TPoint): Float;
    55 function ArcTanPoint(Point: TPoint): Float;
    56 function RectEquals(A, B: TRect): Boolean;
    57 function RectEnlarge(Rect: TRect; Value: Integer): TRect;
    58 function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
    59 function PointsToRect(P1, P2: TPoint): TRect;
    60 function PointInRect(P: TPoint; aRect: TRect): Boolean;
    61 function HalfDistancePoint(P1, P2: TPoint): TPoint;
    62 function NormalizeAngle(Angle: Double): Double;
     50function ComparePoint(const P1, P2: TPoint): Boolean;
     51function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
     52function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
     53function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
     54function ArcTan2Point(const Point: TPoint): Float;
     55function ArcTanPoint(const Point: TPoint): Float;
     56function RectEquals(const A, B: TRect): Boolean;
     57function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
     58function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
     59function PointsToRect(const P1, P2: TPoint): TRect;
     60function PointInRect(const P: TPoint; aRect: TRect): Boolean;
     61function HalfDistancePoint(const P1, P2: TPoint): TPoint;
     62function NormalizeAngle(const Angle: Double): Double;
    6363function SubAngle(A1, A2: Double): Double;
    6464
    6565implementation
    6666
    67 function Distance(P1, P2: TPoint): Integer;
     67function Distance(const P1, P2: TPoint): Integer;
    6868begin
    6969  Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
     
    116116end;
    117117
    118 function ComparePoint(P1, P2: TPoint): Boolean;
     118function ComparePoint(const P1, P2: TPoint): Boolean;
    119119begin
    120120  Result := (P1.X = P2.X) and (P1.Y = P2.Y);
    121121end;
    122122
    123 function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
    124 begin
    125   P := Point(P.X - Center.X, P.Y - Center.Y);
    126   Result := Point(Center.X + Round(P.X * Cos(Angle) - P.Y * Sin(Angle)),
    127     Center.Y + Round(P.X * Sin(Angle) + P.Y * Cos(Angle)));
    128 end;
    129 
    130 function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
     123function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
     124var
     125  D: TPoint;
     126begin
     127  D := Point(P.X - Center.X, P.Y - Center.Y);
     128  Result := Point(Center.X + Round(D.X * Cos(Angle) - D.Y * Sin(Angle)),
     129    Center.Y + Round(D.X * Sin(Angle) + D.Y * Cos(Angle)));
     130end;
     131
     132function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
    131133var
    132134  I: Integer;
     
    137139end;
    138140
    139 function LineIntersect(LineA, LineB: TLine; out Intersection: TPoint): Boolean;
     141function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
    140142Var
    141143  LDetLineA, LDetLineB, LDetDivInv: Double;
     
    143145  D: Integer;
    144146begin
    145   if (LineA.P1 = LineA.P2) or (LineB.P1 = LineB.P2) then begin
     147  if ComparePoint(LineA.P1, LineA.P2) or ComparePoint(LineB.P1, LineB.P2) then begin
    146148    Result := False;
    147149    Exit;
     
    166168end;
    167169
    168 function ArcTan2Point(Point: TPoint): Float;
     170function ArcTan2Point(const Point: TPoint): Float;
    169171begin
    170172  Result := ArcTan2(Point.Y, Point.X);
    171173end;
    172174
    173 function ArcTanPoint(Point: TPoint): Float;
     175function ArcTanPoint(const Point: TPoint): Float;
    174176begin
    175177  if Point.Y = 0 then Result := Infinity
     
    177179end;
    178180
    179 function RectEquals(A, B: TRect): Boolean;
     181function RectEquals(const A, B: TRect): Boolean;
    180182begin
    181183  Result := (A.Left = B.Left) and (A.Top = B.Top) and
     
    183185end;
    184186
    185 function RectEnlarge(Rect: TRect; Value: Integer): TRect;
     187function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
    186188begin
    187189  Result.Left := Rect.Left - Value;
     
    191193end;
    192194
    193 function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
     195function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
    194196begin
    195197  Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
     
    197199end;
    198200
    199 function PointsToRect(P1, P2: TPoint): TRect;
     201function PointsToRect(const P1, P2: TPoint): TRect;
    200202begin
    201203  if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
     
    205207end;
    206208
    207 function PointInRect(P: TPoint; aRect: TRect): Boolean;
     209function PointInRect(const P: TPoint; aRect: TRect): Boolean;
    208210begin
    209211  Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
     
    211213end;
    212214
    213 function HalfDistancePoint(P1, P2: TPoint): TPoint;
     215function HalfDistancePoint(const P1, P2: TPoint): TPoint;
    214216begin
    215217  Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
    216218end;
    217219
    218 function NormalizeAngle(Angle: Double): Double;
     220function NormalizeAngle(const Angle: Double): Double;
    219221begin
    220222  if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
     
    233235{ TPolygon }
    234236
    235 function TPolygon.IsPointInside(P: TPoint): Boolean;
     237function TPolygon.IsPointInside(const P: TPoint): Boolean;
    236238var
    237239  I, J: Integer;
     
    273275
    274276
    275 function TPolygon.Create(Points: TPointArray): TPolygon;
     277function TPolygon.Create(const Points: TPointArray): TPolygon;
    276278var
    277279  I: Integer;
     
    282284end;
    283285
    284 function TPolygon.Create(Rect: TRect): TPolygon;
     286function TPolygon.Create(const Rect: TRect): TPolygon;
    285287begin
    286288  SetLength(Result.Points, 4);
     
    291293end;
    292294
    293 procedure TPolygon.AddPoint(P: TPoint);
     295procedure TPolygon.AddPoint(const P: TPoint);
    294296begin
    295297  SetLength(Points, Length(Points) + 1);
     
    302304end;
    303305
    304 procedure TPolygon.CutLine(Vector: TLine; PointInside: TPoint);
     306procedure TPolygon.CutLine(const Vector: TLine; const PointInside: TPoint);
    305307var
    306308  I: Integer;
     
    367369end;
    368370
    369 function TLine.Create(P1, P2: TPoint): TLine;
     371function TLine.Create(const P1, P2: TPoint): TLine;
    370372begin
    371373  Result.P1 := P1;
     
    401403end;
    402404
    403 procedure TLine.Rotate(Angle: Double);
     405procedure TLine.Rotate(const Angle: Double);
    404406begin
    405407  P2 := RotatePoint(P1, P2, Angle);
    406408end;
    407409
    408 class operator TLine.Equal(A, B: TLine): Boolean;
    409 begin
    410   Result := (A.P1 = B.P1) and (A.P2 = B.P2);
    411 end;
    412 
     410class operator TLine.Equal(const A, B: TLine): Boolean;
     411begin
     412  Result := ComparePoint(A.P1, B.P1) and ComparePoint(A.P2, B.P2);
     413end;
    413414
    414415end.
Note: See TracChangeset for help on using the changeset viewer.