typshrd.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2015 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. Types that are in unit types on all platforms but also in
  6. unit Windows on win<x>
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. { TSize }
  14. {$ifdef VER3}
  15. constructor TSize.Create(ax,ay:Longint);
  16. begin
  17. cx:=ax; cy:=ay;
  18. end;
  19. constructor TSize.Create(asz :TSize);
  20. begin
  21. cx:=asz.cx; cy:=asz.cy;
  22. // vector:=TSize(asz.vector); ??
  23. end;
  24. {$endif}
  25. function TSize.IsZero : Boolean;
  26. begin
  27. result:=(cx=0) and (cy=0);
  28. end;
  29. function TSize.Distance(const asz : TSize) : Double;
  30. begin
  31. result:=sqrt(sqr(cx-asz.cx)+sqr(cy-asz.cy));
  32. end;
  33. function TSize.Add(const asz : TSize): TSize;
  34. begin
  35. result.cx:=cx+asz.cx;
  36. result.cy:=cy+asz.cy;
  37. end;
  38. function TSize.Subtract(const asz : TSize): TSize;
  39. begin
  40. result.cx:=cx-asz.cx;
  41. result.cy:=cy-asz.cy;
  42. end;
  43. class operator TSize.=(const asz1, asz2 : TSize) : Boolean;
  44. begin
  45. result:=(asz1.cx=asz2.cx) and (asz1.cy=asz2.cy);
  46. end;
  47. class operator TSize.<> (const asz1, asz2 : TSize): Boolean;
  48. begin
  49. result:=(asz1.cx<>asz2.cx) or (asz1.cy<>asz2.cy);
  50. end;
  51. class operator TSize.+(const asz1, asz2 : TSize): TSize;
  52. begin
  53. result.cx:=asz1.cx+asz2.cx;
  54. result.cy:=asz1.cy+asz2.cy;
  55. end;
  56. class operator TSize.-(const asz1, asz2 : TSize): TSize;
  57. begin
  58. result.cx:=asz1.cx-asz2.cx;
  59. result.cy:=asz1.cy-asz2.cy;
  60. end;
  61. { TPoint }
  62. {$ifdef VER3}
  63. constructor TPoint.Create(ax,ay:Longint);
  64. begin
  65. x:=ax; y:=ay;
  66. end;
  67. constructor TPoint.Create(apt :TPoint);
  68. begin
  69. x:=apt.x; y:=apt.y;
  70. end;
  71. {$endif}
  72. function TPoint.Add(const apt: TPoint): TPoint;
  73. begin
  74. result.x:=x+apt.x;
  75. result.y:=y+apt.y;
  76. end;
  77. function TPoint.Distance(const apt: TPoint): ValReal;
  78. begin
  79. result:=sqrt(sqr(ValReal(apt.x)-ValReal(x))+sqr(ValReal(apt.y)-ValReal(y))); // convert to ValReal to prevent integer overflows
  80. end;
  81. function TPoint.IsZero : Boolean;
  82. begin
  83. result:=(x=0) and (y=0);
  84. end;
  85. function TPoint.Subtract(const apt : TPoint): TPoint;
  86. begin
  87. result.x:=x-apt.x;
  88. result.y:=y-apt.y;
  89. end;
  90. class function TPoint.Zero: TPoint;
  91. begin
  92. Result.x := 0;
  93. Result.y := 0;
  94. end;
  95. procedure TPoint.SetLocation(const apt :TPoint);
  96. begin
  97. x:=apt.x; y:=apt.y;
  98. end;
  99. procedure TPoint.SetLocation(ax,ay : Longint);
  100. begin
  101. x:=ax; y:=ay;
  102. end;
  103. procedure TPoint.Offset(const apt :TPoint);
  104. begin
  105. x:=x+apt.x;
  106. y:=y+apt.y;
  107. end;
  108. class function TPoint.PointInCircle(const apt, acenter: TPoint;
  109. const aradius: Integer): Boolean;
  110. begin
  111. Result := apt.Distance(acenter) <= aradius;
  112. end;
  113. procedure TPoint.Offset(dx,dy : Longint);
  114. begin
  115. x:=x+dx;
  116. y:=y+dy;
  117. end;
  118. class operator TPoint.= (const apt1, apt2 : TPoint) : Boolean;
  119. begin
  120. result:=(apt1.x=apt2.x) and (apt1.y=apt2.y);
  121. end;
  122. class operator TPoint.<> (const apt1, apt2 : TPoint): Boolean;
  123. begin
  124. result:=(apt1.x<>apt2.x) or (apt1.y<>apt2.y);
  125. end;
  126. class operator TPoint.+ (const apt1, apt2 : TPoint): TPoint;
  127. begin
  128. result.x:=apt1.x+apt2.x;
  129. result.y:=apt1.y+apt2.y;
  130. end;
  131. class operator TPoint.- (const apt1, apt2 : TPoint): TPoint;
  132. begin
  133. result.x:=apt1.x-apt2.x;
  134. result.y:=apt1.y-apt2.y;
  135. end;
  136. // warning suppression for the next ones?
  137. class operator TPoint.:= (const aspt : TSmallPoint): TPoint;
  138. begin
  139. result.x:=aspt.x;
  140. result.y:=aspt.y;
  141. end;
  142. class operator TPoint.Explicit (const apt: TPoint): TSmallPoint;
  143. begin
  144. result.x:=apt.x;
  145. result.y:=apt.y;
  146. end;
  147. { TRect }
  148. class operator TRect. * (L, R: TRect): TRect;
  149. begin
  150. Result := TRect.Intersect(L, R);
  151. end;
  152. class operator TRect. + (L, R: TRect): TRect;
  153. begin
  154. Result := TRect.Union(L, R);
  155. end;
  156. class operator TRect. <> (L, R: TRect): Boolean;
  157. begin
  158. Result := not(L=R);
  159. end;
  160. class operator TRect. = (L, R: TRect): Boolean;
  161. begin
  162. Result :=
  163. (L.Left = R.Left) and (L.Right = R.Right) and
  164. (L.Top = R.Top) and (L.Bottom = R.Bottom);
  165. end;
  166. constructor TRect.Create(ALeft, ATop, ARight, ABottom: Longint);
  167. begin
  168. Left := ALeft;
  169. Top := ATop;
  170. Right := ARight;
  171. Bottom := ABottom;
  172. end;
  173. constructor TRect.Create(P1, P2: TPoint; Normalize: Boolean);
  174. begin
  175. TopLeft := P1;
  176. BottomRight := P2;
  177. if Normalize then
  178. NormalizeRect;
  179. end;
  180. constructor TRect.Create(Origin: TPoint);
  181. begin
  182. TopLeft := Origin;
  183. BottomRight := Origin;
  184. end;
  185. constructor TRect.Create(Origin: TPoint; AWidth, AHeight: Longint);
  186. begin
  187. TopLeft := Origin;
  188. Width := AWidth;
  189. Height := AHeight;
  190. end;
  191. constructor TRect.Create(R: TRect; Normalize: Boolean);
  192. begin
  193. Self := R;
  194. if Normalize then
  195. NormalizeRect;
  196. end;
  197. function TRect.CenterPoint: TPoint;
  198. begin
  199. Result.X := (Right-Left) div 2 + Left;
  200. Result.Y := (Bottom-Top) div 2 + Top;
  201. end;
  202. function TRect.Contains(Pt: TPoint): Boolean;
  203. begin
  204. Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);
  205. end;
  206. function TRect.Contains(R: TRect): Boolean;
  207. begin
  208. Result := (Left <= R.Left) and (R.Right <= Right) and (Top <= R.Top) and (R.Bottom <= Bottom);
  209. end;
  210. class function TRect.Empty: TRect;
  211. begin
  212. Result := TRect.Create(0,0,0,0);
  213. end;
  214. function TRect.getHeight: Longint;
  215. begin
  216. result:=bottom-top;
  217. end;
  218. function TRect.getLocation: TPoint;
  219. begin
  220. result.x:=Left; result.y:=top;
  221. end;
  222. function TRect.getSize: TSize;
  223. begin
  224. result.cx:=width; result.cy:=height;
  225. end;
  226. function TRect.getWidth: Longint;
  227. begin
  228. result:=right-left;
  229. end;
  230. procedure TRect.Inflate(DX, DY: Longint);
  231. begin
  232. InflateRect(Self, DX, DY);
  233. end;
  234. procedure TRect.Intersect(R: TRect);
  235. begin
  236. Self := Intersect(Self, R);
  237. end;
  238. class function TRect.Intersect(R1: TRect; R2: TRect): TRect;
  239. begin
  240. IntersectRect(Result, R1, R2);
  241. end;
  242. function TRect.IntersectsWith(R: TRect): Boolean;
  243. begin
  244. Result := (Left < R.Right) and (R.Left < Right) and (Top < R.Bottom) and (R.Top < Bottom);
  245. end;
  246. function TRect.IsEmpty: Boolean;
  247. begin
  248. Result := (Right <= Left) or (Bottom <= Top);
  249. end;
  250. procedure TRect.NormalizeRect;
  251. var
  252. x: LongInt;
  253. begin
  254. if Top>Bottom then
  255. begin
  256. x := Top;
  257. Top := Bottom;
  258. Bottom := x;
  259. end;
  260. if Left>Right then
  261. begin
  262. x := Left;
  263. Left := Right;
  264. Right := x;
  265. end
  266. end;
  267. procedure TRect.Inflate(DL, DT, DR, DB: Longint);
  268. begin
  269. Dec(Left, DL);
  270. Dec(Top, DT);
  271. Inc(Right, DR);
  272. Inc(Bottom, DB);
  273. end;
  274. procedure TRect.Offset(DX, DY: Longint);
  275. begin
  276. OffsetRect(Self, DX, DY);
  277. end;
  278. procedure TRect.Offset(DP: TPoint);
  279. begin
  280. OffsetRect(Self, DP.X, DP.Y);
  281. end;
  282. procedure TRect.setHeight(AValue: Longint);
  283. begin
  284. bottom:=top+avalue;
  285. end;
  286. procedure TRect.SetLocation(X, Y: Longint);
  287. begin
  288. Offset(X-Left, Y-Top);
  289. end;
  290. procedure TRect.SetLocation(P: TPoint);
  291. begin
  292. SetLocation(P.X, P.Y);
  293. end;
  294. procedure TRect.setSize(AValue: TSize);
  295. begin
  296. bottom:=top+avalue.cy;
  297. right:=left+avalue.cx;
  298. end;
  299. procedure TRect.setWidth(AValue: Longint);
  300. begin
  301. right:=left+avalue;
  302. end;
  303. function TRect.SplitRect(SplitType: TSplitRectType; Percent: Double): TRect;
  304. begin
  305. Result := Self;
  306. case SplitType of
  307. srLeft: Result.Right := Left + Trunc(Percent*Width);
  308. srRight: Result.Left := Right - Trunc(Percent*Width);
  309. srTop: Result.Bottom := Top + Trunc(Percent*Height);
  310. srBottom: Result.Top := Bottom - Trunc(Percent*Height);
  311. end;
  312. end;
  313. function TRect.SplitRect(SplitType: TSplitRectType; ASize: Longint): TRect;
  314. begin
  315. Result := Self;
  316. case SplitType of
  317. srLeft: Result.Right := Left + ASize;
  318. srRight: Result.Left := Right - ASize;
  319. srTop: Result.Bottom := Top + ASize;
  320. srBottom: Result.Top := Bottom - ASize;
  321. end;
  322. end;
  323. class function TRect.Union(const Points: array of TPoint): TRect;
  324. var
  325. i: Integer;
  326. begin
  327. if Length(Points) > 0 then
  328. begin
  329. Result.TopLeft := Points[Low(Points)];
  330. Result.BottomRight := Points[Low(Points)];
  331. for i := Low(Points)+1 to High(Points) do
  332. begin
  333. if Points[i].X < Result.Left then Result.Left := Points[i].X;
  334. if Points[i].X > Result.Right then Result.Right := Points[i].X;
  335. if Points[i].Y < Result.Top then Result.Top := Points[i].Y;
  336. if Points[i].Y > Result.Bottom then Result.Bottom := Points[i].Y;
  337. end;
  338. end else
  339. Result := Empty;
  340. end;
  341. procedure TRect.Union(R: TRect);
  342. begin
  343. Self := Union(Self, R);
  344. end;
  345. class function TRect.Union(R1, R2: TRect): TRect;
  346. begin
  347. UnionRect(Result, R1, R2);
  348. end;