typshrd.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. function TPoint.Angle(const pt: TPoint): Single;
  119. function arctan2(y,x : Single) : Single;
  120. begin
  121. if x=0 then
  122. begin
  123. if y=0 then
  124. result:=0.0
  125. else if y>0 then
  126. result:=pi/2
  127. else
  128. result:=-pi/2;
  129. end
  130. else
  131. begin
  132. result:=ArcTan(y/x);
  133. if x<0 then
  134. if y<0 then
  135. result:=result-pi
  136. else
  137. result:=result+pi;
  138. end;
  139. end;
  140. begin
  141. result:=ArcTan2(y-pt.y,x-pt.x);
  142. end;
  143. class operator TPoint.= (const apt1, apt2 : TPoint) : Boolean;
  144. begin
  145. result:=(apt1.x=apt2.x) and (apt1.y=apt2.y);
  146. end;
  147. class operator TPoint.<> (const apt1, apt2 : TPoint): Boolean;
  148. begin
  149. result:=(apt1.x<>apt2.x) or (apt1.y<>apt2.y);
  150. end;
  151. class operator TPoint.+ (const apt1, apt2 : TPoint): TPoint;
  152. begin
  153. result.x:=apt1.x+apt2.x;
  154. result.y:=apt1.y+apt2.y;
  155. end;
  156. class operator TPoint.- (const apt1, apt2 : TPoint): TPoint;
  157. begin
  158. result.x:=apt1.x-apt2.x;
  159. result.y:=apt1.y-apt2.y;
  160. end;
  161. // warning suppression for the next ones?
  162. class operator TPoint.:= (const aspt : TSmallPoint): TPoint;
  163. begin
  164. result.x:=aspt.x;
  165. result.y:=aspt.y;
  166. end;
  167. class operator TPoint.Explicit (const apt: TPoint): TSmallPoint;
  168. begin
  169. result.x:=apt.x;
  170. result.y:=apt.y;
  171. end;
  172. { TRect }
  173. class operator TRect. * (L, R: TRect): TRect;
  174. begin
  175. Result := TRect.Intersect(L, R);
  176. end;
  177. class operator TRect. + (L, R: TRect): TRect;
  178. begin
  179. Result := TRect.Union(L, R);
  180. end;
  181. class operator TRect. <> (L, R: TRect): Boolean;
  182. begin
  183. Result := not(L=R);
  184. end;
  185. class operator TRect. = (L, R: TRect): Boolean;
  186. begin
  187. Result :=
  188. (L.Left = R.Left) and (L.Right = R.Right) and
  189. (L.Top = R.Top) and (L.Bottom = R.Bottom);
  190. end;
  191. constructor TRect.Create(ALeft, ATop, ARight, ABottom: Longint);
  192. begin
  193. Left := ALeft;
  194. Top := ATop;
  195. Right := ARight;
  196. Bottom := ABottom;
  197. end;
  198. constructor TRect.Create(P1, P2: TPoint; Normalize: Boolean);
  199. begin
  200. TopLeft := P1;
  201. BottomRight := P2;
  202. if Normalize then
  203. NormalizeRect;
  204. end;
  205. constructor TRect.Create(Origin: TPoint);
  206. begin
  207. TopLeft := Origin;
  208. BottomRight := Origin;
  209. end;
  210. constructor TRect.Create(Origin: TPoint; AWidth, AHeight: Longint);
  211. begin
  212. TopLeft := Origin;
  213. Width := AWidth;
  214. Height := AHeight;
  215. end;
  216. constructor TRect.Create(R: TRect; Normalize: Boolean);
  217. begin
  218. Self := R;
  219. if Normalize then
  220. NormalizeRect;
  221. end;
  222. function TRect.CenterPoint: TPoint;
  223. begin
  224. Result.X := (Right-Left) div 2 + Left;
  225. Result.Y := (Bottom-Top) div 2 + Top;
  226. end;
  227. function TRect.Contains(Pt: TPoint): Boolean;
  228. begin
  229. Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);
  230. end;
  231. function TRect.Contains(R: TRect): Boolean;
  232. begin
  233. Result := (Left <= R.Left) and (R.Right <= Right) and (Top <= R.Top) and (R.Bottom <= Bottom);
  234. end;
  235. class function TRect.Empty: TRect;
  236. begin
  237. Result := TRect.Create(0,0,0,0);
  238. end;
  239. function TRect.getHeight: Longint;
  240. begin
  241. result:=bottom-top;
  242. end;
  243. function TRect.getLocation: TPoint;
  244. begin
  245. result.x:=Left; result.y:=top;
  246. end;
  247. function TRect.getSize: TSize;
  248. begin
  249. result.cx:=width; result.cy:=height;
  250. end;
  251. function TRect.getWidth: Longint;
  252. begin
  253. result:=right-left;
  254. end;
  255. procedure TRect.Inflate(DX, DY: Longint);
  256. begin
  257. InflateRect(Self, DX, DY);
  258. end;
  259. procedure TRect.Intersect(R: TRect);
  260. begin
  261. Self := Intersect(Self, R);
  262. end;
  263. class function TRect.Intersect(R1: TRect; R2: TRect): TRect;
  264. begin
  265. IntersectRect(Result, R1, R2);
  266. end;
  267. function TRect.IntersectsWith(R: TRect): Boolean;
  268. begin
  269. Result := (Left < R.Right) and (R.Left < Right) and (Top < R.Bottom) and (R.Top < Bottom);
  270. end;
  271. function TRect.IsEmpty: Boolean;
  272. begin
  273. Result := (Right <= Left) or (Bottom <= Top);
  274. end;
  275. procedure TRect.NormalizeRect;
  276. var
  277. x: LongInt;
  278. begin
  279. if Top>Bottom then
  280. begin
  281. x := Top;
  282. Top := Bottom;
  283. Bottom := x;
  284. end;
  285. if Left>Right then
  286. begin
  287. x := Left;
  288. Left := Right;
  289. Right := x;
  290. end
  291. end;
  292. procedure TRect.Inflate(DL, DT, DR, DB: Longint);
  293. begin
  294. Dec(Left, DL);
  295. Dec(Top, DT);
  296. Inc(Right, DR);
  297. Inc(Bottom, DB);
  298. end;
  299. procedure TRect.Offset(DX, DY: Longint);
  300. begin
  301. OffsetRect(Self, DX, DY);
  302. end;
  303. procedure TRect.Offset(DP: TPoint);
  304. begin
  305. OffsetRect(Self, DP.X, DP.Y);
  306. end;
  307. procedure TRect.setHeight(AValue: Longint);
  308. begin
  309. bottom:=top+avalue;
  310. end;
  311. procedure TRect.SetLocation(X, Y: Longint);
  312. begin
  313. Offset(X-Left, Y-Top);
  314. end;
  315. procedure TRect.SetLocation(P: TPoint);
  316. begin
  317. SetLocation(P.X, P.Y);
  318. end;
  319. procedure TRect.setSize(AValue: TSize);
  320. begin
  321. bottom:=top+avalue.cy;
  322. right:=left+avalue.cx;
  323. end;
  324. procedure TRect.setWidth(AValue: Longint);
  325. begin
  326. right:=left+avalue;
  327. end;
  328. function TRect.SplitRect(SplitType: TSplitRectType; Percent: Double): TRect;
  329. begin
  330. Result := Self;
  331. case SplitType of
  332. srLeft: Result.Right := Left + Trunc(Percent*Width);
  333. srRight: Result.Left := Right - Trunc(Percent*Width);
  334. srTop: Result.Bottom := Top + Trunc(Percent*Height);
  335. srBottom: Result.Top := Bottom - Trunc(Percent*Height);
  336. end;
  337. end;
  338. function TRect.SplitRect(SplitType: TSplitRectType; ASize: Longint): TRect;
  339. begin
  340. Result := Self;
  341. case SplitType of
  342. srLeft: Result.Right := Left + ASize;
  343. srRight: Result.Left := Right - ASize;
  344. srTop: Result.Bottom := Top + ASize;
  345. srBottom: Result.Top := Bottom - ASize;
  346. end;
  347. end;
  348. class function TRect.Union(const Points: array of TPoint): TRect;
  349. var
  350. i: Integer;
  351. begin
  352. if Length(Points) > 0 then
  353. begin
  354. Result.TopLeft := Points[Low(Points)];
  355. Result.BottomRight := Points[Low(Points)];
  356. for i := Low(Points)+1 to High(Points) do
  357. begin
  358. if Points[i].X < Result.Left then Result.Left := Points[i].X;
  359. if Points[i].X > Result.Right then Result.Right := Points[i].X;
  360. if Points[i].Y < Result.Top then Result.Top := Points[i].Y;
  361. if Points[i].Y > Result.Bottom then Result.Bottom := Points[i].Y;
  362. end;
  363. end else
  364. Result := Empty;
  365. end;
  366. procedure TRect.Union(R: TRect);
  367. begin
  368. Self := Union(Self, R);
  369. end;
  370. class function TRect.Union(R1, R2: TRect): TRect;
  371. begin
  372. UnionRect(Result, R1, R2);
  373. end;