close Warning: Can't synchronize with repository "(default)" (No changeset 184 in the repository). Look in the Trac log for more information.

Changeset 165 for trunk/Packages


Ignore:
Timestamp:
Nov 22, 2017, 4:48:33 PM (7 years ago)
Author:
chronos
Message:
  • Added: Optimization phase for voronoi map generation.
File:
1 edited

Legend:

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

    r164 r165  
    1414
    1515  TLine = record
     16  private
     17    function GetDistance: Double;
     18    procedure SetDistance(AValue: Double);
     19  public
    1620    P1: TPoint;
    1721    P2: TPoint;
    1822    function Create(P1, P2: TPoint): TLine;
    19     function Distance: Double;
    2023    function GetMiddle: TPoint;
    2124    function GetAngle: Double;
     
    2528    procedure Rotate(Angle: Double);
    2629    class operator Equal(A, B: TLine): Boolean;
     30    property Distance: Double read GetDistance write SetDistance;
    2731  end;
    2832
     
    5660function PointInRect(P: TPoint; aRect: TRect): Boolean;
    5761function HalfDistancePoint(P1, P2: TPoint): TPoint;
     62function NormalizeAngle(Angle: Double): Double;
     63function SubAngle(A1, A2: Double): Double;
    5864
    5965implementation
     
    208214begin
    209215  Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
     216end;
     217
     218function NormalizeAngle(Angle: Double): Double;
     219begin
     220  if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
     221  else if Angle > 2 * Pi then Result := Angle - Trunc(Angle / (2 * Pi)) * (2 * Pi)
     222  else Result := Angle;
     223end;
     224
     225function SubAngle(A1, A2: Double): Double;
     226begin
     227  A1 := NormalizeAngle(A1);
     228  A2 := NormalizeAngle(A2);
     229  if A1 < A2 then Result := A1 + 2 * Pi - A2
     230    else Result := A1 - A2;
    210231end;
    211232
     
    310331      end else begin
    311332        // Crossing line, end polygon. If point NewPolygonStarted, the use polygon as result
     333        NewPoly.AddPoint(Points[I]);
    312334        NewPoly.AddPoint(Intersection);
    313335        if NewPoly.IsPointInside(PointInside) then begin
     
    326348    if PointsChecked > 2 * Length(Points) then Break;
    327349  end;
    328   if Success then Points := NewPoly.Points
    329     else Clear;
     350  if Success then Points := NewPoly.Points;
    330351end;
    331352
    332353{ TLine }
     354
     355function TLine.GetDistance: Double;
     356begin
     357  Result := Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y));
     358end;
     359
     360procedure TLine.SetDistance(AValue: Double);
     361var
     362  Angle: Double;
     363begin
     364  Angle := GetAngle;
     365  P2 := Point(Round(P1.X + Cos(Angle) * AValue),
     366    Round(P1.Y + Sin(Angle) * AValue));
     367end;
    333368
    334369function TLine.Create(P1, P2: TPoint): TLine;
     
    336371  Result.P1 := P1;
    337372  Result.P2 := P2;
    338 end;
    339 
    340 function TLine.Distance: Double;
    341 begin
    342   Result := Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y));
    343373end;
    344374
Note: See TracChangeset for help on using the changeset viewer.