123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070 |
- unit Clipper.Core;
- (*******************************************************************************
- * Author : Angus Johnson *
- * Date : 2 November 2022 *
- * Copyright : Angus Johnson 2010-2022 *
- * Purpose : Core Clipper Library module *
- * Contains structures and functions used throughout the library *
- * License : http://www.boost.org/LICENSE_1_0.txt *
- *******************************************************************************)
- {$I Clipper.inc}
- interface
- uses
- SysUtils, Math;
- type
- PPoint64 = ^TPoint64;
- TPoint64 = record
- X, Y: Int64;
- {$IFDEF USINGZ}
- Z: Int64;
- {$ENDIF}
- end;
- PPointD = ^TPointD;
- TPointD = record
- X, Y: double;
- {$IFDEF USINGZ}
- Z: Int64;
- {$ENDIF}
- end;
- // Path: a simple data structure representing a series of vertices, whether
- // open (poly-line) or closed (polygon). Paths may be simple or complex (self
- // intersecting). For simple polygons, consisting of a single non-intersecting
- // path, path orientation is unimportant. However, for complex polygons and
- // for overlapping polygons, various 'filling rules' define which regions will
- // be inside (filled) and which will be outside (unfilled).
- TPath64 = array of TPoint64;
- TPaths64 = array of TPath64;
- TArrayOfPaths = array of TPaths64;
- TPathD = array of TPointD;
- TPathsD = array of TPathD;
- TArrayOfPathsD = array of TPathsD;
- // The most commonly used filling rules for polygons are EvenOdd and NonZero.
- // https://en.wikipedia.org/wiki/Even-odd_rule
- // https://en.wikipedia.org/wiki/Nonzero-rule
- TFillRule = (frEvenOdd, frNonZero, frPositive, frNegative);
- TArrayOfBoolean = array of Boolean;
- TArrayOfInteger = array of Integer;
- TArrayOfDouble = array of double;
- TRect64 = {$IFDEF RECORD_METHODS}record{$ELSE}object{$ENDIF}
- private
- function GetWidth: Int64; {$IFDEF INLINING} inline; {$ENDIF}
- function GetHeight: Int64; {$IFDEF INLINING} inline; {$ENDIF}
- function GetIsEmpty: Boolean; {$IFDEF INLINING} inline; {$ENDIF}
- function GetMidPoint: TPoint64; {$IFDEF INLINING} inline; {$ENDIF}
- public
- Left : Int64;
- Top : Int64;
- Right : Int64;
- Bottom : Int64;
- function Contains(const pt: TPoint64; inclusive: Boolean = false): Boolean; overload;
- function Contains(const rec: TRect64): Boolean; overload;
- function Intersect(const rec: TRect64): TRect64;
- function Intersects(const rec: TRect64): Boolean;
- function AsPath: TPath64;
- property Width: Int64 read GetWidth;
- property Height: Int64 read GetHeight;
- property IsEmpty: Boolean read GetIsEmpty;
- property MidPoint: TPoint64 read GetMidPoint;
- end;
- TRectD = {$ifdef RECORD_METHODS}record{$else}object{$endif}
- private
- function GetWidth: double; {$IFDEF INLINING} inline; {$ENDIF}
- function GetHeight: double; {$IFDEF INLINING} inline; {$ENDIF}
- function GetIsEmpty: Boolean; {$IFDEF INLINING} inline; {$ENDIF}
- function GetMidPoint: TPointD; {$IFDEF INLINING} inline; {$ENDIF}
- public
- Left : double;
- Top : double;
- Right : double;
- Bottom : double;
- function Contains(const pt: TPointD): Boolean; overload;
- function Contains(const rec: TRectD): Boolean; overload;
- function Intersects(const rec: TRectD): Boolean;
- function AsPath: TPathD;
- property Width: double read GetWidth;
- property Height: double read GetHeight;
- property IsEmpty: Boolean read GetIsEmpty;
- property MidPoint: TPointD read GetMidPoint;
- end;
- TClipType = (ctNone, ctIntersection, ctUnion, ctDifference, ctXor);
- TPointInPolygonResult = (pipInside, pipOutside, pipOn);
- EClipperLibException = class(Exception);
- function Area(const path: TPath64): Double; overload;
- function Area(const paths: TPaths64): Double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function Area(const path: TPathD): Double; overload;
- function Area(const paths: TPathsD): Double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function IsPositive(const path: TPath64): Boolean; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function IsPositive(const path: TPathD): Boolean; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function CrossProduct(const pt1, pt2, pt3: TPoint64): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function CrossProduct(const pt1, pt2, pt3: TPointD): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function CrossProduct(const vec1, vec2: TPointD): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function CrossProduct(vec1x, vec1y, vec2x, vec2y: double): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function DotProduct(const pt1, pt2, pt3: TPoint64): double;
- {$IFDEF INLINING} inline; {$ENDIF}
- function DistanceSqr(const pt1, pt2: TPoint64): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function DistanceSqr(const pt1, pt2: TPointD): double; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function DistanceFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double; overload;
- function DistanceFromLineSqrd(const pt, linePt1, linePt2: TPointD): double; overload;
- function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64;
- inclusive: Boolean = false): boolean; {$IFDEF INLINING} inline; {$ENDIF}
- function PointsEqual(const pt1, pt2: TPoint64): Boolean; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function PointsNearEqual(const pt1, pt2: TPointD): Boolean; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function PointsNearEqual(const pt1, pt2: TPointD; distanceSqrd: double): Boolean; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- {$IFDEF USINGZ}
- function Point64(const X, Y: Int64; Z: Int64 = 0): TPoint64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function Point64(const X, Y: Double; Z: Int64 = 0): TPoint64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function PointD(const X, Y: Double; Z: Int64 = 0): TPointD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- {$ELSE}
- function Point64(const X, Y: Int64): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF}
- function Point64(const X, Y: Double): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF}
- function PointD(const X, Y: Double): TPointD; overload; {$IFDEF INLINING} inline; {$ENDIF}
- {$ENDIF}
- function Negate(const pt: TPointD): TPointD; {$IFDEF INLINING} inline; {$ENDIF}
- function Point64(const pt: TPointD): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF}
- function PointD(const pt: TPoint64): TPointD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function Rect64(const left, top, right, bottom: Int64): TRect64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function Rect64(const recD: TRectD): TRect64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function RectD(const left, top, right, bottom: double): TRectD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function RectD(const rec64: TRect64): TRectD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function GetBounds(const paths: TArrayOfPaths): TRect64; overload;
- function GetBounds(const paths: TPaths64): TRect64; overload;
- function GetBounds(const paths: TPathsD): TRectD; overload;
- function GetBounds(const path: TPath64): TRect64; overload;
- function GetBounds(const path: TPathD): TRectD; overload;
- function TranslatePoint(const pt: TPoint64; dx, dy: Int64): TPoint64; overload;
- function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD; overload;
- procedure RotatePt(var pt: TPointD; const center: TPointD; sinA, cosA: double);
- procedure RotatePath(var path: TPathD; const center: TPointD; sinA, cosA: double);
- procedure InflateRect(var rec: TRect64; dx, dy: Int64); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- procedure InflateRect(var rec: TRectD; dx, dy: double); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function UnionRect(const rec, rec2: TRect64): TRect64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function UnionRect(const rec, rec2: TRectD): TRectD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function RotateRect(const rec: TRect64; angleRad: double): TRect64; overload;
- function RotateRect(const rec: TRectD; angleRad: double): TRectD; overload;
- procedure OffsetRect(var rec: TRect64; dx, dy: Int64); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- procedure OffsetRect(var rec: TRectD; dx, dy: double); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ScaleRect(const rec: TRect64; scale: double): TRect64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ScaleRect(const rec: TRectD; scale: double): TRectD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ScalePoint(const pt: TPoint64; scale: double): TPointD;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ScalePath(const path: TPath64; sx, sy: double): TPath64; overload;
- function ScalePath(const path: TPathD; sx, sy: double): TPath64; overload;
- function ScalePath(const path: TPath64; scale: double): TPath64; overload;
- function ScalePath(const path: TPathD; scale: double): TPath64; overload;
- function ScalePathD(const path: TPath64; sx, sy: double): TPathD; overload;
- function ScalePathD(const path: TPathD; sx, sy: double): TPathD; overload;
- function ScalePathD(const path: TPath64; scale: double): TPathD; overload;
- function ScalePathD(const path: TPathD; scale: double): TPathD; overload;
- function ScalePaths(const paths: TPaths64; sx, sy: double): TPaths64; overload;
- function ScalePaths(const paths: TPathsD; sx, sy: double): TPaths64; overload;
- function ScalePaths(const paths: TPaths64; scale: double): TPaths64; overload;
- function ScalePaths(const paths: TPathsD; scale: double): TPaths64; overload;
- function ScalePathsD(const paths: TPaths64; sx, sy: double): TPathsD; overload;
- function ScalePathsD(const paths: TPathsD; sx, sy: double): TPathsD; overload;
- function ScalePathsD(const paths: TPaths64; scale: double): TPathsD; overload;
- function ScalePathsD(const paths: TPathsD; scale: double): TPathsD; overload;
- function Path64(const pathD: TPathD): TPath64;
- function PathD(const path: TPath64): TPathD;
- function Paths64(const pathsD: TPathsD): TPaths64;
- function PathsD(const paths: TPaths64): TPathsD;
- function StripDuplicates(const path: TPath64; isClosedPath: Boolean = false): TPath64;
- function StripNearDuplicates(const path: TPathD;
- minLenSqrd: double; isClosedPath: Boolean): TPathD;
- function ValueBetween(val, end1, end2: Int64): Boolean;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ValueEqualOrBetween(val, end1, end2: Int64): Boolean;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ReversePath(const path: TPath64): TPath64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ReversePath(const path: TPathD): TPathD; overload;
- function ReversePaths(const paths: TPaths64): TPaths64; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function ReversePaths(const paths: TPathsD): TPathsD; overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- procedure AppendPoint(var path: TPath64; const pt: TPoint64); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- procedure AppendPoint(var path: TPathD; const pt: TPointD); overload;
- {$IFDEF INLINING} inline; {$ENDIF}
- function AppendPoints(const path, extra: TPath64): TPath64;
- {$IFDEF INLINING} inline; {$ENDIF}
- procedure AppendPath(var paths: TPaths64; const extra: TPath64); overload;
- procedure AppendPath(var paths: TPathsD; const extra: TPathD); overload;
- procedure AppendPaths(var paths: TPaths64; const extra: TPaths64); overload;
- procedure AppendPaths(var paths: TPathsD; const extra: TPathsD); overload;
- function ArrayOfPathsToPaths(const ap: TArrayOfPaths): TPaths64;
- function GetIntersectPoint64(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPoint64;
- function GetIntersectPointD(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPointD; overload;
- function GetIntersectPointD(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD; overload;
- function GetClosestLineEnd(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPoint64; overload;
- function GetClosestLineEnd(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD; overload;
- function PointInPolygon(const pt: TPoint64; const polygon: TPath64): TPointInPolygonResult;
- function RamerDouglasPeucker(const path: TPath64; epsilon: double): TPath64; overload;
- function RamerDouglasPeucker(const paths: TPaths64; epsilon: double): TPaths64; overload;
- function RamerDouglasPeucker(const path: TPathD; epsilon: double): TPathD; overload;
- function RamerDouglasPeucker(const paths: TPathsD; epsilon: double): TPathsD; overload;
- procedure GetSinCos(angle: double; out sinA, cosA: double);
- function Ellipse(const rec: TRect64; steps: integer = 0): TPath64; overload;
- function Ellipse(const rec: TRectD; steps: integer = 0): TPathD; overload;
- procedure CheckPrecisionRange(var precision: integer);
- const
- MaxInt64 = 9223372036854775807;
- invalid64 = MaxInt64;
- invalidD = infinity;
- NullPointD : TPointD = (X: 0; Y: 0);
- NullRect64 : TRect64 = (left: 0; top: 0; right: 0; Bottom: 0);
- InvalidPt64 : TPoint64 = (X: invalid64; Y: invalid64);
- InvalidPtD : TPointD = (X: invalidD; Y: invalidD);
- NullRectD : TRectD = (left: 0; top: 0; right: 0; Bottom: 0);
- Tolerance : Double = 1.0E-12;
- implementation
- resourcestring
- rsClipper_PrecisonErr = 'The decimal rounding value is invalid';
- //------------------------------------------------------------------------------
- // TRect64 methods ...
- //------------------------------------------------------------------------------
- function TRect64.GetWidth: Int64;
- begin
- result := right - left;
- end;
- //------------------------------------------------------------------------------
- function TRect64.GetHeight: Int64;
- begin
- result := bottom - top;
- end;
- //------------------------------------------------------------------------------
- function TRect64.GetIsEmpty: Boolean;
- begin
- result := (bottom <= top) or (right <= left);
- end;
- //------------------------------------------------------------------------------
- function TRect64.GetMidPoint: TPoint64;
- begin
- result := Point64((Left + Right) div 2, (Top + Bottom) div 2);
- end;
- //------------------------------------------------------------------------------
- function TRect64.Contains(const pt: TPoint64; inclusive: Boolean = false): Boolean;
- begin
- if inclusive then
- result := (pt.X >= Left) and (pt.X <= Right) and
- (pt.Y >= Top) and (pt.Y <= Bottom)
- else
- result := (pt.X > Left) and (pt.X < Right) and
- (pt.Y > Top) and (pt.Y < Bottom);
- end;
- //------------------------------------------------------------------------------
- function TRect64.Contains(const rec: TRect64): Boolean;
- begin
- result := (rec.Left >= Left) and (rec.Right <= Right) and
- (rec.Top >= Top) and (rec.Bottom <= Bottom);
- end;
- //------------------------------------------------------------------------------
- function TRect64.Intersects(const rec: TRect64): Boolean;
- begin
- Result := (Max(Left, rec.Left) < Min(Right, rec.Right)) and
- (Max(Top, rec.Top) < Min(Bottom, rec.Bottom));
- end;
- //------------------------------------------------------------------------------
- function TRect64.Intersect(const rec: TRect64): TRect64;
- begin
- Result.Left := Max(Left, rec.Left);
- Result.Top := Max(Top, rec.Top);
- Result.Right := Min(Right, rec.Right);
- Result.Bottom := Min(Bottom, rec.Bottom);
- if IsEmpty then Result := NullRect64;
- end;
- //------------------------------------------------------------------------------
- function TRect64.AsPath: TPath64;
- begin
- SetLength(Result, 4);
- Result[0] := Point64(Left, Top);
- Result[1] := Point64(Right, Top);
- Result[2] := Point64(Right, Bottom);
- Result[3] := Point64(Left, Bottom);
- end;
- //------------------------------------------------------------------------------
- // TRectD methods ...
- //------------------------------------------------------------------------------
- function TRectD.GetWidth: double;
- begin
- result := right - left;
- end;
- //------------------------------------------------------------------------------
- function TRectD.GetHeight: double;
- begin
- result := bottom - top;
- end;
- //------------------------------------------------------------------------------
- function TRectD.GetIsEmpty: Boolean;
- begin
- result := (bottom <= top) or (right <= left);
- end;
- //------------------------------------------------------------------------------
- function TRectD.GetMidPoint: TPointD;
- begin
- result := PointD((Left + Right) *0.5, (Top + Bottom) *0.5);
- end;
- //------------------------------------------------------------------------------
- function TRectD.Contains(const pt: TPointD): Boolean;
- begin
- result := (pt.X > Left) and (pt.X < Right) and
- (pt.Y > Top) and (pt.Y < Bottom);
- end;
- //------------------------------------------------------------------------------
- function TRectD.Contains(const rec: TRectD): Boolean;
- begin
- result := (rec.Left >= Left) and (rec.Right <= Right) and
- (rec.Top >= Top) and (rec.Bottom <= Bottom);
- end;
- //------------------------------------------------------------------------------
- function TRectD.Intersects(const rec: TRectD): Boolean;
- begin
- Result := (Max(Left, rec.Left) < Min(Right, rec.Right)) and
- (Max(Top, rec.Top) < Min(Bottom, rec.Bottom));
- end;
- //------------------------------------------------------------------------------
- function TRectD.AsPath: TPathD;
- begin
- SetLength(Result, 4);
- Result[0] := PointD(Left, Top);
- Result[1] := PointD(Right, Top);
- Result[2] := PointD(Right, Bottom);
- Result[3] := PointD(Left, Bottom);
- end;
- //------------------------------------------------------------------------------
- // Miscellaneous Functions ...
- //------------------------------------------------------------------------------
- procedure CheckPrecisionRange(var precision: integer);
- begin
- if (precision < -8) or (precision > 8) then
- Raise EClipperLibException(rsClipper_PrecisonErr);
- end;
- //------------------------------------------------------------------------------
- procedure RaiseError(const msg: string); {$IFDEF INLINING} inline; {$ENDIF}
- begin
- raise EClipperLibException.Create(msg);
- end;
- //------------------------------------------------------------------------------
- function PointsEqual(const pt1, pt2: TPoint64): Boolean;
- begin
- Result := (pt1.X = pt2.X) and (pt1.Y = pt2.Y);
- end;
- //------------------------------------------------------------------------------
- function PointsNearEqual(const pt1, pt2: TPointD): Boolean;
- begin
- Result := (Abs(pt1.X - pt2.X) < Tolerance) and
- (Abs(pt1.Y - pt2.Y) < Tolerance);
- end;
- //------------------------------------------------------------------------------
- function PointsNearEqual(const pt1, pt2: TPointD; distanceSqrd: double): Boolean;
- begin
- Result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y) < distanceSqrd;
- end;
- //------------------------------------------------------------------------------
- function StripDuplicates(const path: TPath64; isClosedPath: Boolean): TPath64;
- var
- i,j, len: integer;
- begin
- len := length(path);
- SetLength(Result, len);
- if len = 0 then Exit;
- Result[0] := path[0];
- j := 0;
- for i := 1 to len -1 do
- if not PointsEqual(Result[j], path[i]) then
- begin
- inc(j);
- Result[j] := path[i];
- end;
- if isClosedPath and PointsEqual(Result[0], path[j]) then dec(j);
- SetLength(Result, j +1);
- end;
- //------------------------------------------------------------------------------
- function StripNearDuplicates(const path: TPathD;
- minLenSqrd: double; isClosedPath: Boolean): TPathD;
- var
- i,j, len: integer;
- begin
- len := length(path);
- SetLength(Result, len);
- if len = 0 then Exit;
- Result[0] := path[0];
- j := 0;
- for i := 1 to len -1 do
- if not PointsNearEqual(Result[j], path[i], minLenSqrd) then
- begin
- inc(j);
- Result[j] := path[i];
- end;
- if isClosedPath and
- PointsNearEqual(Result[j], Result[0], minLenSqrd) then dec(j);
- SetLength(Result, j +1);
- end;
- //------------------------------------------------------------------------------
- function ValueBetween(val, end1, end2: Int64): Boolean;
- begin
- // nb: accommodates axis aligned between where end1 == end2
- Result := ((val <> end1) = (val <> end2)) and
- ((val > end1) = (val < end2));
- end;
- //------------------------------------------------------------------------------
- function ValueEqualOrBetween(val, end1, end2: Int64): Boolean;
- begin
- Result := (val = end1) or (val = end2) or
- ((val > end1) = (val < end2));
- end;
- //------------------------------------------------------------------------------
- function ScaleRect(const rec: TRect64; scale: double): TRect64;
- begin
- Result.Left := Round(rec.Left * scale);
- Result.Top := Round(rec.Top * scale);
- Result.Right := Round(rec.Right * scale);
- Result.Bottom := Round(rec.Bottom * scale);
- end;
- //------------------------------------------------------------------------------
- function ScaleRect(const rec: TRectD; scale: double): TRectD;
- begin
- Result.Left := rec.Left * scale;
- Result.Top := rec.Top * scale;
- Result.Right := rec.Right * scale;
- Result.Bottom := rec.Bottom * scale;
- end;
- //------------------------------------------------------------------------------
- function ScalePoint(const pt: TPoint64; scale: double): TPointD;
- begin
- Result.X := pt.X * scale;
- Result.Y := pt.Y * scale;
- {$IFDEF USINGZ}
- Result.Z := pt.Z;
- {$ENDIF}
- end;
- //------------------------------------------------------------------------------
- function ScalePath(const path: TPath64; sx, sy: double): TPath64;
- var
- i,len: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- len := length(path);
- setlength(result, len);
- for i := 0 to len -1 do
- begin
- result[i].X := Round(path[i].X * sx);
- result[i].Y := Round(path[i].Y * sy);
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePath(const path: TPathD; sx, sy: double): TPath64;
- var
- i,j, len: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- len := length(path);
- setlength(result, len);
- if len = 0 then Exit;
- j := 1;
- result[0].X := Round(path[0].X * sx);
- result[0].Y := Round(path[0].Y * sy);
- for i := 1 to len -1 do
- begin
- result[j].X := Round(path[i].X * sx);
- result[j].Y := Round(path[i].Y * sy);
- if (result[j].X <> result[j-1].X) or
- (result[j].Y <> result[j-1].Y) then inc(j);
- end;
- setlength(result, j);
- end;
- //------------------------------------------------------------------------------
- function ScalePath(const path: TPath64; scale: double): TPath64;
- var
- i,j, len: integer;
- begin
- len := length(path);
- setlength(result, len);
- if len = 0 then Exit;
- j := 1;
- result[0].X := Round(path[0].X * scale);
- result[0].Y := Round(path[0].Y * scale);
- for i := 1 to len -1 do
- begin
- result[j].X := Round(path[i].X * scale);
- result[j].Y := Round(path[i].Y * scale);
- if (result[j].X <> result[j-1].X) or
- (result[j].Y <> result[j-1].Y) then inc(j);
- end;
- setlength(result, j);
- end;
- //------------------------------------------------------------------------------
- function ScalePath(const path: TPathD; scale: double): TPath64;
- var
- i,len: integer;
- begin
- len := length(path);
- setlength(result, len);
- for i := 0 to len -1 do
- begin
- result[i].X := Round(path[i].X * scale);
- result[i].Y := Round(path[i].Y * scale);
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePaths(const paths: TPaths64; sx, sy: double): TPaths64;
- var
- i,len: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- len := length(paths);
- setlength(result, len);
- for i := 0 to len -1 do
- result[i] := ScalePath(paths[i], sx, sy);
- end;
- //------------------------------------------------------------------------------
- function ScalePaths(const paths: TPathsD; sx, sy: double): TPaths64;
- var
- i,len: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- len := length(paths);
- setlength(result, len);
- for i := 0 to len -1 do
- result[i] := ScalePath(paths[i], sx, sy);
- end;
- //------------------------------------------------------------------------------
- function ScalePathD(const path: TPath64; sx, sy: double): TPathD;
- var
- i: integer;
- begin
- setlength(result, length(path));
- for i := 0 to high(path) do
- begin
- result[i].X := path[i].X * sx;
- result[i].Y := path[i].Y * sy;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathD(const path: TPathD; sx, sy: double): TPathD;
- var
- i: integer;
- begin
- setlength(result, length(path));
- for i := 0 to high(path) do
- begin
- result[i].X := path[i].X * sx;
- result[i].Y := path[i].Y * sy;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathD(const path: TPath64; scale: double): TPathD;
- var
- i: integer;
- begin
- setlength(result, length(path));
- for i := 0 to high(path) do
- begin
- result[i].X := path[i].X * scale;
- result[i].Y := path[i].Y * scale;
- {$IFDEF USINGZ}
- result[i].Z := path[i].Z;
- {$ENDIF}
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathD(const path: TPathD; scale: double): TPathD;
- var
- i: integer;
- begin
- setlength(result, length(path));
- for i := 0 to high(path) do
- begin
- result[i].X := path[i].X * scale;
- result[i].Y := path[i].Y * scale;
- {$IFDEF USINGZ}
- result[i].Z := path[i].Z;
- {$ENDIF}
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathsD(const paths: TPaths64; sx, sy: double): TPathsD;
- var
- i,j: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := (paths[i][j].X * sx);
- result[i][j].Y := (paths[i][j].Y * sy);
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathsD(const paths: TPathsD; sx, sy: double): TPathsD;
- var
- i,j: integer;
- begin
- if sx = 0 then sx := 1;
- if sy = 0 then sy := 1;
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := paths[i][j].X * sx;
- result[i][j].Y := paths[i][j].Y * sy;
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePaths(const paths: TPaths64; scale: double): TPaths64;
- var
- i,j: integer;
- begin
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := Round(paths[i][j].X * scale);
- result[i][j].Y := Round(paths[i][j].Y * scale);
- {$IFDEF USINGZ}
- result[i][j].Z := paths[i][j].Z;
- {$ENDIF}
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePaths(const paths: TPathsD; scale: double): TPaths64;
- var
- i,j: integer;
- begin
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := Round(paths[i][j].X * scale);
- result[i][j].Y := Round(paths[i][j].Y * scale);
- {$IFDEF USINGZ}
- result[i][j].Z := paths[i][j].Z;
- {$ENDIF}
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathsD(const paths: TPaths64; scale: double): TPathsD; overload;
- var
- i,j: integer;
- begin
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := paths[i][j].X * scale;
- result[i][j].Y := paths[i][j].Y * scale;
- {$IFDEF USINGZ}
- result[i][j].Z := paths[i][j].Z;
- {$ENDIF}
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function ScalePathsD(const paths: TPathsD; scale: double): TPathsD; overload;
- var
- i,j: integer;
- begin
- setlength(result, length(paths));
- for i := 0 to high(paths) do
- begin
- setlength(result[i], length(paths[i]));
- for j := 0 to high(paths[i]) do
- begin
- result[i][j].X := paths[i][j].X * scale;
- result[i][j].Y := paths[i][j].Y * scale;
- {$IFDEF USINGZ}
- result[i][j].Z := paths[i][j].Z;
- {$ENDIF}
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function Path64(const pathD: TPathD): TPath64;
- var
- i, len: integer;
- begin
- len := Length(pathD);
- setLength(Result, len);
- for i := 0 to len -1 do
- begin
- Result[i].X := Round(pathD[i].X);
- Result[i].Y := Round(pathD[i].Y);
- end;
- end;
- //------------------------------------------------------------------------------
- function PathD(const path: TPath64): TPathD;
- var
- i, len: integer;
- begin
- len := Length(path);
- setLength(Result, len);
- for i := 0 to len -1 do
- begin
- Result[i].X := path[i].X;
- Result[i].Y := path[i].Y;
- end;
- end;
- //------------------------------------------------------------------------------
- function Paths64(const pathsD: TPathsD): TPaths64;
- var
- i, len: integer;
- begin
- len := Length(pathsD);
- setLength(Result, len);
- for i := 0 to len -1 do
- Result[i] := Path64(pathsD[i]);
- end;
- //------------------------------------------------------------------------------
- function PathsD(const paths: TPaths64): TPathsD;
- var
- i, len: integer;
- begin
- len := Length(paths);
- setLength(Result, len);
- for i := 0 to len -1 do
- Result[i] := PathD(paths[i]);
- end;
- //------------------------------------------------------------------------------
- function ReversePath(const path: TPath64): TPath64;
- var
- i, highI: Integer;
- begin
- highI := high(path);
- SetLength(Result, highI +1);
- for i := 0 to highI do
- Result[i] := path[highI - i];
- end;
- //------------------------------------------------------------------------------
- function ReversePath(const path: TPathD): TPathD;
- var
- i, highI: Integer;
- begin
- highI := high(path);
- SetLength(Result, highI +1);
- for i := 0 to highI do
- Result[i] := path[highI - i];
- end;
- //------------------------------------------------------------------------------
- function ReversePaths(const paths: TPaths64): TPaths64;
- var
- i, j, highJ: Integer;
- begin
- i := length(paths);
- SetLength(Result, i);
- for i := 0 to i -1 do
- begin
- highJ := high(paths[i]);
- SetLength(Result[i], highJ+1);
- for j := 0 to highJ do
- Result[i][j] := paths[i][highJ - j];
- end;
- end;
- //------------------------------------------------------------------------------
- function ReversePaths(const paths: TPathsD): TPathsD;
- var
- i, j, highJ: Integer;
- begin
- i := length(paths);
- SetLength(Result, i);
- for i := 0 to i -1 do
- begin
- highJ := high(paths[i]);
- SetLength(Result[i], highJ+1);
- for j := 0 to highJ do
- Result[i][j] := paths[i][highJ - j];
- end;
- end;
- //------------------------------------------------------------------------------
- procedure AppendPoint(var path: TPath64; const pt: TPoint64);
- var
- len: Integer;
- begin
- len := length(path);
- SetLength(path, len +1);
- path[len] := pt;
- end;
- //------------------------------------------------------------------------------
- function AppendPoints(const path, extra: TPath64): TPath64;
- var
- len1, len2: Integer;
- begin
- len1 := length(path);
- len2 := length(extra);
- SetLength(Result, len1 + len2);
- if len1 > 0 then
- Move(path[0], Result[0], len1 * sizeOf(TPoint64));
- if len2 > 0 then
- Move(extra[0], Result[len1], len2 * sizeOf(TPoint64));
- end;
- //------------------------------------------------------------------------------
- procedure AppendPoint(var path: TPathD; const pt: TPointD);
- var
- len: Integer;
- begin
- len := length(path);
- SetLength(path, len +1);
- path[len] := pt;
- end;
- //------------------------------------------------------------------------------
- procedure AppendPath(var paths: TPaths64; const extra: TPath64);
- var
- len: Integer;
- begin
- len := length(paths);
- SetLength(paths, len +1);
- paths[len] := extra;
- end;
- //------------------------------------------------------------------------------
- procedure AppendPath(var paths: TPathsD; const extra: TPathD);
- var
- len: Integer;
- begin
- len := length(paths);
- SetLength(paths, len +1);
- paths[len] := extra;
- end;
- //------------------------------------------------------------------------------
- procedure AppendPaths(var paths: TPaths64; const extra: TPaths64);
- var
- i, len1, len2: Integer;
- begin
- len1 := length(paths);
- len2 := length(extra);
- SetLength(paths, len1 + len2);
- for i := 0 to len2 -1 do
- paths[len1 + i] := extra[i];
- end;
- //------------------------------------------------------------------------------
- procedure AppendPaths(var paths: TPathsD; const extra: TPathsD);
- var
- i, len1, len2: Integer;
- begin
- len1 := length(paths);
- len2 := length(extra);
- SetLength(paths, len1 + len2);
- for i := 0 to len2 -1 do
- paths[len1 + i] := extra[i];
- end;
- //------------------------------------------------------------------------------
- function ArrayOfPathsToPaths(const ap: TArrayOfPaths): TPaths64;
- var
- i,j,k, len, cnt: integer;
- begin
- cnt := 0;
- len := length(ap);
- for i := 0 to len -1 do
- inc(cnt, length(ap[i]));
- k := 0;
- setlength(result, cnt);
- for i := 0 to len -1 do
- for j := 0 to length(ap[i]) -1 do
- begin
- result[k] := ap[i][j];
- inc(k);
- end;
- end;
- //------------------------------------------------------------------------------
- {$IFDEF USINGZ}
- function Point64(const X, Y: Int64; Z: Int64): TPoint64;
- begin
- Result.X := X;
- Result.Y := Y;
- Result.Z := Z;
- end;
- //------------------------------------------------------------------------------
- function Point64(const X, Y: Double; Z: Int64): TPoint64;
- begin
- Result.X := Round(X);
- Result.Y := Round(Y);
- Result.Z := Z;
- end;
- //------------------------------------------------------------------------------
- function PointD(const X, Y: Double; Z: Int64): TPointD;
- begin
- Result.X := X;
- Result.Y := Y;
- Result.Z := Z;
- end;
- //------------------------------------------------------------------------------
- function Point64(const pt: TPointD): TPoint64;
- begin
- Result.X := Round(pt.X);
- Result.Y := Round(pt.Y);
- Result.Z := pt.Z;
- end;
- //------------------------------------------------------------------------------
- function PointD(const pt: TPoint64): TPointD;
- begin
- Result.X := pt.X;
- Result.Y := pt.Y;
- Result.Z := pt.Z;
- end;
- //------------------------------------------------------------------------------
- {$ELSE}
- function Point64(const X, Y: Int64): TPoint64;
- begin
- Result.X := X;
- Result.Y := Y;
- end;
- //------------------------------------------------------------------------------
- function Point64(const X, Y: Double): TPoint64;
- begin
- Result.X := Round(X);
- Result.Y := Round(Y);
- end;
- //------------------------------------------------------------------------------
- function PointD(const X, Y: Double): TPointD;
- begin
- Result.X := X;
- Result.Y := Y;
- end;
- //------------------------------------------------------------------------------
- function Point64(const pt: TPointD): TPoint64;
- begin
- Result.X := Round(pt.X);
- Result.Y := Round(pt.Y);
- end;
- //------------------------------------------------------------------------------
- function PointD(const pt: TPoint64): TPointD;
- begin
- Result.X := pt.X;
- Result.Y := pt.Y;
- end;
- //------------------------------------------------------------------------------
- {$ENDIF}
- function Negate(const pt: TPointD): TPointD;
- begin
- Result.X := -pt.X;
- Result.Y := -pt.Y;
- end;
- //------------------------------------------------------------------------------
- function Rect64(const left, top, right, bottom: Int64): TRect64;
- begin
- Result.Left := left;
- Result.Top := top;
- Result.Right := right;
- Result.Bottom := bottom;
- end;
- //------------------------------------------------------------------------------
- function Rect64(const recD: TRectD): TRect64;
- begin
- Result.Left := Floor(recD.left);
- Result.Top := Floor(recD.top);
- Result.Right := Ceil(recD.right);
- Result.Bottom := Ceil(recD.bottom);
- end;
- //------------------------------------------------------------------------------
- function RectD(const left, top, right, bottom: double): TRectD;
- begin
- Result.Left := left;
- Result.Top := top;
- Result.Right := right;
- Result.Bottom := bottom;
- end;
- //------------------------------------------------------------------------------
- function RectD(const rec64: TRect64): TRectD; overload;
- begin
- Result.Left := rec64.left;
- Result.Top := rec64.top;
- Result.Right := rec64.right;
- Result.Bottom := rec64.bottom;
- end;
- //------------------------------------------------------------------------------
- function GetBounds(const paths: TArrayOfPaths): TRect64; overload;
- var
- i,j,k: Integer;
- p: PPoint64;
- begin
- Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64);
- for i := 0 to High(paths) do
- for j := 0 to High(paths[i]) do
- if Assigned(paths[i][j]) then
- begin
- p := @paths[i][j][0];
- for k := 0 to High(paths[i][j]) do
- begin
- if p.X < Result.Left then Result.Left := p.X;
- if p.X > Result.Right then Result.Right := p.X;
- if p.Y < Result.Top then Result.Top := p.Y;
- if p.Y > Result.Bottom then Result.Bottom := p.Y;
- inc(p);
- end;
- end;
- if Result.Left > Result.Right then Result := NullRect64;
- end;
- //------------------------------------------------------------------------------
- function GetBounds(const paths: TPaths64): TRect64;
- var
- i,j: Integer;
- p: PPoint64;
- begin
- Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64);
- for i := 0 to High(paths) do
- if Assigned(paths[i]) then
- begin
- p := @paths[i][0];
- for j := 0 to High(paths[i]) do
- begin
- if p.X < Result.Left then Result.Left := p.X;
- if p.X > Result.Right then Result.Right := p.X;
- if p.Y < Result.Top then Result.Top := p.Y;
- if p.Y > Result.Bottom then Result.Bottom := p.Y;
- inc(p);
- end;
- end;
- if Result.Left > Result.Right then Result := NullRect64;
- end;
- //------------------------------------------------------------------------------
- function GetBounds(const paths: TPathsD): TRectD;
- var
- i,j: Integer;
- p: PPointD;
- begin
- Result := RectD(MaxDouble, MaxDouble, -MaxDouble, -MaxDouble);
- for i := 0 to High(paths) do
- if Assigned(paths[i]) then
- begin
- p := @paths[i][0];
- for j := 0 to High(paths[i]) do
- begin
- if p.X < Result.Left then Result.Left := p.X;
- if p.X > Result.Right then Result.Right := p.X;
- if p.Y < Result.Top then Result.Top := p.Y;
- if p.Y > Result.Bottom then Result.Bottom := p.Y;
- inc(p);
- end;
- end;
- if Result.Left >= Result.Right then Result := nullRectD;
- end;
- //------------------------------------------------------------------------------
- function GetBounds(const path: TPath64): TRect64;
- var
- i, len: Integer;
- p: PPoint64;
- begin
- len := Length(path);
- if len = 0 then
- begin
- Result := NullRect64;
- Exit;
- end;
- Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64);
- p := @path[0];
- for i := 0 to High(path) do
- begin
- if p.X < Result.Left then Result.Left := p.X;
- if p.X > Result.Right then Result.Right := p.X;
- if p.Y < Result.Top then Result.Top := p.Y;
- if p.Y > Result.Bottom then Result.Bottom := p.Y;
- inc(p);
- end;
- end;
- //------------------------------------------------------------------------------
- function GetBounds(const path: TPathD): TRectD;
- var
- i, len: Integer;
- p: PPointD;
- begin
- len := Length(path);
- if len = 0 then
- begin
- Result := NullRectD;
- Exit;
- end;
- Result := RectD(infinity, infinity, -infinity, -infinity);
- p := @path[0];
- for i := 0 to High(path) do
- begin
- if p.X < Result.Left then Result.Left := p.X;
- if p.X > Result.Right then Result.Right := p.X;
- if p.Y < Result.Top then Result.Top := p.Y;
- if p.Y > Result.Bottom then Result.Bottom := p.Y;
- inc(p);
- end;
- end;
- //------------------------------------------------------------------------------
- function TranslatePoint(const pt: TPoint64; dx, dy: Int64): TPoint64;
- begin
- Result.X := pt.X + dx;
- Result.Y := pt.Y + dy;
- end;
- //------------------------------------------------------------------------------
- function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD;
- begin
- Result.X := pt.X + dx;
- Result.Y := pt.Y + dy;
- end;
- //------------------------------------------------------------------------------
- procedure InflateRect(var rec: TRect64; dx, dy: Int64);
- begin
- dec(rec.Left, dx);
- inc(rec.Right, dx);
- dec(rec.Top, dy);
- inc(rec.Bottom, dy);
- end;
- //------------------------------------------------------------------------------
- procedure InflateRect(var rec: TRectD; dx, dy: double);
- begin
- rec.Left := rec.Left - dx;
- rec.Right := rec.Right + dx;
- rec.Top := rec.Top - dy;
- rec.Bottom := rec.Bottom + dy;
- end;
- //------------------------------------------------------------------------------
- procedure RotatePt(var pt: TPointD; const center: TPointD; sinA, cosA: double);
- var
- tmpX, tmpY: double;
- begin
- tmpX := pt.X-center.X;
- tmpY := pt.Y-center.Y;
- pt.X := tmpX * cosA - tmpY * sinA + center.X;
- pt.Y := tmpX * sinA + tmpY * cosA + center.Y;
- end;
- //------------------------------------------------------------------------------
- procedure RotatePath(var path: TPathD; const center: TPointD; sinA, cosA: double);
- var
- i: integer;
- begin
- for i := 0 to High(path) do
- RotatePt(path[i], center, sinA, cosA);
- end;
- //------------------------------------------------------------------------------
- function RotateRect(const rec: TRectD; angleRad: double): TRectD;
- var
- i: integer;
- sinA, cosA: double;
- cp: TPointD;
- pts: TPathD;
- begin
- setLength(pts, 4);
- sinA := Sin(-angleRad);
- cosA := cos(-angleRad);
- cp.X := (rec.Right + rec.Left) / 2;
- cp.Y := (rec.Bottom + rec.Top) / 2;
- pts[0] := PointD(rec.Left, rec.Top);
- pts[1] := PointD(rec.Right, rec.Top);
- pts[2] := PointD(rec.Left, rec.Bottom);
- pts[3] := PointD(rec.Right, rec.Bottom);
- for i := 0 to 3 do RotatePt(pts[i], cp, sinA, cosA);
- result.Left := pts[0].X;
- result.Right := result.Left;
- result.Top := pts[0].Y;
- result.Bottom := result.Top;
- for i := 1 to 3 do
- begin
- if pts[i].X < result.Left then result.Left := pts[i].X;
- if pts[i].Y < result.Top then result.Top := pts[i].Y;
- if pts[i].X > result.Right then result.Right := pts[i].X;
- if pts[i].Y > result.Bottom then result.Bottom := pts[i].Y;
- end;
- end;
- //------------------------------------------------------------------------------
- function RotateRect(const rec: TRect64; angleRad: double): TRect64;
- var
- recD: TRectD;
- begin
- recD := RectD(rec.Left, rec.Top, rec.Right, rec.Bottom);
- recD := RotateRect(recD, angleRad);
- result.Left := Floor(recD.Left);
- result.Top := Floor(recD.Top);
- result.Right := Ceil(recD.Right);
- result.Bottom := Ceil(recD.Bottom);
- end;
- //------------------------------------------------------------------------------
- procedure OffsetRect(var rec: TRect64; dx, dy: Int64);
- begin
- inc(rec.Left, dx); inc(rec.Top, dy);
- inc(rec.Right, dx); inc(rec.Bottom, dy);
- end;
- //------------------------------------------------------------------------------
- procedure OffsetRect(var rec: TRectD; dx, dy: double);
- begin
- rec.Left := rec.Left + dx;
- rec.Right := rec.Right + dx;
- rec.Top := rec.Top + dy;
- rec.Bottom := rec.Bottom + dy;
- end;
- //------------------------------------------------------------------------------
- function UnionRect(const rec, rec2: TRect64): TRect64;
- begin
- // nb: don't use rec.IsEmpty as this will
- // reject open axis-aligned flat paths
- if (rec.Width <= 0) and (rec.Height <= 0) then result := rec2
- else if (rec2.Width <= 0) and (rec2.Height <= 0) then result := rec
- else
- begin
- result.Left := min(rec.Left, rec2.Left);
- result.Right := max(rec.Right, rec2.Right);
- result.Top := min(rec.Top, rec2.Top);
- result.Bottom := max(rec.Bottom, rec2.Bottom);
- end;
- end;
- //------------------------------------------------------------------------------
- function UnionRect(const rec, rec2: TRectD): TRectD;
- begin
- // nb: don't use rec.IsEmpty as this will
- // reject open axis-aligned flat paths
- if (rec.Width <= 0) and (rec.Height <= 0) then result := rec2
- else if (rec2.Width <= 0) and (rec2.Height <= 0) then result := rec
- else
- begin
- result.Left := min(rec.Left, rec2.Left);
- result.Right := max(rec.Right, rec2.Right);
- result.Top := min(rec.Top, rec2.Top);
- result.Bottom := max(rec.Bottom, rec2.Bottom);
- end;
- end;
- //------------------------------------------------------------------------------
- function Area(const path: TPath64): Double;
- var
- i, highI: Integer;
- d: double;
- p1,p2: PPoint64;
- begin
- // shoelace formula
- Result := 0.0;
- highI := High(path);
- if highI < 2 then Exit;
- p1 := @path[highI];
- p2 := @path[0];
- for i := 0 to highI do
- begin
- d := (p1.Y + p2.Y); // needed for Delphi7
- Result := Result + d * (p1.X - p2.X);
- p1 := p2; inc(p2);
- end;
- Result := Result * 0.5;
- end;
- //------------------------------------------------------------------------------
- function Area(const paths: TPaths64): Double;
- var
- i: integer;
- begin
- Result := 0;
- for i := 0 to High(paths) do
- Result := Result + Area(paths[i]);
- end;
- //------------------------------------------------------------------------------
- function Area(const path: TPathD): Double;
- var
- i, highI: Integer;
- p1,p2: PPointD;
- begin
- // https://en.wikipedia.org/wiki/Shoelace_formula
- Result := 0.0;
- highI := High(path);
- if highI < 2 then Exit;
- p1 := @path[highI];
- p2 := @path[0];
- for i := 0 to highI do
- begin
- Result := Result + (p1.Y + p2.Y) * (p1.X - p2.X);
- p1 := p2; inc(p2);
- end;
- Result := Result * 0.5;
- end;
- //------------------------------------------------------------------------------
- function Area(const paths: TPathsD): Double;
- var
- i: integer;
- begin
- Result := 0;
- for i := 0 to High(paths) do
- Result := Result + Area(paths[i]);
- end;
- //------------------------------------------------------------------------------
- function IsPositive(const path: TPath64): Boolean;
- begin
- Result := (Area(path) >= 0);
- end;
- //------------------------------------------------------------------------------
- function IsPositive(const path: TPathD): Boolean;
- begin
- Result := (Area(path) >= 0);
- end;
- //------------------------------------------------------------------------------
- function CrossProduct(const pt1, pt2, pt3: TPoint64): double;
- begin
- result := CrossProduct(
- pt2.X - pt1.X, pt2.Y - pt1.Y,
- pt3.X - pt2.X, pt3.Y - pt2.Y);
- end;
- //------------------------------------------------------------------------------
- function CrossProduct(const pt1, pt2, pt3: TPointD): double;
- begin
- result := CrossProduct(
- pt2.X - pt1.X, pt2.Y - pt1.Y,
- pt3.X - pt2.X, pt3.Y - pt2.Y);
- end;
- //------------------------------------------------------------------------------
- function CrossProduct(const vec1, vec2: TPointD): double;
- begin
- result := (vec1.X * vec2.Y - vec1.Y * vec2.X);
- end;
- //------------------------------------------------------------------------------
- function CrossProduct(vec1x, vec1y, vec2x, vec2y: double): double;
- begin
- result := (vec1x * vec2y - vec1y * vec2x);
- end;
- //------------------------------------------------------------------------------
- function DotProduct(const pt1, pt2, pt3: TPoint64): double;
- var
- x1,x2,y1,y2: double; // avoids potential int overflow
- begin
- x1 := pt2.X - pt1.X;
- y1 := pt2.Y - pt1.Y;
- x2 := pt3.X - pt2.X;
- y2 := pt3.Y - pt2.Y;
- result := (x1 * x2 + y1 * y2);
- end;
- //------------------------------------------------------------------------------
- function SqrInt64(val: Int64): double; {$IFDEF INLINING} inline; {$ENDIF}
- begin
- Result := val; // force conversion
- Result := Result * Result;
- end;
- //------------------------------------------------------------------------------
- function DistanceSqr(const pt1, pt2: TPoint64): double;
- begin
- Result := SqrInt64(pt1.X - pt2.X) + SqrInt64(pt1.Y - pt2.Y);
- end;
- //------------------------------------------------------------------------------
- function DistanceSqr(const pt1, pt2: TPointD): double;
- begin
- Result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y);
- end;
- //------------------------------------------------------------------------------
- function DistanceFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double;
- var
- a,b,c: double;
- begin
- // perpendicular distance of point (x0,y0) = (a*x0 + b*y0 + C)/Sqrt(a*a + b*b)
- // where ax + by +c = 0 is the equation of the line
- // see https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
- a := (linePt1.Y - linePt2.Y);
- b := (linePt2.X - linePt1.X);
- c := a * linePt1.X + b * linePt1.Y;
- c := a * pt.x + b * pt.y - c;
- Result := (c * c) / (a * a + b * b);
- end;
- //---------------------------------------------------------------------------
- function DistanceFromLineSqrd(const pt, linePt1, linePt2: TPointD): double;
- var
- a,b,c: double;
- begin
- a := (linePt1.Y - linePt2.Y);
- b := (linePt2.X - linePt1.X);
- c := a * linePt1.X + b * linePt1.Y;
- c := a * pt.x + b * pt.y - c;
- Result := (c * c) / (a * a + b * b);
- end;
- //---------------------------------------------------------------------------
- function CleanPath(const path: TPath64): TPath64;
- var
- i,j, len: integer;
- prev: TPoint64;
- begin
- Result := nil;
- len := Length(path);
- while (len > 2) and
- (CrossProduct(path[len-2], path[len-1], path[0]) = 0) do dec(len);
- SetLength(Result, len);
- if (len < 2) then Exit;
- prev := path[len -1];
- j := 0;
- for i := 0 to len -2 do
- begin
- if CrossProduct(prev, path[i], path[i+1]) = 0 then Continue;
- Result[j] := path[i];
- inc(j);
- prev := path[i];
- end;
- Result[j] := path[len -1];
- SetLength(Result, j+1);
- end;
- //------------------------------------------------------------------------------
- function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64;
- inclusive: Boolean): boolean;
- var
- res1, res2, res3, res4: double;
- begin
- if inclusive then //result can include segments that only touch
- begin
- Result := false;
- res1 := CrossProduct(s1a, s2a, s2b);
- res2 := CrossProduct(s1b, s2a, s2b);
- if (res1 * res2 > 0) then Exit;
- res3 := CrossProduct(s2a, s1a, s1b);
- res4 := CrossProduct(s2b, s1a, s1b);
- if (res3 * res4 > 0) then Exit;
- Result := (res1 <> 0) or (res2 <> 0) or
- (res3 <> 0) or (res4 <> 0); // ensures not collinear
- end else
- result := (CrossProduct(s1a, s2a, s2b) * CrossProduct(s1b, s2a, s2b) < 0) and
- (CrossProduct(s2a, s1a, s1b) * CrossProduct(s2b, s1a, s1b) < 0);
- end;
- //------------------------------------------------------------------------------
- function GetClosestLineEnd(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPoint64;
- var
- i, idx: integer;
- d: array[0..3] of double;
- begin
- d[0] := DistanceSqr(ln1a, ln2a);
- if (d[0] = 0) then begin Result := ln1a; Exit; end;
- d[1] := DistanceSqr(ln1b, ln2b);
- if (d[1] = 0) then begin Result := ln1b; Exit; end;
- d[2] := DistanceSqr(ln1a, ln2b);
- if (d[2] = 0) then begin Result := ln1a; Exit; end;
- d[3] := DistanceSqr(ln1b, ln2a);
- if (d[3] = 0) then begin Result := ln1b; Exit; end;
- idx := 0;
- for i := 1 to 3 do
- if (d[i] < d[idx]) then idx := i;
- case idx of
- 1,2: Result := ln1b;
- else Result := ln1a;
- end;
- end;
- //------------------------------------------------------------------------------
- function GetIntersectPoint64(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPoint64;
- var
- x, m1,b1,m2,b2: double;
- begin
- // see http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
- if (ln1B.X = ln1A.X) then
- begin
- if (ln2B.X = ln2A.X) then exit; // parallel lines
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- Result.X := ln1A.X;
- Result.Y := Round(m2*ln1A.X + b2);
- end
- else if (ln2B.X = ln2A.X) then
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- Result.X := ln2A.X;
- Result.Y := Round(m1*ln2A.X + b1);
- end else
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- if Abs(m1 - m2) < 1.0E-5 then
- begin
- Result := GetClosestLineEnd(ln1a, ln1b, ln2a, ln2b);
- end else
- begin
- x := (b2 - b1)/(m1 - m2);
- Result.X := Round(x);
- Result.Y := Round(m1 * x + b1);
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function GetIntersectPointD(const ln1a, ln1b, ln2a, ln2b: TPoint64): TPointD;
- var
- m1,b1,m2,b2: double;
- begin
- // see http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
- if (ln1B.X = ln1A.X) then
- begin
- if (ln2B.X = ln2A.X) then exit; // parallel lines
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- Result.X := ln1A.X;
- Result.Y := m2*ln1A.X + b2;
- end
- else if (ln2B.X = ln2A.X) then
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- Result.X := ln2A.X;
- Result.Y := m1*ln2A.X + b1;
- end else
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- if Abs(m1 - m2) < 1.0E-5 then
- begin
- Result := PointD(GetClosestLineEnd(ln1a, ln1b, ln2a, ln2b));
- end else
- begin
- Result.X := (b2 - b1)/(m1 - m2);
- Result.Y := m1 * Result.X + b1;
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function GetClosestLineEnd(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD;
- var
- i, idx: integer;
- d: array[0..3] of double;
- begin
- d[0] := DistanceSqr(ln1a, ln2a);
- if (d[0] = 0) then begin Result := ln1a; Exit; end;
- d[1] := DistanceSqr(ln1b, ln2b);
- if (d[1] = 0) then begin Result := ln1b; Exit; end;
- d[2] := DistanceSqr(ln1a, ln2b);
- if (d[2] = 0) then begin Result := ln1a; Exit; end;
- d[3] := DistanceSqr(ln1b, ln2a);
- if (d[3] = 0) then begin Result := ln1b; Exit; end;
- idx := 0;
- for i := 1 to 3 do
- if (d[i] < d[idx]) then idx := i;
- case idx of
- 1,2: Result := ln1b;
- else Result := ln1a;
- end;
- end;
- //------------------------------------------------------------------------------
- function GetIntersectPointD(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD; overload;
- var
- m1,b1,m2,b2: double;
- begin
- // see http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
- if (ln1B.X = ln1A.X) then
- begin
- if (ln2B.X = ln2A.X) then exit; // parallel lines
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- Result.X := ln1A.X;
- Result.Y := m2*ln1A.X + b2;
- end
- else if (ln2B.X = ln2A.X) then
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- Result.X := ln2A.X;
- Result.Y := m1*ln2A.X + b1;
- end else
- begin
- m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
- b1 := ln1A.Y - m1 * ln1A.X;
- m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
- b2 := ln2A.Y - m2 * ln2A.X;
- if Abs(m1 - m2) < 1.0E-5 then
- begin
- Result := GetClosestLineEnd(ln1a, ln1b, ln2a, ln2b);
- end else
- begin
- Result.X := (b2 - b1)/(m1 - m2);
- Result.Y := m1 * Result.X + b1;
- end;
- end;
- end;
- //------------------------------------------------------------------------------
- function PointInPolygon(const pt: TPoint64;
- const polygon: TPath64): TPointInPolygonResult;
- var
- i, len, val: Integer;
- isAbove: Boolean;
- d: Double; // used to avoid integer overflow
- curr, prev, first, stop: PPoint64;
- begin
- result := pipOutside;
- len := Length(polygon);
- if len < 3 then Exit;
- i := len -1;
- first := @polygon[0];
- while (i >= 0) and (polygon[i].Y = pt.Y) do dec(i);
- if i < 0 then Exit;
- isAbove := polygon[i].Y < pt.Y;
- Result := pipOn;
- stop := @polygon[len -1];
- inc(stop); // stop is just past the last point
- curr := first;
- val := 0;
- while (curr <> stop) do
- begin
- if isAbove then
- begin
- while (curr <> stop) and (curr.Y < pt.Y) do inc(curr);
- if (curr = stop) then break;
- end else
- begin
- while (curr <> stop) and (curr.Y > pt.Y) do inc(curr);
- if (curr = stop) then break;
- end;
- if curr = first then
- prev := stop else
- prev := curr;
- dec(prev);
- if (curr.Y = pt.Y) then
- begin
- if (curr.X = pt.X) or ((curr.Y = prev.Y) and
- ((pt.X < prev.X) <> (pt.X < curr.X))) then Exit;
- inc(curr);
- Continue;
- end;
- if (pt.X < curr.X) and (pt.X < prev.X) then
- // we're only interested in edges crossing on the left
- else if((pt.X > prev.X) and (pt.X > curr.X)) then
- val := 1 - val // toggle val
- else
- begin
- d := CrossProduct(prev^, curr^, pt);
- if d = 0 then Exit; // ie point on path
- if (d < 0) = isAbove then val := 1 - val;
- end;
- isAbove := not isAbove;
- inc(curr);
- end;
- if val = 0 then
- result := pipOutside else
- result := pipInside;
- end;
- //------------------------------------------------------------------------------
- procedure GetSinCos(angle: double; out sinA, cosA: double);
- {$IFDEF INLINE} inline; {$ENDIF}
- {$IFNDEF FPC}
- var s, c: extended;
- {$ENDIF}
- begin
- {$IFDEF FPC}
- Math.SinCos(angle, sinA, cosA);
- {$ELSE}
- Math.SinCos(angle, s, c);
- sinA := s; cosA := c;
- {$ENDIF}
- end;
- //------------------------------------------------------------------------------
- function Ellipse(const rec: TRect64; steps: integer): TPath64;
- begin
- Result := Path64(Ellipse(RectD(rec), steps));
- end;
- //------------------------------------------------------------------------------
- function Ellipse(const rec: TRectD; steps: integer): TPathD;
- var
- i: Integer;
- sinA, cosA: double;
- centre, radius, delta: TPointD;
- begin
- result := nil;
- if rec.IsEmpty then Exit;
- with rec do
- begin
- centre := rec.MidPoint;
- radius := PointD(Width * 0.5, Height * 0.5);
- end;
- if (steps < 3) then
- steps := Ceil(PI * sqrt(rec.width + rec.height));
- GetSinCos(2 * Pi / Steps, sinA, cosA);
- delta.x := cosA; delta.y := sinA;
- SetLength(Result, Steps);
- Result[0] := PointD(centre.X + radius.X, centre.Y);
- for i := 1 to steps -1 do
- begin
- Result[i] := PointD(centre.X + radius.X * delta.x,
- centre.Y + radius.y * delta.y);
- delta := PointD(delta.X * cosA - delta.Y * sinA,
- delta.Y * cosA + delta.X * sinA);
- end; // rotates clockwise
- end;
- //------------------------------------------------------------------------------
- function PerpendicDistFromLineSqrd(const pt, line1, line2: TPoint64): double; overload;
- var
- a,b,c,d: double;
- begin
- a := pt.X - line1.X;
- b := pt.Y - line1.Y;
- c := line2.X - line1.X;
- d := line2.Y - line1.Y;
- if (c = 0) and (d = 0) then
- result := 0 else
- result := Sqr(a * d - c * b) / (c * c + d * d);
- end;
- //------------------------------------------------------------------------------
- procedure RDP(const path: TPath64; startIdx, endIdx: integer;
- epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload;
- var
- i, idx: integer;
- d, maxD: double;
- begin
- idx := 0;
- maxD := 0;
- while (endIdx > startIdx) and
- PointsEqual(path[startIdx], path[endIdx]) do
- begin
- boolArray[endIdx] := false;
- dec(endIdx);
- end;
- for i := startIdx +1 to endIdx -1 do
- begin
- // PerpendicDistFromLineSqrd - avoids expensive Sqrt()
- d := PerpendicDistFromLineSqrd(path[i], path[startIdx], path[endIdx]);
- if d <= maxD then Continue;
- maxD := d;
- idx := i;
- end;
- if maxD < epsilonSqrd then Exit;
- boolArray[idx] := true;
- if idx > startIdx + 1 then RDP(path, startIdx, idx, epsilonSqrd, boolArray);
- if endIdx > idx + 1 then RDP(path, idx, endIdx, epsilonSqrd, boolArray);
- end;
- //------------------------------------------------------------------------------
- function PerpendicDistFromLineSqrd(const pt, line1, line2: TPointD): double; overload;
- var
- a,b,c,d: double;
- begin
- a := pt.X - line1.X;
- b := pt.Y - line1.Y;
- c := line2.X - line1.X;
- d := line2.Y - line1.Y;
- if (c = 0) and (d = 0) then
- result := 0 else
- result := Sqr(a * d - c * b) / (c * c + d * d);
- end;
- //------------------------------------------------------------------------------
- procedure RDP(const path: TPathD; startIdx, endIdx: integer;
- epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload;
- var
- i, idx: integer;
- d, maxD: double;
- begin
- idx := 0;
- maxD := 0;
- while (endIdx > startIdx) and
- PointsNearEqual(path[startIdx], path[endIdx]) do
- begin
- boolArray[endIdx] := false;
- dec(endIdx);
- end;
- for i := startIdx +1 to endIdx -1 do
- begin
- // PerpendicDistFromLineSqrd - avoids expensive Sqrt()
- d := PerpendicDistFromLineSqrd(path[i], path[startIdx], path[endIdx]);
- if d <= maxD then Continue;
- maxD := d;
- idx := i;
- end;
- if maxD < epsilonSqrd then Exit;
- boolArray[idx] := true;
- if idx > startIdx + 1 then RDP(path, startIdx, idx, epsilonSqrd, boolArray);
- if endIdx > idx + 1 then RDP(path, idx, endIdx, epsilonSqrd, boolArray);
- end;
- //------------------------------------------------------------------------------
- function RamerDouglasPeucker(const path: TPath64; epsilon: double): TPath64;
- var
- i,j, len: integer;
- boolArray: TArrayOfBoolean;
- begin
- len := length(path);
- if len < 5 then
- begin
- result := Copy(path, 0, len);
- Exit;
- end;
- SetLength(boolArray, len); // already zero initialized
- boolArray[0] := true;
- boolArray[len -1] := true;
- RDP(path, 0, len -1, Sqr(epsilon), boolArray);
- j := 0;
- SetLength(Result, len);
- for i := 0 to len -1 do
- if boolArray[i] then
- begin
- Result[j] := path[i];
- inc(j);
- end;
- SetLength(Result, j);
- end;
- //------------------------------------------------------------------------------
- function RamerDouglasPeucker(const paths: TPaths64; epsilon: double): TPaths64;
- var
- i, len: integer;
- begin
- len := Length(paths);
- SetLength(Result, len);
- for i := 0 to len -1 do
- Result[i] := RamerDouglasPeucker(paths[i], epsilon);
- end;
- //------------------------------------------------------------------------------
- function RamerDouglasPeucker(const path: TPathD; epsilon: double): TPathD; overload;
- var
- i,j, len: integer;
- boolArray: TArrayOfBoolean;
- begin
- len := length(path);
- if len < 5 then
- begin
- result := Copy(path, 0, len);
- Exit;
- end;
- SetLength(boolArray, len); // already zero initialized
- boolArray[0] := true;
- boolArray[len -1] := true;
- RDP(path, 0, len -1, Sqr(epsilon), boolArray);
- j := 0;
- SetLength(Result, len);
- for i := 0 to len -1 do
- if boolArray[i] then
- begin
- Result[j] := path[i];
- inc(j);
- end;
- SetLength(Result, j);
- end;
- //------------------------------------------------------------------------------
- function RamerDouglasPeucker(const paths: TPathsD; epsilon: double): TPathsD; overload;
- var
- i, len: integer;
- begin
- len := Length(paths);
- SetLength(Result, len);
- for i := 0 to len -1 do
- Result[i] := RamerDouglasPeucker(paths[i], epsilon);
- end;
- //------------------------------------------------------------------------------
- end.
|