| 1 |
unit Clipper.Core; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Date : 22 November 2024 * |
| 6 |
* Website : http://www.angusj.com * |
| 7 |
* Copyright : Angus Johnson 2010-2024 * |
| 8 |
* Purpose : Core Clipper Library module * |
| 9 |
* Contains structures and functions used throughout the library * |
| 10 |
* License : http://www.boost.org/LICENSE_1_0.txt * |
| 11 |
*******************************************************************************) |
| 12 |
|
| 13 |
{$I Clipper.inc} |
| 14 |
|
| 15 |
interface |
| 16 |
|
| 17 |
uses |
| 18 |
SysUtils, Classes, Math; |
| 19 |
|
| 20 |
type |
| 21 |
{$IFDEF USINGZ} |
| 22 |
ZType = Int64; // or alternatively, ZType = double |
| 23 |
{$ENDIF} |
| 24 |
|
| 25 |
PPoint64 = ^TPoint64; |
| 26 |
TPoint64 = record |
| 27 |
X, Y: Int64; |
| 28 |
{$IFDEF USINGZ} |
| 29 |
Z: ZType; |
| 30 |
{$ENDIF} |
| 31 |
end; |
| 32 |
|
| 33 |
PPointD = ^TPointD; |
| 34 |
TPointD = record |
| 35 |
X, Y: double; |
| 36 |
{$IFDEF USINGZ} |
| 37 |
Z: ZType; |
| 38 |
{$ENDIF} |
| 39 |
end; |
| 40 |
|
| 41 |
// Path: a simple data structure representing a series of vertices, whether |
| 42 |
// open (poly-line) or closed (polygon). Paths may be simple or complex (self |
| 43 |
// intersecting). For simple polygons, consisting of a single non-intersecting |
| 44 |
// path, path orientation is unimportant. However, for complex polygons and |
| 45 |
// for overlapping polygons, various 'filling rules' define which regions will |
| 46 |
// be inside (filled) and which will be outside (unfilled). |
| 47 |
|
| 48 |
TPath64 = array of TPoint64; |
| 49 |
TPaths64 = array of TPath64; |
| 50 |
TArrayOfPaths = array of TPaths64; |
| 51 |
|
| 52 |
TPathD = array of TPointD; |
| 53 |
TPathsD = array of TPathD; |
| 54 |
TArrayOfPathsD = array of TPathsD; |
| 55 |
|
| 56 |
// The most commonly used filling rules for polygons are EvenOdd and NonZero. |
| 57 |
// https://en.wikipedia.org/wiki/Even-odd_rule |
| 58 |
// https://en.wikipedia.org/wiki/Nonzero-rule |
| 59 |
TFillRule = (frEvenOdd, frNonZero, frPositive, frNegative); |
| 60 |
|
| 61 |
TArrayOfBoolean = array of Boolean; |
| 62 |
TArrayOfInteger = array of Integer; |
| 63 |
TArrayOfDouble = array of double; |
| 64 |
|
| 65 |
TRect64 = {$IFDEF RECORD_METHODS}record{$ELSE}object{$ENDIF} |
| 66 |
private |
| 67 |
function GetWidth: Int64; {$IFDEF INLINING} inline; {$ENDIF} |
| 68 |
function GetHeight: Int64; {$IFDEF INLINING} inline; {$ENDIF} |
| 69 |
function GetIsEmpty: Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 70 |
function GetIsValid: Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 71 |
function GetMidPoint: TPoint64; {$IFDEF INLINING} inline; {$ENDIF} |
| 72 |
public |
| 73 |
Left : Int64; |
| 74 |
Top : Int64; |
| 75 |
Right : Int64; |
| 76 |
Bottom : Int64; |
| 77 |
function Contains(const pt: TPoint64; inclusive: Boolean = false): Boolean; overload; |
| 78 |
function Contains(const rec: TRect64): Boolean; overload; |
| 79 |
function Intersect(const rec: TRect64): TRect64; |
| 80 |
function Intersects(const rec: TRect64): Boolean; |
| 81 |
function AsPath: TPath64; |
| 82 |
property Width: Int64 read GetWidth; |
| 83 |
property Height: Int64 read GetHeight; |
| 84 |
property IsEmpty: Boolean read GetIsEmpty; |
| 85 |
property IsValid: Boolean read GetIsValid; |
| 86 |
property MidPoint: TPoint64 read GetMidPoint; |
| 87 |
end; |
| 88 |
|
| 89 |
TRectD = {$ifdef RECORD_METHODS}record{$else}object{$endif} |
| 90 |
private |
| 91 |
function GetWidth: double; {$IFDEF INLINING} inline; {$ENDIF} |
| 92 |
function GetHeight: double; {$IFDEF INLINING} inline; {$ENDIF} |
| 93 |
function GetIsEmpty: Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 94 |
function GetIsValid: Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 95 |
function GetMidPoint: TPointD; {$IFDEF INLINING} inline; {$ENDIF} |
| 96 |
public |
| 97 |
Left : double; |
| 98 |
Top : double; |
| 99 |
Right : double; |
| 100 |
Bottom : double; |
| 101 |
function Contains(const pt: TPointD): Boolean; overload; |
| 102 |
function Contains(const rec: TRectD): Boolean; overload; |
| 103 |
function Intersects(const rec: TRectD): Boolean; |
| 104 |
function AsPath: TPathD; |
| 105 |
property Width: double read GetWidth; |
| 106 |
property Height: double read GetHeight; |
| 107 |
property IsEmpty: Boolean read GetIsEmpty; |
| 108 |
property IsValid: Boolean read GetIsValid; |
| 109 |
property MidPoint: TPointD read GetMidPoint; |
| 110 |
end; |
| 111 |
|
| 112 |
{$IFDEF FPC} |
| 113 |
TPointerList = array of Pointer; |
| 114 |
TListSortCompareFunc = function (Item1, Item2: Pointer): Integer; |
| 115 |
{$ELSE} |
| 116 |
{$IF COMPILERVERSION < 23} //PRIOR DELPHI XE2 |
| 117 |
TPointerList = array of Pointer; |
| 118 |
TListSortCompareFunc = function (Item1, Item2: Pointer): Integer; |
| 119 |
{$IFEND} |
| 120 |
{$ENDIF} |
| 121 |
|
| 122 |
TListEx = class |
| 123 |
private |
| 124 |
fCount : integer; |
| 125 |
fCapacity : integer; |
| 126 |
fList : TPointerList; |
| 127 |
fSorted : Boolean; |
| 128 |
protected |
| 129 |
function UnsafeGet(idx: integer): Pointer; // no range checking |
| 130 |
procedure UnsafeSet(idx: integer; val: Pointer); |
| 131 |
procedure UnsafeDelete(index: integer); virtual; |
| 132 |
public |
| 133 |
constructor Create(capacity: integer = 0); virtual; |
| 134 |
destructor Destroy; override; |
| 135 |
procedure Clear; virtual; |
| 136 |
function Add(item: Pointer): integer; |
| 137 |
procedure DeleteLast; |
| 138 |
procedure Swap(idx1, idx2: integer); |
| 139 |
procedure Sort(Compare: TListSortCompareFunc); |
| 140 |
procedure Resize(count: integer); |
| 141 |
property Count: integer read fCount; |
| 142 |
property Sorted: Boolean read fSorted; |
| 143 |
property Item[idx: integer]: Pointer read UnsafeGet; default; |
| 144 |
end; |
| 145 |
|
| 146 |
TClipType = (ctNoClip, ctIntersection, ctUnion, ctDifference, ctXor); |
| 147 |
|
| 148 |
TPointInPolygonResult = (pipOn, pipInside, pipOutside); |
| 149 |
|
| 150 |
EClipper2LibException = class(Exception); |
| 151 |
|
| 152 |
function Area(const path: TPath64): Double; overload; |
| 153 |
function Area(const paths: TPaths64): Double; overload; |
| 154 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 155 |
function Area(const path: TPathD): Double; overload; |
| 156 |
function Area(const paths: TPathsD): Double; overload; |
| 157 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 158 |
function IsPositive(const path: TPath64): Boolean; overload; |
| 159 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 160 |
function IsPositive(const path: TPathD): Boolean; overload; |
| 161 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 162 |
|
| 163 |
function IsCollinear(const pt1, sharedPt, pt2: TPoint64): Boolean; |
| 164 |
|
| 165 |
function CrossProduct(const pt1, pt2, pt3: TPoint64): double; overload; |
| 166 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 167 |
function CrossProduct(const pt1, pt2, pt3: TPointD): double; overload; |
| 168 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 169 |
function CrossProduct(const vec1, vec2: TPointD): double; overload; |
| 170 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 171 |
function CrossProduct(vec1x, vec1y, vec2x, vec2y: double): double; overload; |
| 172 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 173 |
|
| 174 |
function DotProduct(const pt1, pt2, pt3: TPoint64): double; |
| 175 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 176 |
|
| 177 |
function DistanceSqr(const pt1, pt2: TPoint64): double; overload; |
| 178 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 179 |
function DistanceSqr(const pt1, pt2: TPointD): double; overload; |
| 180 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 181 |
function PerpendicDistFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double; overload; |
| 182 |
function PerpendicDistFromLineSqrd(const pt, linePt1, linePt2: TPointD): double; overload; |
| 183 |
|
| 184 |
function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64; |
| 185 |
inclusive: Boolean = false): boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 186 |
|
| 187 |
function PointsEqual(const pt1, pt2: TPoint64): Boolean; overload; |
| 188 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 189 |
function PointsNearEqual(const pt1, pt2: TPointD): Boolean; overload; |
| 190 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 191 |
function PointsNearEqual(const pt1, pt2: TPointD; distanceSqrd: double): Boolean; overload; |
| 192 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 193 |
|
| 194 |
{$IFDEF USINGZ} |
| 195 |
function Point64(const X, Y: Int64; Z: ZType = 0): TPoint64; overload; |
| 196 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 197 |
function Point64(const X, Y: Double; Z: ZType = 0): TPoint64; overload; |
| 198 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 199 |
function PointD(const X, Y: Double; Z: ZType = 0): TPointD; overload; |
| 200 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 201 |
{$ELSE} |
| 202 |
function Point64(const X, Y: Int64): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 203 |
function Point64(const X, Y: Double): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 204 |
function PointD(const X, Y: Double): TPointD; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 205 |
{$ENDIF} |
| 206 |
|
| 207 |
function Negate(const pt: TPoint64): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 208 |
function Negate(const pt: TPointD): TPointD; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 209 |
function NegatePath(const path: TPathD): TPathD; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 210 |
|
| 211 |
function Point64(const pt: TPointD): TPoint64; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 212 |
function PointD(const pt: TPoint64): TPointD; overload; |
| 213 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 214 |
function Rect64(const left, top, right, bottom: Int64): TRect64; overload; |
| 215 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 216 |
function Rect64(const recD: TRectD): TRect64; overload; |
| 217 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 218 |
function RectD(const left, top, right, bottom: double): TRectD; overload; |
| 219 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 220 |
function RectD(const rec64: TRect64): TRectD; overload; |
| 221 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 222 |
function GetBounds(const paths: TArrayOfPaths): TRect64; overload; |
| 223 |
function GetBounds(const paths: TPaths64): TRect64; overload; |
| 224 |
function GetBounds(const paths: TPathsD): TRectD; overload; |
| 225 |
function GetBounds(const path: TPath64): TRect64; overload; |
| 226 |
function GetBounds(const path: TPathD): TRectD; overload; |
| 227 |
|
| 228 |
function TranslatePoint(const pt: TPoint64; dx, dy: Int64): TPoint64; overload; |
| 229 |
function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD; overload; |
| 230 |
|
| 231 |
procedure RotatePt(var pt: TPointD; const center: TPointD; sinA, cosA: double); |
| 232 |
procedure RotatePath(var path: TPathD; const center: TPointD; sinA, cosA: double); |
| 233 |
|
| 234 |
procedure InflateRect(var rec: TRect64; dx, dy: Int64); overload; |
| 235 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 236 |
procedure InflateRect(var rec: TRectD; dx, dy: double); overload; |
| 237 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 238 |
function UnionRect(const rec, rec2: TRect64): TRect64; overload; |
| 239 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 240 |
function UnionRect(const rec, rec2: TRectD): TRectD; overload; |
| 241 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 242 |
|
| 243 |
function RotateRect(const rec: TRect64; angleRad: double): TRect64; overload; |
| 244 |
function RotateRect(const rec: TRectD; angleRad: double): TRectD; overload; |
| 245 |
procedure OffsetRect(var rec: TRect64; dx, dy: Int64); overload; |
| 246 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 247 |
procedure OffsetRect(var rec: TRectD; dx, dy: double); overload; |
| 248 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 249 |
|
| 250 |
function ScaleRect(const rec: TRect64; scale: double): TRect64; overload; |
| 251 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 252 |
function ScaleRect(const rec: TRectD; scale: double): TRectD; overload; |
| 253 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 254 |
|
| 255 |
function ScalePoint(const pt: TPoint64; scale: double): TPointD; overload; |
| 256 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 257 |
function ScalePoint(const pt: TPointD; scale: double): TPointD; overload; |
| 258 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 259 |
|
| 260 |
function ScalePath(const path: TPath64; sx, sy: double): TPath64; overload; |
| 261 |
function ScalePath(const path: TPathD; sx, sy: double): TPath64; overload; |
| 262 |
function ScalePath(const path: TPath64; scale: double): TPath64; overload; |
| 263 |
function ScalePath(const path: TPathD; scale: double): TPath64; overload; |
| 264 |
|
| 265 |
function ScalePathD(const path: TPath64; sx, sy: double): TPathD; overload; |
| 266 |
function ScalePathD(const path: TPathD; sx, sy: double): TPathD; overload; |
| 267 |
function ScalePathD(const path: TPath64; scale: double): TPathD; overload; |
| 268 |
function ScalePathD(const path: TPathD; scale: double): TPathD; overload; |
| 269 |
|
| 270 |
function ScalePaths(const paths: TPaths64; sx, sy: double): TPaths64; overload; |
| 271 |
function ScalePaths(const paths: TPathsD; sx, sy: double): TPaths64; overload; |
| 272 |
function ScalePaths(const paths: TPaths64; scale: double): TPaths64; overload; |
| 273 |
function ScalePaths(const paths: TPathsD; scale: double): TPaths64; overload; |
| 274 |
|
| 275 |
function ScalePathsD(const paths: TPaths64; sx, sy: double): TPathsD; overload; |
| 276 |
function ScalePathsD(const paths: TPathsD; sx, sy: double): TPathsD; overload; |
| 277 |
function ScalePathsD(const paths: TPaths64; scale: double): TPathsD; overload; |
| 278 |
function ScalePathsD(const paths: TPathsD; scale: double): TPathsD; overload; |
| 279 |
|
| 280 |
function Path64(const pathD: TPathD): TPath64; |
| 281 |
function PathD(const path: TPath64): TPathD; |
| 282 |
function Paths64(const path: TPath64): TPaths64; overload; |
| 283 |
function Paths64(const pathsD: TPathsD): TPaths64; overload; |
| 284 |
function PathsD(const paths: TPaths64): TPathsD; overload; |
| 285 |
function PathsD(const path: TPathD): TPathsD; overload; |
| 286 |
|
| 287 |
function StripDuplicates(const path: TPath64; isClosedPath: Boolean = false): TPath64; |
| 288 |
function StripNearDuplicates(const path: TPathD; |
| 289 |
minLenSqrd: double; isClosedPath: Boolean): TPathD; |
| 290 |
|
| 291 |
function ValueBetween(val, end1, end2: Int64): Boolean; |
| 292 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 293 |
function ValueEqualOrBetween(val, end1, end2: Int64): Boolean; |
| 294 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 295 |
|
| 296 |
function ReversePath(const path: TPath64): TPath64; overload; |
| 297 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 298 |
function ReversePath(const path: TPathD): TPathD; overload; |
| 299 |
function ReversePaths(const paths: TPaths64): TPaths64; overload; |
| 300 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 301 |
function ReversePaths(const paths: TPathsD): TPathsD; overload; |
| 302 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 303 |
|
| 304 |
function ShiftPath(const path: TPath64; shift: integer): TPath64; overload; |
| 305 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 306 |
function ShiftPath(const path: TPathD; shift: integer): TPathD; overload; |
| 307 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 308 |
|
| 309 |
procedure AppendPoint(var path: TPath64; const pt: TPoint64); overload; |
| 310 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 311 |
procedure AppendPoint(var path: TPathD; const pt: TPointD); overload; |
| 312 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 313 |
|
| 314 |
function AppendPoints(const path, extra: TPath64): TPath64; |
| 315 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 316 |
|
| 317 |
procedure AppendPath(var paths: TPaths64; const extra: TPath64); overload; |
| 318 |
procedure AppendPath(var paths: TPathsD; const extra: TPathD); overload; |
| 319 |
procedure AppendPaths(var paths: TPaths64; const extra: TPaths64); overload; |
| 320 |
procedure AppendPaths(var paths: TPathsD; const extra: TPathsD); overload; |
| 321 |
|
| 322 |
function ArrayOfPathsToPaths(const ap: TArrayOfPaths): TPaths64; |
| 323 |
|
| 324 |
function GetSegmentIntersectPt(const ln1a, ln1b, ln2a, ln2b: TPoint64; |
| 325 |
out ip: TPoint64): Boolean; |
| 326 |
|
| 327 |
function PointInPolygon(const pt: TPoint64; const polygon: TPath64): TPointInPolygonResult; |
| 328 |
|
| 329 |
function GetClosestPointOnSegment(const pt, seg1, seg2: TPoint64): TPoint64; |
| 330 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 331 |
|
| 332 |
function RamerDouglasPeucker(const path: TPath64; epsilon: double): TPath64; overload; |
| 333 |
function RamerDouglasPeucker(const paths: TPaths64; epsilon: double): TPaths64; overload; |
| 334 |
function RamerDouglasPeucker(const path: TPathD; epsilon: double): TPathD; overload; |
| 335 |
function RamerDouglasPeucker(const paths: TPathsD; epsilon: double): TPathsD; overload; |
| 336 |
|
| 337 |
procedure GetSinCos(angle: double; out sinA, cosA: double); |
| 338 |
function Ellipse(const rec: TRect64; steps: integer = 0): TPath64; overload; |
| 339 |
function Ellipse(const rec: TRectD; steps: integer = 0): TPathD; overload; |
| 340 |
|
| 341 |
procedure QuickSort(SortList: TPointerList; |
| 342 |
L, R: Integer; const SCompare: TListSortCompareFunc); |
| 343 |
|
| 344 |
procedure CheckPrecisionRange(var precision: integer); |
| 345 |
|
| 346 |
function Iif(eval: Boolean; trueVal, falseVal: Boolean): Boolean; overload; |
| 347 |
function Iif(eval: Boolean; trueVal, falseVal: integer): integer; overload; |
| 348 |
function Iif(eval: Boolean; trueVal, falseVal: Int64): Int64; overload; |
| 349 |
function Iif(eval: Boolean; trueVal, falseVal: double): double; overload; |
| 350 |
|
| 351 |
const |
| 352 |
MaxInt64 = 9223372036854775807; |
| 353 |
MinInt64 = -MaxInt64; |
| 354 |
MaxCoord = MaxInt64 div 4; |
| 355 |
MinCoord = - MaxCoord; |
| 356 |
invalid64 = MaxInt64; |
| 357 |
invalidD = infinity; |
| 358 |
|
| 359 |
NullPointD : TPointD = (X: 0; Y: 0); |
| 360 |
NullRect64 : TRect64 = (left: 0; top: 0; right: 0; Bottom: 0); |
| 361 |
InvalidPt64 : TPoint64 = (X: invalid64; Y: invalid64); |
| 362 |
InvalidPtD : TPointD = (X: invalidD; Y: invalidD); |
| 363 |
|
| 364 |
NullRectD : TRectD = (left: 0; top: 0; right: 0; Bottom: 0); |
| 365 |
InvalidRect64 : TRect64 = |
| 366 |
(left: invalid64; top: invalid64; right: invalid64; bottom: invalid64); |
| 367 |
InvalidRectD : TRectD = |
| 368 |
(left: invalidD; top: invalidD; right: invalidD; bottom: invalidD); |
| 369 |
|
| 370 |
Tolerance : Double = 1.0E-12; |
| 371 |
|
| 372 |
//https://github.com/AngusJohnson/Clipper2/discussions/564 |
| 373 |
MaxDecimalPrecision = 8; |
| 374 |
|
| 375 |
implementation |
| 376 |
|
| 377 |
resourcestring |
| 378 |
rsClipper_PrecisonErr = 'The decimal rounding value is invalid'; |
| 379 |
|
| 380 |
//------------------------------------------------------------------------------ |
| 381 |
// TRect64 methods ... |
| 382 |
//------------------------------------------------------------------------------ |
| 383 |
|
| 384 |
function TRect64.GetWidth: Int64; |
| 385 |
begin |
| 386 |
result := right - left; |
| 387 |
end; |
| 388 |
//------------------------------------------------------------------------------ |
| 389 |
|
| 390 |
function TRect64.GetHeight: Int64; |
| 391 |
begin |
| 392 |
result := bottom - top; |
| 393 |
end; |
| 394 |
//------------------------------------------------------------------------------ |
| 395 |
|
| 396 |
function TRect64.GetIsEmpty: Boolean; |
| 397 |
begin |
| 398 |
result := (bottom <= top) or (right <= left); |
| 399 |
end; |
| 400 |
//------------------------------------------------------------------------------ |
| 401 |
|
| 402 |
function TRect64.GetIsValid: Boolean; |
| 403 |
begin |
| 404 |
result := left <> invalid64; |
| 405 |
end; |
| 406 |
//------------------------------------------------------------------------------ |
| 407 |
|
| 408 |
function TRect64.GetMidPoint: TPoint64; |
| 409 |
begin |
| 410 |
result := Point64((Left + Right) div 2, (Top + Bottom) div 2); |
| 411 |
end; |
| 412 |
//------------------------------------------------------------------------------ |
| 413 |
|
| 414 |
function TRect64.Contains(const pt: TPoint64; inclusive: Boolean = false): Boolean; |
| 415 |
begin |
| 416 |
if inclusive then |
| 417 |
result := (pt.X >= Left) and (pt.X <= Right) and |
| 418 |
(pt.Y >= Top) and (pt.Y <= Bottom) |
| 419 |
else |
| 420 |
result := (pt.X > Left) and (pt.X < Right) and |
| 421 |
(pt.Y > Top) and (pt.Y < Bottom); |
| 422 |
end; |
| 423 |
//------------------------------------------------------------------------------ |
| 424 |
|
| 425 |
function TRect64.Contains(const rec: TRect64): Boolean; |
| 426 |
begin |
| 427 |
result := (rec.Left >= Left) and (rec.Right <= Right) and |
| 428 |
(rec.Top >= Top) and (rec.Bottom <= Bottom); |
| 429 |
end; |
| 430 |
//------------------------------------------------------------------------------ |
| 431 |
|
| 432 |
function TRect64.Intersects(const rec: TRect64): Boolean; |
| 433 |
begin |
| 434 |
Result := (Max(Left, rec.Left) <= Min(Right, rec.Right)) and |
| 435 |
(Max(Top, rec.Top) <= Min(Bottom, rec.Bottom)); |
| 436 |
end; |
| 437 |
//------------------------------------------------------------------------------ |
| 438 |
|
| 439 |
function TRect64.Intersect(const rec: TRect64): TRect64; |
| 440 |
begin |
| 441 |
Result.Left := Max(Left, rec.Left); |
| 442 |
Result.Top := Max(Top, rec.Top); |
| 443 |
Result.Right := Min(Right, rec.Right); |
| 444 |
Result.Bottom := Min(Bottom, rec.Bottom); |
| 445 |
if IsEmpty then Result := NullRect64; |
| 446 |
end; |
| 447 |
//------------------------------------------------------------------------------ |
| 448 |
|
| 449 |
function TRect64.AsPath: TPath64; |
| 450 |
begin |
| 451 |
SetLength(Result, 4); |
| 452 |
Result[0] := Point64(Left, Top); |
| 453 |
Result[1] := Point64(Right, Top); |
| 454 |
Result[2] := Point64(Right, Bottom); |
| 455 |
Result[3] := Point64(Left, Bottom); |
| 456 |
end; |
| 457 |
|
| 458 |
//------------------------------------------------------------------------------ |
| 459 |
// TRectD methods ... |
| 460 |
//------------------------------------------------------------------------------ |
| 461 |
|
| 462 |
function TRectD.GetWidth: double; |
| 463 |
begin |
| 464 |
result := right - left; |
| 465 |
end; |
| 466 |
//------------------------------------------------------------------------------ |
| 467 |
|
| 468 |
function TRectD.GetHeight: double; |
| 469 |
begin |
| 470 |
result := bottom - top; |
| 471 |
end; |
| 472 |
//------------------------------------------------------------------------------ |
| 473 |
|
| 474 |
function TRectD.GetIsEmpty: Boolean; |
| 475 |
begin |
| 476 |
result := (bottom <= top) or (right <= left); |
| 477 |
end; |
| 478 |
//------------------------------------------------------------------------------ |
| 479 |
|
| 480 |
function TRectD.GetIsValid: Boolean; |
| 481 |
begin |
| 482 |
result := left <> invalidD; |
| 483 |
end; |
| 484 |
//------------------------------------------------------------------------------ |
| 485 |
|
| 486 |
function TRectD.GetMidPoint: TPointD; |
| 487 |
begin |
| 488 |
result := PointD((Left + Right) *0.5, (Top + Bottom) *0.5); |
| 489 |
end; |
| 490 |
//------------------------------------------------------------------------------ |
| 491 |
|
| 492 |
function TRectD.Contains(const pt: TPointD): Boolean; |
| 493 |
begin |
| 494 |
result := (pt.X > Left) and (pt.X < Right) and |
| 495 |
(pt.Y > Top) and (pt.Y < Bottom); |
| 496 |
end; |
| 497 |
//------------------------------------------------------------------------------ |
| 498 |
|
| 499 |
function TRectD.Contains(const rec: TRectD): Boolean; |
| 500 |
begin |
| 501 |
result := (rec.Left >= Left) and (rec.Right <= Right) and |
| 502 |
(rec.Top >= Top) and (rec.Bottom <= Bottom); |
| 503 |
end; |
| 504 |
//------------------------------------------------------------------------------ |
| 505 |
|
| 506 |
function TRectD.Intersects(const rec: TRectD): Boolean; |
| 507 |
begin |
| 508 |
Result := (Max(Left, rec.Left) <= Min(Right, rec.Right)) and |
| 509 |
(Max(Top, rec.Top) <= Min(Bottom, rec.Bottom)); |
| 510 |
end; |
| 511 |
//------------------------------------------------------------------------------ |
| 512 |
|
| 513 |
function TRectD.AsPath: TPathD; |
| 514 |
begin |
| 515 |
SetLength(Result, 4); |
| 516 |
Result[0] := PointD(Left, Top); |
| 517 |
Result[1] := PointD(Right, Top); |
| 518 |
Result[2] := PointD(Right, Bottom); |
| 519 |
Result[3] := PointD(Left, Bottom); |
| 520 |
end; |
| 521 |
|
| 522 |
//------------------------------------------------------------------------------ |
| 523 |
// TListEx class |
| 524 |
//------------------------------------------------------------------------------ |
| 525 |
|
| 526 |
constructor TListEx.Create(capacity: integer); |
| 527 |
begin |
| 528 |
if capacity > 0 then |
| 529 |
begin |
| 530 |
fCapacity := 16; |
| 531 |
while capacity > fCapacity do fCapacity := fCapacity * 2; |
| 532 |
SetLength(fList, fCapacity); |
| 533 |
end; |
| 534 |
end; |
| 535 |
//------------------------------------------------------------------------------ |
| 536 |
|
| 537 |
destructor TListEx.Destroy; |
| 538 |
begin |
| 539 |
Clear; |
| 540 |
inherited; |
| 541 |
end; |
| 542 |
//------------------------------------------------------------------------------ |
| 543 |
|
| 544 |
procedure TListEx.Clear; |
| 545 |
begin |
| 546 |
fList := nil; |
| 547 |
fCount := 0; |
| 548 |
fCapacity := 0; |
| 549 |
fSorted := false; |
| 550 |
end; |
| 551 |
//------------------------------------------------------------------------------ |
| 552 |
|
| 553 |
function TListEx.Add(item: Pointer): integer; |
| 554 |
begin |
| 555 |
if fCount = fCapacity then |
| 556 |
begin |
| 557 |
if fCapacity = 0 then |
| 558 |
fCapacity := 16 else |
| 559 |
fCapacity := fCapacity *2; |
| 560 |
SetLength(fList, fCapacity); |
| 561 |
end; |
| 562 |
fList[fCount] := item; |
| 563 |
Result := fCount; |
| 564 |
inc(fCount); |
| 565 |
fSorted := false; |
| 566 |
end; |
| 567 |
//------------------------------------------------------------------------------ |
| 568 |
|
| 569 |
procedure TListEx.DeleteLast; |
| 570 |
begin |
| 571 |
dec(fCount); |
| 572 |
end; |
| 573 |
//------------------------------------------------------------------------------ |
| 574 |
|
| 575 |
procedure QuickSort(SortList: TPointerList; L, R: Integer; |
| 576 |
const SCompare: TListSortCompareFunc); |
| 577 |
var |
| 578 |
I, J: Integer; |
| 579 |
P, T: Pointer; |
| 580 |
begin |
| 581 |
if L >= R then Exit; |
| 582 |
|
| 583 |
repeat |
| 584 |
if (R - L) = 1 then |
| 585 |
begin |
| 586 |
if SCompare(SortList[L], SortList[R]) > 0 then |
| 587 |
begin |
| 588 |
T := SortList[L]; |
| 589 |
SortList[L] := SortList[R]; |
| 590 |
SortList[R] := T; |
| 591 |
end; |
| 592 |
break; |
| 593 |
end; |
| 594 |
|
| 595 |
I := L; |
| 596 |
J := R; |
| 597 |
P := SortList[(L + R) shr 1]; |
| 598 |
repeat |
| 599 |
while SCompare(SortList[I], P) < 0 do Inc(I); |
| 600 |
while SCompare(SortList[J], P) > 0 do Dec(J); |
| 601 |
if I <= J then |
| 602 |
begin |
| 603 |
if I <> J then |
| 604 |
begin |
| 605 |
T := SortList[I]; |
| 606 |
SortList[I] := SortList[J]; |
| 607 |
SortList[J] := T; |
| 608 |
end; |
| 609 |
Inc(I); |
| 610 |
Dec(J); |
| 611 |
end; |
| 612 |
until I > J; |
| 613 |
|
| 614 |
if (J - L) > (R - I) then |
| 615 |
begin |
| 616 |
if I < R then QuickSort(SortList, I, R, SCompare); |
| 617 |
R := J; |
| 618 |
end |
| 619 |
else |
| 620 |
begin |
| 621 |
if L < J then QuickSort(SortList, L, J, SCompare); |
| 622 |
L := I; |
| 623 |
end; |
| 624 |
until L >= R; |
| 625 |
end; |
| 626 |
//------------------------------------------------------------------------------ |
| 627 |
|
| 628 |
procedure TListEx.Sort(Compare: TListSortCompareFunc); |
| 629 |
begin |
| 630 |
if fCount < 2 then Exit; |
| 631 |
QuickSort(FList, 0, fCount - 1, Compare); |
| 632 |
fSorted := true; |
| 633 |
end; |
| 634 |
//------------------------------------------------------------------------------ |
| 635 |
|
| 636 |
procedure TListEx.Resize(count: integer); |
| 637 |
begin |
| 638 |
if (fCapacity = 0) then fCapacity := 16; |
| 639 |
while count > fCapacity do fCapacity := fCapacity * 2; |
| 640 |
SetLength(fList, fCapacity); |
| 641 |
fCount := count; |
| 642 |
end; |
| 643 |
//------------------------------------------------------------------------------ |
| 644 |
|
| 645 |
function TListEx.UnsafeGet(idx: integer): Pointer; |
| 646 |
begin |
| 647 |
Result := fList[idx]; |
| 648 |
end; |
| 649 |
//------------------------------------------------------------------------------ |
| 650 |
|
| 651 |
procedure TListEx.UnsafeSet(idx: integer; val: Pointer); |
| 652 |
begin |
| 653 |
fList[idx] := val; |
| 654 |
end; |
| 655 |
//------------------------------------------------------------------------------ |
| 656 |
|
| 657 |
procedure TListEx.UnsafeDelete(index: integer); |
| 658 |
begin |
| 659 |
dec(fCount); |
| 660 |
if index < fCount then |
| 661 |
Move(fList[index +1], fList[index], (fCount - index) * SizeOf(Pointer)); |
| 662 |
end; |
| 663 |
//------------------------------------------------------------------------------ |
| 664 |
|
| 665 |
procedure TListEx.Swap(idx1, idx2: integer); |
| 666 |
var |
| 667 |
p: Pointer; |
| 668 |
begin |
| 669 |
p := fList[idx1]; |
| 670 |
fList[idx1] := fList[idx2]; |
| 671 |
fList[idx2] := p; |
| 672 |
fSorted := false; |
| 673 |
end; |
| 674 |
|
| 675 |
//------------------------------------------------------------------------------ |
| 676 |
// Miscellaneous Functions ... |
| 677 |
//------------------------------------------------------------------------------ |
| 678 |
|
| 679 |
function Iif(eval: Boolean; trueVal, falseVal: Boolean): Boolean; |
| 680 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 681 |
begin |
| 682 |
if eval then Result := trueVal else Result := falseVal; |
| 683 |
end; |
| 684 |
//------------------------------------------------------------------------------ |
| 685 |
|
| 686 |
function Iif(eval: Boolean; trueVal, falseVal: integer): integer; |
| 687 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 688 |
begin |
| 689 |
if eval then Result := trueVal else Result := falseVal; |
| 690 |
end; |
| 691 |
//------------------------------------------------------------------------------ |
| 692 |
|
| 693 |
function Iif(eval: Boolean; trueVal, falseVal: Int64): Int64; |
| 694 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 695 |
begin |
| 696 |
if eval then Result := trueVal else Result := falseVal; |
| 697 |
end; |
| 698 |
//------------------------------------------------------------------------------ |
| 699 |
|
| 700 |
function Iif(eval: Boolean; trueVal, falseVal: double): double; |
| 701 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 702 |
begin |
| 703 |
if eval then Result := trueVal else Result := falseVal; |
| 704 |
end; |
| 705 |
//------------------------------------------------------------------------------ |
| 706 |
|
| 707 |
procedure CheckPrecisionRange(var precision: integer); |
| 708 |
begin |
| 709 |
if (precision < -MaxDecimalPrecision) or (precision > MaxDecimalPrecision) then |
| 710 |
Raise EClipper2LibException(rsClipper_PrecisonErr); |
| 711 |
end; |
| 712 |
//------------------------------------------------------------------------------ |
| 713 |
|
| 714 |
procedure RaiseError(const msg: string); {$IFDEF INLINING} inline; {$ENDIF} |
| 715 |
begin |
| 716 |
raise EClipper2LibException.Create(msg); |
| 717 |
end; |
| 718 |
//------------------------------------------------------------------------------ |
| 719 |
|
| 720 |
function PointsEqual(const pt1, pt2: TPoint64): Boolean; |
| 721 |
begin |
| 722 |
Result := (pt1.X = pt2.X) and (pt1.Y = pt2.Y); |
| 723 |
end; |
| 724 |
//------------------------------------------------------------------------------ |
| 725 |
|
| 726 |
function PointsNearEqual(const pt1, pt2: TPointD): Boolean; |
| 727 |
begin |
| 728 |
Result := (Abs(pt1.X - pt2.X) < Tolerance) and |
| 729 |
(Abs(pt1.Y - pt2.Y) < Tolerance); |
| 730 |
end; |
| 731 |
//------------------------------------------------------------------------------ |
| 732 |
|
| 733 |
function PointsNearEqual(const pt1, pt2: TPointD; distanceSqrd: double): Boolean; |
| 734 |
begin |
| 735 |
Result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y) < distanceSqrd; |
| 736 |
end; |
| 737 |
//------------------------------------------------------------------------------ |
| 738 |
|
| 739 |
function StripDuplicates(const path: TPath64; isClosedPath: Boolean): TPath64; |
| 740 |
var |
| 741 |
i,j, len: integer; |
| 742 |
begin |
| 743 |
len := length(path); |
| 744 |
SetLength(Result, len); |
| 745 |
if len = 0 then Exit; |
| 746 |
Result[0] := path[0]; |
| 747 |
j := 0; |
| 748 |
for i := 1 to len -1 do |
| 749 |
if not PointsEqual(Result[j], path[i]) then |
| 750 |
begin |
| 751 |
inc(j); |
| 752 |
Result[j] := path[i]; |
| 753 |
end; |
| 754 |
if isClosedPath and PointsEqual(Result[0], path[j]) then dec(j); |
| 755 |
SetLength(Result, j +1); |
| 756 |
end; |
| 757 |
//------------------------------------------------------------------------------ |
| 758 |
|
| 759 |
function StripNearDuplicates(const path: TPathD; |
| 760 |
minLenSqrd: double; isClosedPath: Boolean): TPathD; |
| 761 |
var |
| 762 |
i,j, len: integer; |
| 763 |
begin |
| 764 |
len := length(path); |
| 765 |
SetLength(Result, len); |
| 766 |
if len = 0 then Exit; |
| 767 |
|
| 768 |
Result[0] := path[0]; |
| 769 |
j := 0; |
| 770 |
for i := 1 to len -1 do |
| 771 |
if not PointsNearEqual(Result[j], path[i], minLenSqrd) then |
| 772 |
begin |
| 773 |
inc(j); |
| 774 |
Result[j] := path[i]; |
| 775 |
end; |
| 776 |
|
| 777 |
if isClosedPath and |
| 778 |
PointsNearEqual(Result[j], Result[0], minLenSqrd) then dec(j); |
| 779 |
SetLength(Result, j +1); |
| 780 |
end; |
| 781 |
//------------------------------------------------------------------------------ |
| 782 |
|
| 783 |
function ValueBetween(val, end1, end2: Int64): Boolean; |
| 784 |
begin |
| 785 |
// nb: accommodates axis aligned between where end1 == end2 |
| 786 |
Result := ((val <> end1) = (val <> end2)) and |
| 787 |
((val > end1) = (val < end2)); |
| 788 |
end; |
| 789 |
//------------------------------------------------------------------------------ |
| 790 |
|
| 791 |
function ValueEqualOrBetween(val, end1, end2: Int64): Boolean; |
| 792 |
begin |
| 793 |
Result := (val = end1) or (val = end2) or |
| 794 |
((val > end1) = (val < end2)); |
| 795 |
end; |
| 796 |
//------------------------------------------------------------------------------ |
| 797 |
|
| 798 |
function ScaleRect(const rec: TRect64; scale: double): TRect64; |
| 799 |
begin |
| 800 |
Result.Left := Round(rec.Left * scale); |
| 801 |
Result.Top := Round(rec.Top * scale); |
| 802 |
Result.Right := Round(rec.Right * scale); |
| 803 |
Result.Bottom := Round(rec.Bottom * scale); |
| 804 |
end; |
| 805 |
//------------------------------------------------------------------------------ |
| 806 |
|
| 807 |
function ScaleRect(const rec: TRectD; scale: double): TRectD; |
| 808 |
begin |
| 809 |
Result.Left := rec.Left * scale; |
| 810 |
Result.Top := rec.Top * scale; |
| 811 |
Result.Right := rec.Right * scale; |
| 812 |
Result.Bottom := rec.Bottom * scale; |
| 813 |
end; |
| 814 |
//------------------------------------------------------------------------------ |
| 815 |
|
| 816 |
function ScalePoint(const pt: TPoint64; scale: double): TPointD; |
| 817 |
begin |
| 818 |
Result.X := pt.X * scale; |
| 819 |
Result.Y := pt.Y * scale; |
| 820 |
{$IFDEF USINGZ} |
| 821 |
Result.Z := pt.Z; |
| 822 |
{$ENDIF} |
| 823 |
end; |
| 824 |
//------------------------------------------------------------------------------ |
| 825 |
|
| 826 |
function ScalePoint(const pt: TPointD; scale: double): TPointD; |
| 827 |
begin |
| 828 |
Result.X := pt.X * scale; |
| 829 |
Result.Y := pt.Y * scale; |
| 830 |
{$IFDEF USINGZ} |
| 831 |
Result.Z := pt.Z; |
| 832 |
{$ENDIF} |
| 833 |
end; |
| 834 |
//------------------------------------------------------------------------------ |
| 835 |
|
| 836 |
function ScalePath(const path: TPath64; sx, sy: double): TPath64; |
| 837 |
var |
| 838 |
i,len: integer; |
| 839 |
begin |
| 840 |
if sx = 0 then sx := 1; |
| 841 |
if sy = 0 then sy := 1; |
| 842 |
len := length(path); |
| 843 |
setlength(result, len); |
| 844 |
for i := 0 to len -1 do |
| 845 |
begin |
| 846 |
result[i].X := Round(path[i].X * sx); |
| 847 |
result[i].Y := Round(path[i].Y * sy); |
| 848 |
{$IFDEF USINGZ} |
| 849 |
result[i].Z := path[i].Z; |
| 850 |
{$ENDIF} |
| 851 |
end; |
| 852 |
end; |
| 853 |
//------------------------------------------------------------------------------ |
| 854 |
|
| 855 |
function ScalePath(const path: TPathD; sx, sy: double): TPath64; |
| 856 |
var |
| 857 |
i,j, len: integer; |
| 858 |
begin |
| 859 |
if sx = 0 then sx := 1; |
| 860 |
if sy = 0 then sy := 1; |
| 861 |
len := length(path); |
| 862 |
setlength(result, len); |
| 863 |
if len = 0 then Exit; |
| 864 |
j := 1; |
| 865 |
result[0].X := Round(path[0].X * sx); |
| 866 |
result[0].Y := Round(path[0].Y * sy); |
| 867 |
{$IFDEF USINGZ} |
| 868 |
result[0].Z := path[0].Z; |
| 869 |
{$ENDIF} |
| 870 |
for i := 1 to len -1 do |
| 871 |
begin |
| 872 |
result[j].X := Round(path[i].X * sx); |
| 873 |
result[j].Y := Round(path[i].Y * sy); |
| 874 |
{$IFDEF USINGZ} |
| 875 |
result[j].Z := path[i].Z; |
| 876 |
{$ENDIF} |
| 877 |
if (result[j].X <> result[j-1].X) or |
| 878 |
(result[j].Y <> result[j-1].Y) then inc(j); |
| 879 |
end; |
| 880 |
setlength(result, j); |
| 881 |
end; |
| 882 |
//------------------------------------------------------------------------------ |
| 883 |
|
| 884 |
function ScalePath(const path: TPath64; scale: double): TPath64; |
| 885 |
var |
| 886 |
i,j, len: integer; |
| 887 |
begin |
| 888 |
len := length(path); |
| 889 |
setlength(result, len); |
| 890 |
if len = 0 then Exit; |
| 891 |
j := 1; |
| 892 |
result[0].X := Round(path[0].X * scale); |
| 893 |
result[0].Y := Round(path[0].Y * scale); |
| 894 |
{$IFDEF USINGZ} |
| 895 |
result[0].Z := path[0].Z; |
| 896 |
{$ENDIF} |
| 897 |
for i := 1 to len -1 do |
| 898 |
begin |
| 899 |
result[j].X := Round(path[i].X * scale); |
| 900 |
result[j].Y := Round(path[i].Y * scale); |
| 901 |
{$IFDEF USINGZ} |
| 902 |
result[j].Z := path[i].Z; |
| 903 |
{$ENDIF} |
| 904 |
if (result[j].X <> result[j-1].X) or |
| 905 |
(result[j].Y <> result[j-1].Y) then inc(j); |
| 906 |
end; |
| 907 |
setlength(result, j); |
| 908 |
end; |
| 909 |
//------------------------------------------------------------------------------ |
| 910 |
|
| 911 |
function ScalePath(const path: TPathD; scale: double): TPath64; |
| 912 |
var |
| 913 |
i,len: integer; |
| 914 |
begin |
| 915 |
len := length(path); |
| 916 |
setlength(result, len); |
| 917 |
for i := 0 to len -1 do |
| 918 |
begin |
| 919 |
result[i].X := Round(path[i].X * scale); |
| 920 |
result[i].Y := Round(path[i].Y * scale); |
| 921 |
{$IFDEF USINGZ} |
| 922 |
result[i].Z := path[i].Z; |
| 923 |
{$ENDIF} |
| 924 |
end; |
| 925 |
end; |
| 926 |
//------------------------------------------------------------------------------ |
| 927 |
|
| 928 |
function ScalePaths(const paths: TPaths64; sx, sy: double): TPaths64; |
| 929 |
var |
| 930 |
i,len: integer; |
| 931 |
begin |
| 932 |
if sx = 0 then sx := 1; |
| 933 |
if sy = 0 then sy := 1; |
| 934 |
len := length(paths); |
| 935 |
setlength(result, len); |
| 936 |
for i := 0 to len -1 do |
| 937 |
result[i] := ScalePath(paths[i], sx, sy); |
| 938 |
end; |
| 939 |
//------------------------------------------------------------------------------ |
| 940 |
|
| 941 |
function ScalePaths(const paths: TPathsD; sx, sy: double): TPaths64; |
| 942 |
var |
| 943 |
i,len: integer; |
| 944 |
begin |
| 945 |
if sx = 0 then sx := 1; |
| 946 |
if sy = 0 then sy := 1; |
| 947 |
len := length(paths); |
| 948 |
setlength(result, len); |
| 949 |
for i := 0 to len -1 do |
| 950 |
result[i] := ScalePath(paths[i], sx, sy); |
| 951 |
end; |
| 952 |
//------------------------------------------------------------------------------ |
| 953 |
|
| 954 |
function ScalePathD(const path: TPath64; sx, sy: double): TPathD; |
| 955 |
var |
| 956 |
i: integer; |
| 957 |
begin |
| 958 |
setlength(result, length(path)); |
| 959 |
for i := 0 to high(path) do |
| 960 |
begin |
| 961 |
result[i].X := path[i].X * sx; |
| 962 |
result[i].Y := path[i].Y * sy; |
| 963 |
{$IFDEF USINGZ} |
| 964 |
result[i].Z := path[i].Z; |
| 965 |
{$ENDIF} |
| 966 |
end; |
| 967 |
end; |
| 968 |
//------------------------------------------------------------------------------ |
| 969 |
|
| 970 |
function ScalePathD(const path: TPathD; sx, sy: double): TPathD; |
| 971 |
var |
| 972 |
i: integer; |
| 973 |
begin |
| 974 |
setlength(result, length(path)); |
| 975 |
for i := 0 to high(path) do |
| 976 |
begin |
| 977 |
result[i].X := path[i].X * sx; |
| 978 |
result[i].Y := path[i].Y * sy; |
| 979 |
{$IFDEF USINGZ} |
| 980 |
result[i].Z := path[i].Z; |
| 981 |
{$ENDIF} |
| 982 |
end; |
| 983 |
end; |
| 984 |
//------------------------------------------------------------------------------ |
| 985 |
|
| 986 |
function ScalePathD(const path: TPath64; scale: double): TPathD; |
| 987 |
var |
| 988 |
i: integer; |
| 989 |
begin |
| 990 |
setlength(result, length(path)); |
| 991 |
for i := 0 to high(path) do |
| 992 |
begin |
| 993 |
result[i].X := path[i].X * scale; |
| 994 |
result[i].Y := path[i].Y * scale; |
| 995 |
{$IFDEF USINGZ} |
| 996 |
result[i].Z := path[i].Z; |
| 997 |
{$ENDIF} |
| 998 |
end; |
| 999 |
end; |
| 1000 |
//------------------------------------------------------------------------------ |
| 1001 |
|
| 1002 |
function ScalePathD(const path: TPathD; scale: double): TPathD; |
| 1003 |
var |
| 1004 |
i: integer; |
| 1005 |
begin |
| 1006 |
setlength(result, length(path)); |
| 1007 |
for i := 0 to high(path) do |
| 1008 |
begin |
| 1009 |
result[i].X := path[i].X * scale; |
| 1010 |
result[i].Y := path[i].Y * scale; |
| 1011 |
{$IFDEF USINGZ} |
| 1012 |
result[i].Z := path[i].Z; |
| 1013 |
{$ENDIF} |
| 1014 |
end; |
| 1015 |
end; |
| 1016 |
//------------------------------------------------------------------------------ |
| 1017 |
|
| 1018 |
function ScalePathsD(const paths: TPaths64; sx, sy: double): TPathsD; |
| 1019 |
var |
| 1020 |
i,j: integer; |
| 1021 |
begin |
| 1022 |
if sx = 0 then sx := 1; |
| 1023 |
if sy = 0 then sy := 1; |
| 1024 |
setlength(result, length(paths)); |
| 1025 |
for i := 0 to high(paths) do |
| 1026 |
begin |
| 1027 |
setlength(result[i], length(paths[i])); |
| 1028 |
for j := 0 to high(paths[i]) do |
| 1029 |
begin |
| 1030 |
result[i][j].X := (paths[i][j].X * sx); |
| 1031 |
result[i][j].Y := (paths[i][j].Y * sy); |
| 1032 |
{$IFDEF USINGZ} |
| 1033 |
result[i][j].Z := paths[i][j].Z; |
| 1034 |
{$ENDIF} |
| 1035 |
end; |
| 1036 |
end; |
| 1037 |
end; |
| 1038 |
//------------------------------------------------------------------------------ |
| 1039 |
|
| 1040 |
function ScalePathsD(const paths: TPathsD; sx, sy: double): TPathsD; |
| 1041 |
var |
| 1042 |
i,j: integer; |
| 1043 |
begin |
| 1044 |
if sx = 0 then sx := 1; |
| 1045 |
if sy = 0 then sy := 1; |
| 1046 |
setlength(result, length(paths)); |
| 1047 |
for i := 0 to high(paths) do |
| 1048 |
begin |
| 1049 |
setlength(result[i], length(paths[i])); |
| 1050 |
for j := 0 to high(paths[i]) do |
| 1051 |
begin |
| 1052 |
result[i][j].X := paths[i][j].X * sx; |
| 1053 |
result[i][j].Y := paths[i][j].Y * sy; |
| 1054 |
{$IFDEF USINGZ} |
| 1055 |
result[i][j].Z := paths[i][j].Z; |
| 1056 |
{$ENDIF} |
| 1057 |
end; |
| 1058 |
end; |
| 1059 |
end; |
| 1060 |
//------------------------------------------------------------------------------ |
| 1061 |
|
| 1062 |
function ScalePaths(const paths: TPaths64; scale: double): TPaths64; |
| 1063 |
var |
| 1064 |
i,j: integer; |
| 1065 |
begin |
| 1066 |
setlength(result, length(paths)); |
| 1067 |
for i := 0 to high(paths) do |
| 1068 |
begin |
| 1069 |
setlength(result[i], length(paths[i])); |
| 1070 |
for j := 0 to high(paths[i]) do |
| 1071 |
begin |
| 1072 |
result[i][j].X := Round(paths[i][j].X * scale); |
| 1073 |
result[i][j].Y := Round(paths[i][j].Y * scale); |
| 1074 |
{$IFDEF USINGZ} |
| 1075 |
result[i][j].Z := paths[i][j].Z; |
| 1076 |
{$ENDIF} |
| 1077 |
end; |
| 1078 |
end; |
| 1079 |
end; |
| 1080 |
//------------------------------------------------------------------------------ |
| 1081 |
|
| 1082 |
function ScalePaths(const paths: TPathsD; scale: double): TPaths64; |
| 1083 |
var |
| 1084 |
i,j: integer; |
| 1085 |
begin |
| 1086 |
setlength(result, length(paths)); |
| 1087 |
for i := 0 to high(paths) do |
| 1088 |
begin |
| 1089 |
setlength(result[i], length(paths[i])); |
| 1090 |
for j := 0 to high(paths[i]) do |
| 1091 |
begin |
| 1092 |
result[i][j].X := Round(paths[i][j].X * scale); |
| 1093 |
result[i][j].Y := Round(paths[i][j].Y * scale); |
| 1094 |
{$IFDEF USINGZ} |
| 1095 |
result[i][j].Z := paths[i][j].Z; |
| 1096 |
{$ENDIF} |
| 1097 |
end; |
| 1098 |
end; |
| 1099 |
end; |
| 1100 |
//------------------------------------------------------------------------------ |
| 1101 |
|
| 1102 |
function ScalePathsD(const paths: TPaths64; scale: double): TPathsD; overload; |
| 1103 |
var |
| 1104 |
i,j: integer; |
| 1105 |
begin |
| 1106 |
setlength(result, length(paths)); |
| 1107 |
for i := 0 to high(paths) do |
| 1108 |
begin |
| 1109 |
setlength(result[i], length(paths[i])); |
| 1110 |
for j := 0 to high(paths[i]) do |
| 1111 |
begin |
| 1112 |
result[i][j].X := paths[i][j].X * scale; |
| 1113 |
result[i][j].Y := paths[i][j].Y * scale; |
| 1114 |
{$IFDEF USINGZ} |
| 1115 |
result[i][j].Z := paths[i][j].Z; |
| 1116 |
{$ENDIF} |
| 1117 |
end; |
| 1118 |
end; |
| 1119 |
end; |
| 1120 |
//------------------------------------------------------------------------------ |
| 1121 |
|
| 1122 |
function ScalePathsD(const paths: TPathsD; scale: double): TPathsD; overload; |
| 1123 |
var |
| 1124 |
i,j: integer; |
| 1125 |
begin |
| 1126 |
setlength(result, length(paths)); |
| 1127 |
for i := 0 to high(paths) do |
| 1128 |
begin |
| 1129 |
setlength(result[i], length(paths[i])); |
| 1130 |
for j := 0 to high(paths[i]) do |
| 1131 |
begin |
| 1132 |
result[i][j].X := paths[i][j].X * scale; |
| 1133 |
result[i][j].Y := paths[i][j].Y * scale; |
| 1134 |
{$IFDEF USINGZ} |
| 1135 |
result[i][j].Z := paths[i][j].Z; |
| 1136 |
{$ENDIF} |
| 1137 |
end; |
| 1138 |
end; |
| 1139 |
end; |
| 1140 |
//------------------------------------------------------------------------------ |
| 1141 |
|
| 1142 |
function Path64(const pathD: TPathD): TPath64; |
| 1143 |
var |
| 1144 |
i, len: integer; |
| 1145 |
begin |
| 1146 |
len := Length(pathD); |
| 1147 |
setLength(Result, len); |
| 1148 |
for i := 0 to len -1 do |
| 1149 |
begin |
| 1150 |
Result[i].X := Round(pathD[i].X); |
| 1151 |
Result[i].Y := Round(pathD[i].Y); |
| 1152 |
{$IFDEF USINGZ} |
| 1153 |
Result[i].Z := pathD[i].Z; |
| 1154 |
{$ENDIF} |
| 1155 |
end; |
| 1156 |
end; |
| 1157 |
//------------------------------------------------------------------------------ |
| 1158 |
|
| 1159 |
function PathD(const path: TPath64): TPathD; |
| 1160 |
var |
| 1161 |
i, len: integer; |
| 1162 |
begin |
| 1163 |
len := Length(path); |
| 1164 |
setLength(Result, len); |
| 1165 |
for i := 0 to len -1 do |
| 1166 |
begin |
| 1167 |
Result[i].X := path[i].X; |
| 1168 |
Result[i].Y := path[i].Y; |
| 1169 |
{$IFDEF USINGZ} |
| 1170 |
Result[i].Z := path[i].Z; |
| 1171 |
{$ENDIF} |
| 1172 |
end; |
| 1173 |
end; |
| 1174 |
//------------------------------------------------------------------------------ |
| 1175 |
|
| 1176 |
function Paths64(const path: TPath64): TPaths64; |
| 1177 |
begin |
| 1178 |
setLength(Result, 1); |
| 1179 |
Result[0] := path; |
| 1180 |
end; |
| 1181 |
//------------------------------------------------------------------------------ |
| 1182 |
|
| 1183 |
function Paths64(const pathsD: TPathsD): TPaths64; |
| 1184 |
var |
| 1185 |
i, len: integer; |
| 1186 |
begin |
| 1187 |
len := Length(pathsD); |
| 1188 |
setLength(Result, len); |
| 1189 |
for i := 0 to len -1 do |
| 1190 |
Result[i] := Path64(pathsD[i]); |
| 1191 |
end; |
| 1192 |
//------------------------------------------------------------------------------ |
| 1193 |
|
| 1194 |
function PathsD(const paths: TPaths64): TPathsD; |
| 1195 |
var |
| 1196 |
i, len: integer; |
| 1197 |
begin |
| 1198 |
len := Length(paths); |
| 1199 |
setLength(Result, len); |
| 1200 |
for i := 0 to len -1 do |
| 1201 |
Result[i] := PathD(paths[i]); |
| 1202 |
end; |
| 1203 |
//------------------------------------------------------------------------------ |
| 1204 |
|
| 1205 |
function PathsD(const path: TPathD): TPathsD; |
| 1206 |
begin |
| 1207 |
setLength(Result, 1); |
| 1208 |
Result[0] := path; |
| 1209 |
end; |
| 1210 |
//------------------------------------------------------------------------------ |
| 1211 |
|
| 1212 |
|
| 1213 |
function ReversePath(const path: TPath64): TPath64; |
| 1214 |
var |
| 1215 |
i, highI: Integer; |
| 1216 |
begin |
| 1217 |
highI := high(path); |
| 1218 |
SetLength(Result, highI +1); |
| 1219 |
for i := 0 to highI do |
| 1220 |
Result[i] := path[highI - i]; |
| 1221 |
end; |
| 1222 |
//------------------------------------------------------------------------------ |
| 1223 |
|
| 1224 |
function ReversePath(const path: TPathD): TPathD; |
| 1225 |
var |
| 1226 |
i, highI: Integer; |
| 1227 |
begin |
| 1228 |
highI := high(path); |
| 1229 |
SetLength(Result, highI +1); |
| 1230 |
for i := 0 to highI do |
| 1231 |
Result[i] := path[highI - i]; |
| 1232 |
end; |
| 1233 |
//------------------------------------------------------------------------------ |
| 1234 |
|
| 1235 |
function ReversePaths(const paths: TPaths64): TPaths64; |
| 1236 |
var |
| 1237 |
i, j, highJ: Integer; |
| 1238 |
begin |
| 1239 |
i := length(paths); |
| 1240 |
SetLength(Result, i); |
| 1241 |
for i := 0 to i -1 do |
| 1242 |
begin |
| 1243 |
highJ := high(paths[i]); |
| 1244 |
SetLength(Result[i], highJ+1); |
| 1245 |
for j := 0 to highJ do |
| 1246 |
Result[i][j] := paths[i][highJ - j]; |
| 1247 |
end; |
| 1248 |
end; |
| 1249 |
//------------------------------------------------------------------------------ |
| 1250 |
|
| 1251 |
function ReversePaths(const paths: TPathsD): TPathsD; |
| 1252 |
var |
| 1253 |
i, j, highJ: Integer; |
| 1254 |
begin |
| 1255 |
i := length(paths); |
| 1256 |
SetLength(Result, i); |
| 1257 |
for i := 0 to i -1 do |
| 1258 |
begin |
| 1259 |
highJ := high(paths[i]); |
| 1260 |
SetLength(Result[i], highJ+1); |
| 1261 |
for j := 0 to highJ do |
| 1262 |
Result[i][j] := paths[i][highJ - j]; |
| 1263 |
end; |
| 1264 |
end; |
| 1265 |
//------------------------------------------------------------------------------ |
| 1266 |
|
| 1267 |
function ShiftPath(const path: TPath64; shift: integer): TPath64; |
| 1268 |
var |
| 1269 |
diff, len: Integer; |
| 1270 |
begin |
| 1271 |
Result := nil; |
| 1272 |
len := Length(path); |
| 1273 |
if len = 0 then Exit; |
| 1274 |
Result := Copy(path, 0, len); |
| 1275 |
shift := shift mod len; |
| 1276 |
if shift = 0 then Exit; |
| 1277 |
if shift < 0 then shift := len + shift; |
| 1278 |
diff := len - shift; |
| 1279 |
Move(path[shift], Result[0], diff *SizeOf(TPoint64)); |
| 1280 |
Move(path[0], Result[diff], shift *SizeOf(TPoint64)); |
| 1281 |
end; |
| 1282 |
//------------------------------------------------------------------------------ |
| 1283 |
|
| 1284 |
function ShiftPath(const path: TPathD; shift: integer): TPathD; |
| 1285 |
var |
| 1286 |
diff, len: Integer; |
| 1287 |
begin |
| 1288 |
Result := nil; |
| 1289 |
len := Length(path); |
| 1290 |
if len = 0 then Exit; |
| 1291 |
Result := Copy(path, 0, len); |
| 1292 |
shift := shift mod len; |
| 1293 |
if shift = 0 then Exit; |
| 1294 |
if shift < 0 then shift := len + shift; |
| 1295 |
diff := len - shift; |
| 1296 |
Move(path[shift], Result[0], diff *SizeOf(TPointD)); |
| 1297 |
Move(path[0], Result[diff], shift *SizeOf(TPointD)); |
| 1298 |
end; |
| 1299 |
//------------------------------------------------------------------------------ |
| 1300 |
|
| 1301 |
|
| 1302 |
procedure AppendPoint(var path: TPath64; const pt: TPoint64); |
| 1303 |
var |
| 1304 |
len: Integer; |
| 1305 |
begin |
| 1306 |
len := length(path); |
| 1307 |
SetLength(path, len +1); |
| 1308 |
path[len] := pt; |
| 1309 |
end; |
| 1310 |
//------------------------------------------------------------------------------ |
| 1311 |
|
| 1312 |
function AppendPoints(const path, extra: TPath64): TPath64; |
| 1313 |
var |
| 1314 |
len1, len2: Integer; |
| 1315 |
begin |
| 1316 |
len1 := length(path); |
| 1317 |
len2 := length(extra); |
| 1318 |
SetLength(Result, len1 + len2); |
| 1319 |
if len1 > 0 then |
| 1320 |
Move(path[0], Result[0], len1 * sizeOf(TPoint64)); |
| 1321 |
if len2 > 0 then |
| 1322 |
Move(extra[0], Result[len1], len2 * sizeOf(TPoint64)); |
| 1323 |
end; |
| 1324 |
//------------------------------------------------------------------------------ |
| 1325 |
|
| 1326 |
procedure AppendPoint(var path: TPathD; const pt: TPointD); |
| 1327 |
var |
| 1328 |
len: Integer; |
| 1329 |
begin |
| 1330 |
len := length(path); |
| 1331 |
SetLength(path, len +1); |
| 1332 |
path[len] := pt; |
| 1333 |
end; |
| 1334 |
//------------------------------------------------------------------------------ |
| 1335 |
|
| 1336 |
procedure AppendPath(var paths: TPaths64; const extra: TPath64); |
| 1337 |
var |
| 1338 |
len: Integer; |
| 1339 |
begin |
| 1340 |
if not Assigned(extra) then Exit; |
| 1341 |
len := length(paths); |
| 1342 |
SetLength(paths, len +1); |
| 1343 |
paths[len] := extra; |
| 1344 |
end; |
| 1345 |
//------------------------------------------------------------------------------ |
| 1346 |
|
| 1347 |
procedure AppendPath(var paths: TPathsD; const extra: TPathD); |
| 1348 |
var |
| 1349 |
len: Integer; |
| 1350 |
begin |
| 1351 |
if not Assigned(extra) then Exit; |
| 1352 |
len := length(paths); |
| 1353 |
SetLength(paths, len +1); |
| 1354 |
paths[len] := extra; |
| 1355 |
end; |
| 1356 |
//------------------------------------------------------------------------------ |
| 1357 |
|
| 1358 |
procedure AppendPaths(var paths: TPaths64; const extra: TPaths64); |
| 1359 |
var |
| 1360 |
i, len1, len2: Integer; |
| 1361 |
begin |
| 1362 |
len1 := length(paths); |
| 1363 |
len2 := length(extra); |
| 1364 |
SetLength(paths, len1 + len2); |
| 1365 |
for i := 0 to len2 -1 do |
| 1366 |
paths[len1 + i] := extra[i]; |
| 1367 |
end; |
| 1368 |
//------------------------------------------------------------------------------ |
| 1369 |
|
| 1370 |
procedure AppendPaths(var paths: TPathsD; const extra: TPathsD); |
| 1371 |
var |
| 1372 |
i, len1, len2: Integer; |
| 1373 |
begin |
| 1374 |
len1 := length(paths); |
| 1375 |
len2 := length(extra); |
| 1376 |
SetLength(paths, len1 + len2); |
| 1377 |
for i := 0 to len2 -1 do |
| 1378 |
paths[len1 + i] := extra[i]; |
| 1379 |
end; |
| 1380 |
//------------------------------------------------------------------------------ |
| 1381 |
|
| 1382 |
function ArrayOfPathsToPaths(const ap: TArrayOfPaths): TPaths64; |
| 1383 |
var |
| 1384 |
i,j,k, len, cnt: integer; |
| 1385 |
begin |
| 1386 |
cnt := 0; |
| 1387 |
len := length(ap); |
| 1388 |
for i := 0 to len -1 do |
| 1389 |
inc(cnt, length(ap[i])); |
| 1390 |
k := 0; |
| 1391 |
setlength(result, cnt); |
| 1392 |
for i := 0 to len -1 do |
| 1393 |
for j := 0 to length(ap[i]) -1 do |
| 1394 |
begin |
| 1395 |
result[k] := ap[i][j]; |
| 1396 |
inc(k); |
| 1397 |
end; |
| 1398 |
end; |
| 1399 |
//------------------------------------------------------------------------------ |
| 1400 |
|
| 1401 |
{$IFDEF USINGZ} |
| 1402 |
function Point64(const X, Y: Int64; Z: ZType): TPoint64; |
| 1403 |
begin |
| 1404 |
Result.X := X; |
| 1405 |
Result.Y := Y; |
| 1406 |
Result.Z := Z; |
| 1407 |
end; |
| 1408 |
//------------------------------------------------------------------------------ |
| 1409 |
|
| 1410 |
function Point64(const X, Y: Double; Z: ZType): TPoint64; |
| 1411 |
begin |
| 1412 |
Result.X := Round(X); |
| 1413 |
Result.Y := Round(Y); |
| 1414 |
Result.Z := Z; |
| 1415 |
end; |
| 1416 |
//------------------------------------------------------------------------------ |
| 1417 |
|
| 1418 |
function PointD(const X, Y: Double; Z: ZType): TPointD; |
| 1419 |
begin |
| 1420 |
Result.X := X; |
| 1421 |
Result.Y := Y; |
| 1422 |
Result.Z := Z; |
| 1423 |
end; |
| 1424 |
//------------------------------------------------------------------------------ |
| 1425 |
|
| 1426 |
function Point64(const pt: TPointD): TPoint64; |
| 1427 |
begin |
| 1428 |
Result.X := Round(pt.X); |
| 1429 |
Result.Y := Round(pt.Y); |
| 1430 |
Result.Z := pt.Z; |
| 1431 |
end; |
| 1432 |
//------------------------------------------------------------------------------ |
| 1433 |
|
| 1434 |
function PointD(const pt: TPoint64): TPointD; |
| 1435 |
begin |
| 1436 |
Result.X := pt.X; |
| 1437 |
Result.Y := pt.Y; |
| 1438 |
Result.Z := pt.Z; |
| 1439 |
end; |
| 1440 |
//------------------------------------------------------------------------------ |
| 1441 |
|
| 1442 |
{$ELSE} |
| 1443 |
|
| 1444 |
function Point64(const X, Y: Int64): TPoint64; |
| 1445 |
begin |
| 1446 |
Result.X := X; |
| 1447 |
Result.Y := Y; |
| 1448 |
end; |
| 1449 |
//------------------------------------------------------------------------------ |
| 1450 |
|
| 1451 |
function Point64(const X, Y: Double): TPoint64; |
| 1452 |
begin |
| 1453 |
Result.X := Round(X); |
| 1454 |
Result.Y := Round(Y); |
| 1455 |
end; |
| 1456 |
//------------------------------------------------------------------------------ |
| 1457 |
|
| 1458 |
function PointD(const X, Y: Double): TPointD; |
| 1459 |
begin |
| 1460 |
Result.X := X; |
| 1461 |
Result.Y := Y; |
| 1462 |
end; |
| 1463 |
//------------------------------------------------------------------------------ |
| 1464 |
|
| 1465 |
function Point64(const pt: TPointD): TPoint64; |
| 1466 |
begin |
| 1467 |
Result.X := Round(pt.X); |
| 1468 |
Result.Y := Round(pt.Y); |
| 1469 |
end; |
| 1470 |
//------------------------------------------------------------------------------ |
| 1471 |
|
| 1472 |
function PointD(const pt: TPoint64): TPointD; |
| 1473 |
begin |
| 1474 |
Result.X := pt.X; |
| 1475 |
Result.Y := pt.Y; |
| 1476 |
end; |
| 1477 |
//------------------------------------------------------------------------------ |
| 1478 |
{$ENDIF} |
| 1479 |
|
| 1480 |
function Negate(const pt: TPoint64): TPoint64; |
| 1481 |
begin |
| 1482 |
Result.X := -pt.X; |
| 1483 |
Result.Y := -pt.Y; |
| 1484 |
end; |
| 1485 |
//------------------------------------------------------------------------------ |
| 1486 |
|
| 1487 |
function Negate(const pt: TPointD): TPointD; |
| 1488 |
begin |
| 1489 |
Result.X := -pt.X; |
| 1490 |
Result.Y := -pt.Y; |
| 1491 |
end; |
| 1492 |
//------------------------------------------------------------------------------ |
| 1493 |
|
| 1494 |
function NegatePath(const path: TPathD): TPathD; |
| 1495 |
var |
| 1496 |
i: Integer; |
| 1497 |
begin |
| 1498 |
Result := path; |
| 1499 |
for i := 0 to High(Result) do |
| 1500 |
with Result[i] do |
| 1501 |
begin |
| 1502 |
X := -X; |
| 1503 |
Y := -Y; |
| 1504 |
end; |
| 1505 |
end; |
| 1506 |
//------------------------------------------------------------------------------ |
| 1507 |
|
| 1508 |
function Rect64(const left, top, right, bottom: Int64): TRect64; |
| 1509 |
begin |
| 1510 |
Result.Left := left; |
| 1511 |
Result.Top := top; |
| 1512 |
Result.Right := right; |
| 1513 |
Result.Bottom := bottom; |
| 1514 |
end; |
| 1515 |
//------------------------------------------------------------------------------ |
| 1516 |
|
| 1517 |
function Rect64(const recD: TRectD): TRect64; |
| 1518 |
begin |
| 1519 |
Result.Left := Floor(recD.left); |
| 1520 |
Result.Top := Floor(recD.top); |
| 1521 |
Result.Right := Ceil(recD.right); |
| 1522 |
Result.Bottom := Ceil(recD.bottom); |
| 1523 |
end; |
| 1524 |
//------------------------------------------------------------------------------ |
| 1525 |
|
| 1526 |
function RectD(const left, top, right, bottom: double): TRectD; |
| 1527 |
begin |
| 1528 |
Result.Left := left; |
| 1529 |
Result.Top := top; |
| 1530 |
Result.Right := right; |
| 1531 |
Result.Bottom := bottom; |
| 1532 |
end; |
| 1533 |
//------------------------------------------------------------------------------ |
| 1534 |
|
| 1535 |
function RectD(const rec64: TRect64): TRectD; overload; |
| 1536 |
begin |
| 1537 |
Result.Left := rec64.left; |
| 1538 |
Result.Top := rec64.top; |
| 1539 |
Result.Right := rec64.right; |
| 1540 |
Result.Bottom := rec64.bottom; |
| 1541 |
end; |
| 1542 |
//------------------------------------------------------------------------------ |
| 1543 |
|
| 1544 |
function GetBounds(const paths: TArrayOfPaths): TRect64; overload; |
| 1545 |
var |
| 1546 |
i,j,k: Integer; |
| 1547 |
p: PPoint64; |
| 1548 |
begin |
| 1549 |
Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64); |
| 1550 |
for i := 0 to High(paths) do |
| 1551 |
for j := 0 to High(paths[i]) do |
| 1552 |
if Assigned(paths[i][j]) then |
| 1553 |
begin |
| 1554 |
p := @paths[i][j][0]; |
| 1555 |
for k := 0 to High(paths[i][j]) do |
| 1556 |
begin |
| 1557 |
if p.X < Result.Left then Result.Left := p.X; |
| 1558 |
if p.X > Result.Right then Result.Right := p.X; |
| 1559 |
if p.Y < Result.Top then Result.Top := p.Y; |
| 1560 |
if p.Y > Result.Bottom then Result.Bottom := p.Y; |
| 1561 |
inc(p); |
| 1562 |
end; |
| 1563 |
end; |
| 1564 |
if Result.Left > Result.Right then Result := NullRect64; |
| 1565 |
end; |
| 1566 |
//------------------------------------------------------------------------------ |
| 1567 |
|
| 1568 |
function GetBounds(const paths: TPaths64): TRect64; |
| 1569 |
var |
| 1570 |
i,j: Integer; |
| 1571 |
p: PPoint64; |
| 1572 |
begin |
| 1573 |
Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64); |
| 1574 |
for i := 0 to High(paths) do |
| 1575 |
if Assigned(paths[i]) then |
| 1576 |
begin |
| 1577 |
p := @paths[i][0]; |
| 1578 |
for j := 0 to High(paths[i]) do |
| 1579 |
begin |
| 1580 |
if p.X < Result.Left then Result.Left := p.X; |
| 1581 |
if p.X > Result.Right then Result.Right := p.X; |
| 1582 |
if p.Y < Result.Top then Result.Top := p.Y; |
| 1583 |
if p.Y > Result.Bottom then Result.Bottom := p.Y; |
| 1584 |
inc(p); |
| 1585 |
end; |
| 1586 |
end; |
| 1587 |
if Result.Left = MaxInt64 then Result := NullRect64; |
| 1588 |
end; |
| 1589 |
//------------------------------------------------------------------------------ |
| 1590 |
|
| 1591 |
function GetBounds(const paths: TPathsD): TRectD; |
| 1592 |
var |
| 1593 |
i,j: Integer; |
| 1594 |
p: PPointD; |
| 1595 |
begin |
| 1596 |
Result := RectD(MaxDouble, MaxDouble, -MaxDouble, -MaxDouble); |
| 1597 |
for i := 0 to High(paths) do |
| 1598 |
if Assigned(paths[i]) then |
| 1599 |
begin |
| 1600 |
p := @paths[i][0]; |
| 1601 |
for j := 0 to High(paths[i]) do |
| 1602 |
begin |
| 1603 |
if p.X < Result.Left then Result.Left := p.X; |
| 1604 |
if p.X > Result.Right then Result.Right := p.X; |
| 1605 |
if p.Y < Result.Top then Result.Top := p.Y; |
| 1606 |
if p.Y > Result.Bottom then Result.Bottom := p.Y; |
| 1607 |
inc(p); |
| 1608 |
end; |
| 1609 |
end; |
| 1610 |
if Result.Left = MaxDouble then Result := NullRectD; |
| 1611 |
end; |
| 1612 |
//------------------------------------------------------------------------------ |
| 1613 |
|
| 1614 |
function GetBounds(const path: TPath64): TRect64; |
| 1615 |
var |
| 1616 |
i, len: Integer; |
| 1617 |
p: PPoint64; |
| 1618 |
begin |
| 1619 |
len := Length(path); |
| 1620 |
if len = 0 then |
| 1621 |
begin |
| 1622 |
Result := NullRect64; |
| 1623 |
Exit; |
| 1624 |
end; |
| 1625 |
|
| 1626 |
Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64); |
| 1627 |
p := @path[0]; |
| 1628 |
for i := 0 to High(path) do |
| 1629 |
begin |
| 1630 |
if p.X < Result.Left then Result.Left := p.X; |
| 1631 |
if p.X > Result.Right then Result.Right := p.X; |
| 1632 |
if p.Y < Result.Top then Result.Top := p.Y; |
| 1633 |
if p.Y > Result.Bottom then Result.Bottom := p.Y; |
| 1634 |
inc(p); |
| 1635 |
end; |
| 1636 |
end; |
| 1637 |
//------------------------------------------------------------------------------ |
| 1638 |
|
| 1639 |
function GetBounds(const path: TPathD): TRectD; |
| 1640 |
var |
| 1641 |
i, len: Integer; |
| 1642 |
p: PPointD; |
| 1643 |
begin |
| 1644 |
len := Length(path); |
| 1645 |
if len = 0 then |
| 1646 |
begin |
| 1647 |
Result := NullRectD; |
| 1648 |
Exit; |
| 1649 |
end; |
| 1650 |
|
| 1651 |
Result := RectD(infinity, infinity, -infinity, -infinity); |
| 1652 |
p := @path[0]; |
| 1653 |
for i := 0 to High(path) do |
| 1654 |
begin |
| 1655 |
if p.X < Result.Left then Result.Left := p.X; |
| 1656 |
if p.X > Result.Right then Result.Right := p.X; |
| 1657 |
if p.Y < Result.Top then Result.Top := p.Y; |
| 1658 |
if p.Y > Result.Bottom then Result.Bottom := p.Y; |
| 1659 |
inc(p); |
| 1660 |
end; |
| 1661 |
end; |
| 1662 |
//------------------------------------------------------------------------------ |
| 1663 |
|
| 1664 |
function TranslatePoint(const pt: TPoint64; dx, dy: Int64): TPoint64; |
| 1665 |
begin |
| 1666 |
Result.X := pt.X + dx; |
| 1667 |
Result.Y := pt.Y + dy; |
| 1668 |
end; |
| 1669 |
//------------------------------------------------------------------------------ |
| 1670 |
|
| 1671 |
function TranslatePoint(const pt: TPointD; dx, dy: double): TPointD; |
| 1672 |
begin |
| 1673 |
Result.X := pt.X + dx; |
| 1674 |
Result.Y := pt.Y + dy; |
| 1675 |
end; |
| 1676 |
//------------------------------------------------------------------------------ |
| 1677 |
|
| 1678 |
procedure InflateRect(var rec: TRect64; dx, dy: Int64); |
| 1679 |
begin |
| 1680 |
dec(rec.Left, dx); |
| 1681 |
inc(rec.Right, dx); |
| 1682 |
dec(rec.Top, dy); |
| 1683 |
inc(rec.Bottom, dy); |
| 1684 |
end; |
| 1685 |
//------------------------------------------------------------------------------ |
| 1686 |
|
| 1687 |
procedure InflateRect(var rec: TRectD; dx, dy: double); |
| 1688 |
begin |
| 1689 |
rec.Left := rec.Left - dx; |
| 1690 |
rec.Right := rec.Right + dx; |
| 1691 |
rec.Top := rec.Top - dy; |
| 1692 |
rec.Bottom := rec.Bottom + dy; |
| 1693 |
end; |
| 1694 |
//------------------------------------------------------------------------------ |
| 1695 |
|
| 1696 |
procedure RotatePt(var pt: TPointD; const center: TPointD; sinA, cosA: double); |
| 1697 |
var |
| 1698 |
tmpX, tmpY: double; |
| 1699 |
begin |
| 1700 |
tmpX := pt.X-center.X; |
| 1701 |
tmpY := pt.Y-center.Y; |
| 1702 |
pt.X := tmpX * cosA - tmpY * sinA + center.X; |
| 1703 |
pt.Y := tmpX * sinA + tmpY * cosA + center.Y; |
| 1704 |
end; |
| 1705 |
//------------------------------------------------------------------------------ |
| 1706 |
|
| 1707 |
procedure RotatePath(var path: TPathD; const center: TPointD; sinA, cosA: double); |
| 1708 |
var |
| 1709 |
i: integer; |
| 1710 |
begin |
| 1711 |
for i := 0 to High(path) do |
| 1712 |
RotatePt(path[i], center, sinA, cosA); |
| 1713 |
end; |
| 1714 |
//------------------------------------------------------------------------------ |
| 1715 |
|
| 1716 |
function RotateRect(const rec: TRectD; angleRad: double): TRectD; |
| 1717 |
var |
| 1718 |
i: integer; |
| 1719 |
sinA, cosA: double; |
| 1720 |
cp: TPointD; |
| 1721 |
pts: TPathD; |
| 1722 |
begin |
| 1723 |
setLength(pts, 4); |
| 1724 |
sinA := Sin(-angleRad); |
| 1725 |
cosA := cos(-angleRad); |
| 1726 |
cp.X := (rec.Right + rec.Left) / 2; |
| 1727 |
cp.Y := (rec.Bottom + rec.Top) / 2; |
| 1728 |
pts[0] := PointD(rec.Left, rec.Top); |
| 1729 |
pts[1] := PointD(rec.Right, rec.Top); |
| 1730 |
pts[2] := PointD(rec.Left, rec.Bottom); |
| 1731 |
pts[3] := PointD(rec.Right, rec.Bottom); |
| 1732 |
for i := 0 to 3 do RotatePt(pts[i], cp, sinA, cosA); |
| 1733 |
result.Left := pts[0].X; |
| 1734 |
result.Right := result.Left; |
| 1735 |
result.Top := pts[0].Y; |
| 1736 |
result.Bottom := result.Top; |
| 1737 |
for i := 1 to 3 do |
| 1738 |
begin |
| 1739 |
if pts[i].X < result.Left then result.Left := pts[i].X; |
| 1740 |
if pts[i].Y < result.Top then result.Top := pts[i].Y; |
| 1741 |
if pts[i].X > result.Right then result.Right := pts[i].X; |
| 1742 |
if pts[i].Y > result.Bottom then result.Bottom := pts[i].Y; |
| 1743 |
end; |
| 1744 |
end; |
| 1745 |
//------------------------------------------------------------------------------ |
| 1746 |
|
| 1747 |
function RotateRect(const rec: TRect64; angleRad: double): TRect64; |
| 1748 |
var |
| 1749 |
recD: TRectD; |
| 1750 |
begin |
| 1751 |
recD := RectD(rec.Left, rec.Top, rec.Right, rec.Bottom); |
| 1752 |
recD := RotateRect(recD, angleRad); |
| 1753 |
result.Left := Floor(recD.Left); |
| 1754 |
result.Top := Floor(recD.Top); |
| 1755 |
result.Right := Ceil(recD.Right); |
| 1756 |
result.Bottom := Ceil(recD.Bottom); |
| 1757 |
end; |
| 1758 |
//------------------------------------------------------------------------------ |
| 1759 |
|
| 1760 |
procedure OffsetRect(var rec: TRect64; dx, dy: Int64); |
| 1761 |
begin |
| 1762 |
inc(rec.Left, dx); inc(rec.Top, dy); |
| 1763 |
inc(rec.Right, dx); inc(rec.Bottom, dy); |
| 1764 |
end; |
| 1765 |
//------------------------------------------------------------------------------ |
| 1766 |
|
| 1767 |
procedure OffsetRect(var rec: TRectD; dx, dy: double); |
| 1768 |
begin |
| 1769 |
rec.Left := rec.Left + dx; |
| 1770 |
rec.Right := rec.Right + dx; |
| 1771 |
rec.Top := rec.Top + dy; |
| 1772 |
rec.Bottom := rec.Bottom + dy; |
| 1773 |
end; |
| 1774 |
//------------------------------------------------------------------------------ |
| 1775 |
|
| 1776 |
function UnionRect(const rec, rec2: TRect64): TRect64; |
| 1777 |
begin |
| 1778 |
// nb: don't use rec.IsEmpty as this will |
| 1779 |
// reject open axis-aligned flat paths |
| 1780 |
if (rec.Width <= 0) and (rec.Height <= 0) then result := rec2 |
| 1781 |
else if (rec2.Width <= 0) and (rec2.Height <= 0) then result := rec |
| 1782 |
else |
| 1783 |
begin |
| 1784 |
result.Left := min(rec.Left, rec2.Left); |
| 1785 |
result.Right := max(rec.Right, rec2.Right); |
| 1786 |
result.Top := min(rec.Top, rec2.Top); |
| 1787 |
result.Bottom := max(rec.Bottom, rec2.Bottom); |
| 1788 |
end; |
| 1789 |
end; |
| 1790 |
//------------------------------------------------------------------------------ |
| 1791 |
|
| 1792 |
function UnionRect(const rec, rec2: TRectD): TRectD; |
| 1793 |
begin |
| 1794 |
// nb: don't use rec.IsEmpty as this will |
| 1795 |
// reject open axis-aligned flat paths |
| 1796 |
if (rec.Width <= 0) and (rec.Height <= 0) then result := rec2 |
| 1797 |
else if (rec2.Width <= 0) and (rec2.Height <= 0) then result := rec |
| 1798 |
else |
| 1799 |
begin |
| 1800 |
result.Left := min(rec.Left, rec2.Left); |
| 1801 |
result.Right := max(rec.Right, rec2.Right); |
| 1802 |
result.Top := min(rec.Top, rec2.Top); |
| 1803 |
result.Bottom := max(rec.Bottom, rec2.Bottom); |
| 1804 |
end; |
| 1805 |
end; |
| 1806 |
//------------------------------------------------------------------------------ |
| 1807 |
|
| 1808 |
function Area(const path: TPath64): Double; |
| 1809 |
var |
| 1810 |
i, highI: Integer; |
| 1811 |
d: double; |
| 1812 |
p1,p2: PPoint64; |
| 1813 |
begin |
| 1814 |
// shoelace formula |
| 1815 |
Result := 0.0; |
| 1816 |
highI := High(path); |
| 1817 |
if highI < 2 then Exit; |
| 1818 |
p1 := @path[highI]; |
| 1819 |
p2 := @path[0]; |
| 1820 |
for i := 0 to highI do |
| 1821 |
begin |
| 1822 |
d := (p1.Y + p2.Y); // needed for Delphi7 |
| 1823 |
Result := Result + d * (p1.X - p2.X); |
| 1824 |
p1 := p2; inc(p2); |
| 1825 |
end; |
| 1826 |
Result := Result * 0.5; |
| 1827 |
end; |
| 1828 |
//------------------------------------------------------------------------------ |
| 1829 |
|
| 1830 |
function Area(const paths: TPaths64): Double; |
| 1831 |
var |
| 1832 |
i: integer; |
| 1833 |
begin |
| 1834 |
Result := 0; |
| 1835 |
for i := 0 to High(paths) do |
| 1836 |
Result := Result + Area(paths[i]); |
| 1837 |
end; |
| 1838 |
//------------------------------------------------------------------------------ |
| 1839 |
|
| 1840 |
function Area(const path: TPathD): Double; |
| 1841 |
var |
| 1842 |
i, highI: Integer; |
| 1843 |
p1,p2: PPointD; |
| 1844 |
begin |
| 1845 |
// https://en.wikipedia.org/wiki/Shoelace_formula |
| 1846 |
Result := 0.0; |
| 1847 |
highI := High(path); |
| 1848 |
if highI < 2 then Exit; |
| 1849 |
p1 := @path[highI]; |
| 1850 |
p2 := @path[0]; |
| 1851 |
for i := 0 to highI do |
| 1852 |
begin |
| 1853 |
Result := Result + (p1.Y + p2.Y) * (p1.X - p2.X); |
| 1854 |
p1 := p2; inc(p2); |
| 1855 |
end; |
| 1856 |
Result := Result * 0.5; |
| 1857 |
end; |
| 1858 |
//------------------------------------------------------------------------------ |
| 1859 |
|
| 1860 |
function Area(const paths: TPathsD): Double; |
| 1861 |
var |
| 1862 |
i: integer; |
| 1863 |
begin |
| 1864 |
Result := 0; |
| 1865 |
for i := 0 to High(paths) do |
| 1866 |
Result := Result + Area(paths[i]); |
| 1867 |
end; |
| 1868 |
//------------------------------------------------------------------------------ |
| 1869 |
|
| 1870 |
function IsPositive(const path: TPath64): Boolean; |
| 1871 |
begin |
| 1872 |
Result := (Area(path) >= 0); |
| 1873 |
end; |
| 1874 |
//------------------------------------------------------------------------------ |
| 1875 |
|
| 1876 |
function IsPositive(const path: TPathD): Boolean; |
| 1877 |
begin |
| 1878 |
Result := (Area(path) >= 0); |
| 1879 |
end; |
| 1880 |
//------------------------------------------------------------------------------ |
| 1881 |
|
| 1882 |
function TriSign(val: Int64): integer; // returns 0, 1 or -1 |
| 1883 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 1884 |
begin |
| 1885 |
if (val < 0) then Result := -1 |
| 1886 |
else if (val > 1) then Result := 1 |
| 1887 |
else Result := 0; |
| 1888 |
end; |
| 1889 |
//------------------------------------------------------------------------------ |
| 1890 |
|
| 1891 |
type |
| 1892 |
TMultiplyUInt64Result = record |
| 1893 |
lo64: UInt64; |
| 1894 |
hi64 : UInt64; |
| 1895 |
end; |
| 1896 |
|
| 1897 |
function MultiplyUInt64(a, b: UInt64): TMultiplyUInt64Result; // #834, #835 |
| 1898 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 1899 |
var |
| 1900 |
x1, x2, x3: UInt64; |
| 1901 |
begin |
| 1902 |
x1 := (a and $FFFFFFFF) * (b and $FFFFFFFF); |
| 1903 |
x2 := (a shr 32) * (b and $FFFFFFFF) + (x1 shr 32); |
| 1904 |
x3 := (a and $FFFFFFFF) * (b shr 32) + (x2 and $FFFFFFFF); |
| 1905 |
Result.lo64 := ((x3 and $FFFFFFFF) shl 32) or (x1 and $FFFFFFFF); |
| 1906 |
Result.hi64 := hi(a shr 32) * (b shr 32) + (x2 shr 32) + (x3 shr 32); |
| 1907 |
end; |
| 1908 |
//------------------------------------------------------------------------------ |
| 1909 |
|
| 1910 |
function ProductsAreEqual(a, b, c, d: Int64): Boolean; |
| 1911 |
var |
| 1912 |
absA,absB,absC,absD: UInt64; |
| 1913 |
absAB, absCD : TMultiplyUInt64Result; |
| 1914 |
signAB, signCD : integer; |
| 1915 |
begin |
| 1916 |
// nb: unsigned values will be needed for CalcOverflowCarry() |
| 1917 |
absA := UInt64(Abs(a)); |
| 1918 |
absB := UInt64(Abs(b)); |
| 1919 |
absC := UInt64(Abs(c)); |
| 1920 |
absD := UInt64(Abs(d)); |
| 1921 |
|
| 1922 |
absAB := MultiplyUInt64(absA, absB); |
| 1923 |
absCD := MultiplyUInt64(absC, absD); |
| 1924 |
|
| 1925 |
// nb: it's important to differentiate 0 values here from other values |
| 1926 |
signAB := TriSign(a) * TriSign(b); |
| 1927 |
signCD := TriSign(c) * TriSign(d); |
| 1928 |
|
| 1929 |
Result := (absAB.lo64 = absCD.lo64) and |
| 1930 |
(absAB.hi64 = absCD.hi64) and (signAB = signCD); |
| 1931 |
end; |
| 1932 |
//------------------------------------------------------------------------------ |
| 1933 |
|
| 1934 |
function IsCollinear(const pt1, sharedPt, pt2: TPoint64): Boolean; |
| 1935 |
var |
| 1936 |
a,b,c,d: Int64; |
| 1937 |
begin |
| 1938 |
a := sharedPt.X - pt1.X; |
| 1939 |
b := pt2.Y - sharedPt.Y; |
| 1940 |
c := sharedPt.Y - pt1.Y; |
| 1941 |
d := pt2.X - sharedPt.X; |
| 1942 |
// When checking for collinearity with very large coordinate values |
| 1943 |
// then ProductsAreEqual is more accurate than using CrossProduct. |
| 1944 |
Result := ProductsAreEqual(a, b, c, d); |
| 1945 |
end; |
| 1946 |
//------------------------------------------------------------------------------ |
| 1947 |
|
| 1948 |
function CrossProduct(const pt1, pt2, pt3: TPoint64): double; |
| 1949 |
begin |
| 1950 |
result := CrossProduct( |
| 1951 |
pt2.X - pt1.X, pt2.Y - pt1.Y, |
| 1952 |
pt3.X - pt2.X, pt3.Y - pt2.Y); |
| 1953 |
end; |
| 1954 |
//------------------------------------------------------------------------------ |
| 1955 |
|
| 1956 |
function CrossProduct(const pt1, pt2, pt3: TPointD): double; |
| 1957 |
begin |
| 1958 |
result := CrossProduct( |
| 1959 |
pt2.X - pt1.X, pt2.Y - pt1.Y, |
| 1960 |
pt3.X - pt2.X, pt3.Y - pt2.Y); |
| 1961 |
end; |
| 1962 |
//------------------------------------------------------------------------------ |
| 1963 |
|
| 1964 |
function CrossProduct(const vec1, vec2: TPointD): double; |
| 1965 |
begin |
| 1966 |
result := (vec1.X * vec2.Y - vec1.Y * vec2.X); |
| 1967 |
end; |
| 1968 |
//------------------------------------------------------------------------------ |
| 1969 |
|
| 1970 |
function CrossProduct(vec1x, vec1y, vec2x, vec2y: double): double; |
| 1971 |
begin |
| 1972 |
result := (vec1x * vec2y - vec1y * vec2x); |
| 1973 |
end; |
| 1974 |
//------------------------------------------------------------------------------ |
| 1975 |
|
| 1976 |
function DotProduct(const pt1, pt2, pt3: TPoint64): double; |
| 1977 |
var |
| 1978 |
x1,x2,y1,y2: double; // avoids potential int overflow |
| 1979 |
begin |
| 1980 |
x1 := pt2.X - pt1.X; |
| 1981 |
y1 := pt2.Y - pt1.Y; |
| 1982 |
x2 := pt3.X - pt2.X; |
| 1983 |
y2 := pt3.Y - pt2.Y; |
| 1984 |
result := (x1 * x2 + y1 * y2); |
| 1985 |
end; |
| 1986 |
//------------------------------------------------------------------------------ |
| 1987 |
|
| 1988 |
function SqrInt64(val: Int64): double; {$IFDEF INLINING} inline; {$ENDIF} |
| 1989 |
begin |
| 1990 |
Result := val; // force conversion |
| 1991 |
Result := Result * Result; |
| 1992 |
end; |
| 1993 |
//------------------------------------------------------------------------------ |
| 1994 |
|
| 1995 |
function DistanceSqr(const pt1, pt2: TPoint64): double; |
| 1996 |
begin |
| 1997 |
Result := SqrInt64(pt1.X - pt2.X) + SqrInt64(pt1.Y - pt2.Y); |
| 1998 |
end; |
| 1999 |
//------------------------------------------------------------------------------ |
| 2000 |
|
| 2001 |
function DistanceSqr(const pt1, pt2: TPointD): double; |
| 2002 |
begin |
| 2003 |
Result := Sqr(pt1.X - pt2.X) + Sqr(pt1.Y - pt2.Y); |
| 2004 |
end; |
| 2005 |
//------------------------------------------------------------------------------ |
| 2006 |
|
| 2007 |
function PerpendicDistFromLineSqrd(const pt, linePt1, linePt2: TPoint64): double; |
| 2008 |
var |
| 2009 |
a,b,c: double; |
| 2010 |
begin |
| 2011 |
// perpendicular distance of point (x0,y0) = (a*x0 + b*y0 + C)/Sqrt(a*a + b*b) |
| 2012 |
// where ax + by +c = 0 is the equation of the line |
| 2013 |
// see https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line |
| 2014 |
a := (linePt1.Y - linePt2.Y); |
| 2015 |
b := (linePt2.X - linePt1.X); |
| 2016 |
c := a * linePt1.X + b * linePt1.Y; |
| 2017 |
c := a * pt.x + b * pt.y - c; |
| 2018 |
if (a = 0) and (b = 0) then |
| 2019 |
Result := 0 else |
| 2020 |
Result := (c * c) / (a * a + b * b); |
| 2021 |
end; |
| 2022 |
//--------------------------------------------------------------------------- |
| 2023 |
|
| 2024 |
function PerpendicDistFromLineSqrd(const pt, linePt1, linePt2: TPointD): double; |
| 2025 |
var |
| 2026 |
a,b,c: double; |
| 2027 |
begin |
| 2028 |
a := (linePt1.Y - linePt2.Y); |
| 2029 |
b := (linePt2.X - linePt1.X); |
| 2030 |
c := a * linePt1.X + b * linePt1.Y; |
| 2031 |
c := a * pt.x + b * pt.y - c; |
| 2032 |
if (a = 0) and (b = 0) then |
| 2033 |
Result := 0 else |
| 2034 |
Result := (c * c) / (a * a + b * b); |
| 2035 |
end; |
| 2036 |
//--------------------------------------------------------------------------- |
| 2037 |
|
| 2038 |
function CleanPath(const path: TPath64): TPath64; |
| 2039 |
var |
| 2040 |
i,j, len: integer; |
| 2041 |
prev: TPoint64; |
| 2042 |
begin |
| 2043 |
Result := nil; |
| 2044 |
len := Length(path); |
| 2045 |
while (len > 2) and |
| 2046 |
(IsCollinear(path[len-2], path[len-1], path[0])) do dec(len); |
| 2047 |
SetLength(Result, len); |
| 2048 |
if (len < 2) then Exit; |
| 2049 |
prev := path[len -1]; |
| 2050 |
j := 0; |
| 2051 |
for i := 0 to len -2 do |
| 2052 |
begin |
| 2053 |
if IsCollinear(prev, path[i], path[i+1]) then Continue; |
| 2054 |
Result[j] := path[i]; |
| 2055 |
inc(j); |
| 2056 |
prev := path[i]; |
| 2057 |
end; |
| 2058 |
Result[j] := path[len -1]; |
| 2059 |
SetLength(Result, j+1); |
| 2060 |
end; |
| 2061 |
//------------------------------------------------------------------------------ |
| 2062 |
|
| 2063 |
function GetSign(const val: double): integer; {$IFDEF INLINING} inline; {$ENDIF} |
| 2064 |
begin |
| 2065 |
if val = 0 then Result := 0 |
| 2066 |
else if val < 0 then Result := -1 |
| 2067 |
else Result := 1; |
| 2068 |
end; |
| 2069 |
//------------------------------------------------------------------------------ |
| 2070 |
|
| 2071 |
function SegmentsIntersect(const s1a, s1b, s2a, s2b: TPoint64; |
| 2072 |
inclusive: Boolean): boolean; |
| 2073 |
var |
| 2074 |
res1, res2, res3, res4: double; |
| 2075 |
begin |
| 2076 |
if inclusive then |
| 2077 |
begin |
| 2078 |
//result can include segments that only touch |
| 2079 |
Result := false; |
| 2080 |
res1 := CrossProduct(s1a, s2a, s2b); |
| 2081 |
res2 := CrossProduct(s1b, s2a, s2b); |
| 2082 |
if (res1 * res2 > 0) then Exit; |
| 2083 |
res3 := CrossProduct(s2a, s1a, s1b); |
| 2084 |
res4 := CrossProduct(s2b, s1a, s1b); |
| 2085 |
if (res3 * res4 > 0) then Exit; |
| 2086 |
Result := (res1 <> 0) or (res2 <> 0) or |
| 2087 |
(res3 <> 0) or (res4 <> 0); // ensures not collinear |
| 2088 |
end else |
| 2089 |
begin |
| 2090 |
result := (GetSign(CrossProduct(s1a, s2a, s2b)) * |
| 2091 |
GetSign(CrossProduct(s1b, s2a, s2b)) < 0) and |
| 2092 |
(GetSign(CrossProduct(s2a, s1a, s1b)) * |
| 2093 |
GetSign(CrossProduct(s2b, s1a, s1b)) < 0); |
| 2094 |
end; |
| 2095 |
end; |
| 2096 |
//------------------------------------------------------------------------------ |
| 2097 |
|
| 2098 |
function GetSegmentIntersectPt(const ln1a, ln1b, ln2a, ln2b: TPoint64; |
| 2099 |
out ip: TPoint64): Boolean; |
| 2100 |
var |
| 2101 |
dx1,dy1, dx2,dy2, t, cp: double; |
| 2102 |
begin |
| 2103 |
// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection |
| 2104 |
dy1 := (ln1b.y - ln1a.y); |
| 2105 |
dx1 := (ln1b.x - ln1a.x); |
| 2106 |
dy2 := (ln2b.y - ln2a.y); |
| 2107 |
dx2 := (ln2b.x - ln2a.x); |
| 2108 |
cp := dy1 * dx2 - dy2 * dx1; |
| 2109 |
Result := (cp <> 0.0); |
| 2110 |
if not Result then Exit; |
| 2111 |
t := ((ln1a.x-ln2a.x) * dy2 - (ln1a.y-ln2a.y) * dx2) / cp; |
| 2112 |
if t <= 0.0 then ip := ln1a |
| 2113 |
else if t >= 1.0 then ip := ln1b; |
| 2114 |
ip.X := Trunc(ln1a.X + t * dx1); |
| 2115 |
ip.Y := Trunc(ln1a.Y + t * dy1); |
| 2116 |
{$IFDEF USINGZ} |
| 2117 |
ip.Z := 0; |
| 2118 |
{$ENDIF} |
| 2119 |
end; |
| 2120 |
//------------------------------------------------------------------------------ |
| 2121 |
|
| 2122 |
{$R-} |
| 2123 |
function PointInPolygon(const pt: TPoint64; |
| 2124 |
const polygon: TPath64): TPointInPolygonResult; |
| 2125 |
var |
| 2126 |
len, val: Integer; |
| 2127 |
isAbove, startingAbove: Boolean; |
| 2128 |
d: Double; // avoids integer overflow |
| 2129 |
curr, prev, cbegin, cend, first: PPoint64; |
| 2130 |
begin |
| 2131 |
result := pipOutside; |
| 2132 |
len := Length(polygon); |
| 2133 |
if len < 3 then Exit; |
| 2134 |
|
| 2135 |
cbegin := @polygon[0]; |
| 2136 |
cend := @polygon[len]; // stop is just past the last point (nb {$R-}) |
| 2137 |
|
| 2138 |
first := cbegin; |
| 2139 |
while (first <> cend) and (first.Y = pt.Y) do inc(first); |
| 2140 |
if (first = cend) then Exit; // not a proper polygon |
| 2141 |
|
| 2142 |
isAbove := first.Y < pt.Y; |
| 2143 |
startingAbove := isAbove; |
| 2144 |
Result := pipOn; |
| 2145 |
curr := first; |
| 2146 |
inc(curr); |
| 2147 |
val := 0; |
| 2148 |
while true do |
| 2149 |
begin |
| 2150 |
if (curr = cend) then |
| 2151 |
begin |
| 2152 |
if (cend = first) or (first = cbegin) then break; |
| 2153 |
cend := first; |
| 2154 |
curr := cbegin; |
| 2155 |
end; |
| 2156 |
|
| 2157 |
if isAbove then |
| 2158 |
begin |
| 2159 |
while (curr <> cend) and (curr.Y < pt.Y) do inc(curr); |
| 2160 |
if (curr = cend) then Continue; |
| 2161 |
end else |
| 2162 |
begin |
| 2163 |
while (curr <> cend) and (curr.Y > pt.Y) do inc(curr); |
| 2164 |
if (curr = cend) then Continue; |
| 2165 |
end; |
| 2166 |
|
| 2167 |
if curr = cbegin then |
| 2168 |
prev := @polygon[len] else // NOT cend! |
| 2169 |
prev := curr; |
| 2170 |
dec(prev); |
| 2171 |
|
| 2172 |
if (curr.Y = pt.Y) then |
| 2173 |
begin |
| 2174 |
if (curr.X = pt.X) or ((curr.Y = prev.Y) and |
| 2175 |
((pt.X < prev.X) <> (pt.X < curr.X))) then Exit; |
| 2176 |
inc(curr); |
| 2177 |
if (curr = first) then Break; |
| 2178 |
Continue; |
| 2179 |
end; |
| 2180 |
|
| 2181 |
if (pt.X < curr.X) and (pt.X < prev.X) then |
| 2182 |
// we're only interested in edges crossing on the left |
| 2183 |
else if((pt.X > prev.X) and (pt.X > curr.X)) then |
| 2184 |
val := 1 - val // toggle val |
| 2185 |
else |
| 2186 |
begin |
| 2187 |
d := CrossProduct(prev^, curr^, pt); |
| 2188 |
if d = 0 then Exit; // ie point on path |
| 2189 |
if (d < 0) = isAbove then val := 1 - val; |
| 2190 |
end; |
| 2191 |
|
| 2192 |
isAbove := not isAbove; |
| 2193 |
inc(curr); |
| 2194 |
end; |
| 2195 |
|
| 2196 |
if (isAbove <> startingAbove) then |
| 2197 |
begin |
| 2198 |
cend := @polygon[len]; |
| 2199 |
if (curr = cend) then curr := cbegin; |
| 2200 |
if curr = cbegin then |
| 2201 |
prev := cend else |
| 2202 |
prev := curr; |
| 2203 |
dec(prev); |
| 2204 |
d := CrossProduct(prev^, curr^, pt); |
| 2205 |
if d = 0 then Exit; // ie point on path |
| 2206 |
if (d < 0) = isAbove then val := 1 - val; |
| 2207 |
end; |
| 2208 |
|
| 2209 |
if val = 0 then |
| 2210 |
result := pipOutside else |
| 2211 |
result := pipInside; |
| 2212 |
end; |
| 2213 |
//------------------------------------------------------------------------------ |
| 2214 |
{$R+} |
| 2215 |
|
| 2216 |
procedure GetSinCos(angle: double; out sinA, cosA: double); |
| 2217 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 2218 |
{$IFNDEF FPC} |
| 2219 |
var s, c: extended; |
| 2220 |
{$ENDIF} |
| 2221 |
begin |
| 2222 |
{$IFDEF FPC} |
| 2223 |
Math.SinCos(angle, sinA, cosA); |
| 2224 |
{$ELSE} |
| 2225 |
Math.SinCos(angle, s, c); |
| 2226 |
sinA := s; cosA := c; |
| 2227 |
{$ENDIF} |
| 2228 |
end; |
| 2229 |
//------------------------------------------------------------------------------ |
| 2230 |
|
| 2231 |
function Ellipse(const rec: TRect64; steps: integer): TPath64; |
| 2232 |
begin |
| 2233 |
Result := Path64(Ellipse(RectD(rec), steps)); |
| 2234 |
end; |
| 2235 |
//------------------------------------------------------------------------------ |
| 2236 |
|
| 2237 |
function Ellipse(const rec: TRectD; steps: integer): TPathD; |
| 2238 |
var |
| 2239 |
i: Integer; |
| 2240 |
sinA, cosA: double; |
| 2241 |
centre, radius, delta: TPointD; |
| 2242 |
begin |
| 2243 |
result := nil; |
| 2244 |
if rec.IsEmpty then Exit; |
| 2245 |
with rec do |
| 2246 |
begin |
| 2247 |
centre := rec.MidPoint; |
| 2248 |
radius := PointD(Width * 0.5, Height * 0.5); |
| 2249 |
end; |
| 2250 |
if (steps < 3) then |
| 2251 |
steps := Ceil(PI * sqrt(rec.width + rec.height)); |
| 2252 |
GetSinCos(2 * Pi / Steps, sinA, cosA); |
| 2253 |
delta.x := cosA; delta.y := sinA; |
| 2254 |
SetLength(Result, Steps); |
| 2255 |
Result[0] := PointD(centre.X + radius.X, centre.Y); |
| 2256 |
for i := 1 to steps -1 do |
| 2257 |
begin |
| 2258 |
Result[i] := PointD(centre.X + radius.X * delta.x, |
| 2259 |
centre.Y + radius.y * delta.y); |
| 2260 |
delta := PointD(delta.X * cosA - delta.Y * sinA, |
| 2261 |
delta.Y * cosA + delta.X * sinA); |
| 2262 |
end; // rotates clockwise |
| 2263 |
end; |
| 2264 |
//------------------------------------------------------------------------------ |
| 2265 |
|
| 2266 |
function GetClosestPointOnSegment(const pt, seg1, seg2: TPoint64): TPoint64; |
| 2267 |
var |
| 2268 |
dx, dy, q: double; |
| 2269 |
begin |
| 2270 |
if (seg1.X = seg2.X) and (seg1.Y = seg2.Y) then |
| 2271 |
begin |
| 2272 |
Result := seg1; |
| 2273 |
Exit; |
| 2274 |
end; |
| 2275 |
dx := (seg2.X - seg1.X); |
| 2276 |
dy := (seg2.Y - seg1.Y); |
| 2277 |
q := ((pt.X - seg1.X) * dx + (pt.Y - seg1.Y) * dy) / (Sqr(dx) + Sqr(dy)); |
| 2278 |
if (q < 0) then q := 0 |
| 2279 |
else if (q > 1) then q := 1; |
| 2280 |
Result := Point64( |
| 2281 |
seg1.X + Round(q * dx), |
| 2282 |
seg1.Y + Round(q * dy)); |
| 2283 |
end; |
| 2284 |
//------------------------------------------------------------------------------ |
| 2285 |
|
| 2286 |
procedure RDP(const path: TPath64; startIdx, endIdx: integer; |
| 2287 |
epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload; |
| 2288 |
var |
| 2289 |
i, idx: integer; |
| 2290 |
d, maxD: double; |
| 2291 |
begin |
| 2292 |
idx := 0; |
| 2293 |
maxD := 0; |
| 2294 |
while (endIdx > startIdx) and |
| 2295 |
PointsEqual(path[startIdx], path[endIdx]) do |
| 2296 |
begin |
| 2297 |
boolArray[endIdx] := false; |
| 2298 |
dec(endIdx); |
| 2299 |
end; |
| 2300 |
for i := startIdx +1 to endIdx -1 do |
| 2301 |
begin |
| 2302 |
// PerpendicDistFromLineSqrd - avoids expensive Sqrt() |
| 2303 |
d := PerpendicDistFromLineSqrd(path[i], path[startIdx], path[endIdx]); |
| 2304 |
if d <= maxD then Continue; |
| 2305 |
maxD := d; |
| 2306 |
idx := i; |
| 2307 |
end; |
| 2308 |
if maxD < epsilonSqrd then Exit; |
| 2309 |
boolArray[idx] := true; |
| 2310 |
if idx > startIdx + 1 then RDP(path, startIdx, idx, epsilonSqrd, boolArray); |
| 2311 |
if endIdx > idx + 1 then RDP(path, idx, endIdx, epsilonSqrd, boolArray); |
| 2312 |
end; |
| 2313 |
//------------------------------------------------------------------------------ |
| 2314 |
|
| 2315 |
procedure RDP(const path: TPathD; startIdx, endIdx: integer; |
| 2316 |
epsilonSqrd: double; var boolArray: TArrayOfBoolean); overload; |
| 2317 |
var |
| 2318 |
i, idx: integer; |
| 2319 |
d, maxD: double; |
| 2320 |
begin |
| 2321 |
idx := 0; |
| 2322 |
maxD := 0; |
| 2323 |
while (endIdx > startIdx) and |
| 2324 |
PointsNearEqual(path[startIdx], path[endIdx]) do |
| 2325 |
begin |
| 2326 |
boolArray[endIdx] := false; |
| 2327 |
dec(endIdx); |
| 2328 |
end; |
| 2329 |
for i := startIdx +1 to endIdx -1 do |
| 2330 |
begin |
| 2331 |
// PerpendicDistFromLineSqrd - avoids expensive Sqrt() |
| 2332 |
d := PerpendicDistFromLineSqrd(path[i], path[startIdx], path[endIdx]); |
| 2333 |
if d <= maxD then Continue; |
| 2334 |
maxD := d; |
| 2335 |
idx := i; |
| 2336 |
end; |
| 2337 |
if maxD < epsilonSqrd then Exit; |
| 2338 |
boolArray[idx] := true; |
| 2339 |
if idx > startIdx + 1 then RDP(path, startIdx, idx, epsilonSqrd, boolArray); |
| 2340 |
if endIdx > idx + 1 then RDP(path, idx, endIdx, epsilonSqrd, boolArray); |
| 2341 |
end; |
| 2342 |
//------------------------------------------------------------------------------ |
| 2343 |
|
| 2344 |
function RamerDouglasPeucker(const path: TPath64; epsilon: double): TPath64; |
| 2345 |
var |
| 2346 |
i,j, len: integer; |
| 2347 |
boolArray: TArrayOfBoolean; |
| 2348 |
begin |
| 2349 |
len := length(path); |
| 2350 |
if len < 5 then |
| 2351 |
begin |
| 2352 |
result := Copy(path, 0, len); |
| 2353 |
Exit; |
| 2354 |
end; |
| 2355 |
SetLength(boolArray, len); // already zero initialized |
| 2356 |
boolArray[0] := true; |
| 2357 |
boolArray[len -1] := true; |
| 2358 |
RDP(path, 0, len -1, Sqr(epsilon), boolArray); |
| 2359 |
j := 0; |
| 2360 |
SetLength(Result, len); |
| 2361 |
for i := 0 to len -1 do |
| 2362 |
if boolArray[i] then |
| 2363 |
begin |
| 2364 |
Result[j] := path[i]; |
| 2365 |
inc(j); |
| 2366 |
end; |
| 2367 |
SetLength(Result, j); |
| 2368 |
end; |
| 2369 |
//------------------------------------------------------------------------------ |
| 2370 |
|
| 2371 |
function RamerDouglasPeucker(const paths: TPaths64; epsilon: double): TPaths64; |
| 2372 |
var |
| 2373 |
i, len: integer; |
| 2374 |
begin |
| 2375 |
len := Length(paths); |
| 2376 |
SetLength(Result, len); |
| 2377 |
for i := 0 to len -1 do |
| 2378 |
Result[i] := RamerDouglasPeucker(paths[i], epsilon); |
| 2379 |
end; |
| 2380 |
//------------------------------------------------------------------------------ |
| 2381 |
|
| 2382 |
function RamerDouglasPeucker(const path: TPathD; epsilon: double): TPathD; overload; |
| 2383 |
var |
| 2384 |
i,j, len: integer; |
| 2385 |
boolArray: TArrayOfBoolean; |
| 2386 |
begin |
| 2387 |
len := length(path); |
| 2388 |
if len < 5 then |
| 2389 |
begin |
| 2390 |
result := Copy(path, 0, len); |
| 2391 |
Exit; |
| 2392 |
end; |
| 2393 |
SetLength(boolArray, len); // already zero initialized |
| 2394 |
boolArray[0] := true; |
| 2395 |
boolArray[len -1] := true; |
| 2396 |
RDP(path, 0, len -1, Sqr(epsilon), boolArray); |
| 2397 |
j := 0; |
| 2398 |
SetLength(Result, len); |
| 2399 |
for i := 0 to len -1 do |
| 2400 |
if boolArray[i] then |
| 2401 |
begin |
| 2402 |
Result[j] := path[i]; |
| 2403 |
inc(j); |
| 2404 |
end; |
| 2405 |
SetLength(Result, j); |
| 2406 |
end; |
| 2407 |
//------------------------------------------------------------------------------ |
| 2408 |
|
| 2409 |
function RamerDouglasPeucker(const paths: TPathsD; epsilon: double): TPathsD; overload; |
| 2410 |
var |
| 2411 |
i, len: integer; |
| 2412 |
begin |
| 2413 |
len := Length(paths); |
| 2414 |
SetLength(Result, len); |
| 2415 |
for i := 0 to len -1 do |
| 2416 |
Result[i] := RamerDouglasPeucker(paths[i], epsilon); |
| 2417 |
end; |
| 2418 |
//------------------------------------------------------------------------------ |
| 2419 |
|
| 2420 |
end. |
| 2421 |
|