Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

743 lines
14 KiB

  1. /***************************************************************************\
  2. *
  3. * File: Geom2D.h
  4. *
  5. * Description:
  6. * Geom2D defines a standard lightweight objects for 2D space, including
  7. * size, point, and rect.
  8. *
  9. *
  10. * History:
  11. * 2/10/2001: JStall: Copied from \windows\AdvCore\Gdiplus\sdkinc\GdiplusTypes.h
  12. *
  13. * Copyright (C) 2001 by Microsoft Corporation. All rights reserved.
  14. *
  15. \***************************************************************************/
  16. #if !defined(DUSERX___Types_h__INCLUDED)
  17. #define DUSERX___Types_h__INCLUDED
  18. #pragma once
  19. namespace DirectUser
  20. {
  21. namespace Geometry
  22. {
  23. typedef float REAL;
  24. //--------------------------------------------------------------------------
  25. // Primitive data types
  26. //
  27. // NOTE:
  28. // Types already defined in standard header files:
  29. // INT8
  30. // UINT8
  31. // INT16
  32. // UINT16
  33. // INT32
  34. // UINT32
  35. // INT64
  36. // UINT64
  37. //
  38. // Avoid using the following types:
  39. // LONG - use INT
  40. // ULONG - use UINT
  41. // DWORD - use UINT32
  42. //--------------------------------------------------------------------------
  43. // Forward declarations
  44. class Size;
  45. class SizeF;
  46. class Point;
  47. class PointF;
  48. class Rect;
  49. class RectF;
  50. //--------------------------------------------------------------------------
  51. // Represents a dimension in a 2D coordinate system
  52. // (floating-point coordinates)
  53. //--------------------------------------------------------------------------
  54. class SizeF
  55. {
  56. public:
  57. // Default constructor
  58. SizeF()
  59. {
  60. }
  61. SizeF(bool fInit)
  62. {
  63. if (fInit) {
  64. Width = Height = 0.0f;
  65. }
  66. }
  67. SizeF(IN const SizeF& size)
  68. {
  69. Width = size.Width;
  70. Height = size.Height;
  71. }
  72. SizeF(IN REAL width,
  73. IN REAL height)
  74. {
  75. Width = width;
  76. Height = height;
  77. }
  78. SizeF operator+(IN const SizeF& sz) const
  79. {
  80. return SizeF(Width + sz.Width,
  81. Height + sz.Height);
  82. }
  83. SizeF operator-(IN const SizeF& sz) const
  84. {
  85. return SizeF(Width - sz.Width,
  86. Height - sz.Height);
  87. }
  88. BOOL Equals(IN const SizeF& sz) const
  89. {
  90. return (Width == sz.Width) && (Height == sz.Height);
  91. }
  92. BOOL Empty() const
  93. {
  94. return (Width == 0.0f && Height == 0.0f);
  95. }
  96. public:
  97. REAL Width;
  98. REAL Height;
  99. };
  100. //--------------------------------------------------------------------------
  101. // Represents a dimension in a 2D coordinate system
  102. // (integer coordinates)
  103. //--------------------------------------------------------------------------
  104. class Size
  105. {
  106. public:
  107. // Default constructor
  108. Size()
  109. {
  110. }
  111. Size(bool fInit)
  112. {
  113. if (fInit) {
  114. Width = Height = 0;
  115. }
  116. }
  117. Size(IN const Size& size)
  118. {
  119. Width = size.Width;
  120. Height = size.Height;
  121. }
  122. Size(IN INT width,
  123. IN INT height)
  124. {
  125. Width = width;
  126. Height = height;
  127. }
  128. Size operator+(IN const Size& sz) const
  129. {
  130. return Size(Width + sz.Width,
  131. Height + sz.Height);
  132. }
  133. Size operator-(IN const Size& sz) const
  134. {
  135. return Size(Width - sz.Width,
  136. Height - sz.Height);
  137. }
  138. BOOL Equals(IN const Size& sz) const
  139. {
  140. return (Width == sz.Width) && (Height == sz.Height);
  141. }
  142. BOOL Empty() const
  143. {
  144. return (Width == 0 && Height == 0);
  145. }
  146. public:
  147. INT Width;
  148. INT Height;
  149. };
  150. //--------------------------------------------------------------------------
  151. // Represents a location in a 2D coordinate system
  152. // (floating-point coordinates)
  153. //--------------------------------------------------------------------------
  154. class PointF
  155. {
  156. public:
  157. PointF()
  158. {
  159. }
  160. PointF(bool fInit)
  161. {
  162. if (fInit) {
  163. X = Y = 0.0f;
  164. }
  165. }
  166. PointF(IN const PointF &point)
  167. {
  168. X = point.X;
  169. Y = point.Y;
  170. }
  171. PointF(IN const SizeF &size)
  172. {
  173. X = size.Width;
  174. Y = size.Height;
  175. }
  176. PointF(IN REAL x,
  177. IN REAL y)
  178. {
  179. X = x;
  180. Y = y;
  181. }
  182. PointF operator+(IN const PointF& point) const
  183. {
  184. return PointF(X + point.X,
  185. Y + point.Y);
  186. }
  187. PointF operator-(IN const PointF& point) const
  188. {
  189. return PointF(X - point.X,
  190. Y - point.Y);
  191. }
  192. BOOL Equals(IN const PointF& point)
  193. {
  194. return (X == point.X) && (Y == point.Y);
  195. }
  196. public:
  197. REAL X;
  198. REAL Y;
  199. };
  200. //--------------------------------------------------------------------------
  201. // Represents a location in a 2D coordinate system
  202. // (integer coordinates)
  203. //--------------------------------------------------------------------------
  204. class Point
  205. {
  206. public:
  207. Point()
  208. {
  209. }
  210. Point(bool fInit)
  211. {
  212. if (fInit) {
  213. X = Y = 0;
  214. }
  215. }
  216. Point(IN const Point &point)
  217. {
  218. X = point.X;
  219. Y = point.Y;
  220. }
  221. Point(IN const Size &size)
  222. {
  223. X = size.Width;
  224. Y = size.Height;
  225. }
  226. Point(IN INT x,
  227. IN INT y)
  228. {
  229. X = x;
  230. Y = y;
  231. }
  232. Point operator+(IN const Point& point) const
  233. {
  234. return Point(X + point.X,
  235. Y + point.Y);
  236. }
  237. Point operator-(IN const Point& point) const
  238. {
  239. return Point(X - point.X,
  240. Y - point.Y);
  241. }
  242. BOOL Equals(IN const Point& point)
  243. {
  244. return (X == point.X) && (Y == point.Y);
  245. }
  246. public:
  247. INT X;
  248. INT Y;
  249. };
  250. //--------------------------------------------------------------------------
  251. // Represents a rectangle in a 2D coordinate system
  252. // (floating-point coordinates)
  253. //--------------------------------------------------------------------------
  254. class RectF
  255. {
  256. public:
  257. // Default constructor
  258. RectF()
  259. {
  260. }
  261. RectF(bool fInit)
  262. {
  263. if (fInit) {
  264. X = Y = Width = Height = 0.0f;
  265. }
  266. }
  267. RectF(IN REAL x,
  268. IN REAL y,
  269. IN REAL width,
  270. IN REAL height)
  271. {
  272. X = x;
  273. Y = y;
  274. Width = width;
  275. Height = height;
  276. }
  277. RectF(IN const PointF& location,
  278. IN const SizeF& size)
  279. {
  280. X = location.X;
  281. Y = location.Y;
  282. Width = size.Width;
  283. Height = size.Height;
  284. }
  285. RectF* Clone() const
  286. {
  287. return new RectF(X, Y, Width, Height);
  288. }
  289. VOID GetLocation(OUT PointF* point) const
  290. {
  291. point->X = X;
  292. point->Y = Y;
  293. }
  294. VOID GetSize(OUT SizeF* size) const
  295. {
  296. size->Width = Width;
  297. size->Height = Height;
  298. }
  299. VOID GetBounds(OUT RectF* rect) const
  300. {
  301. rect->X = X;
  302. rect->Y = Y;
  303. rect->Width = Width;
  304. rect->Height = Height;
  305. }
  306. // Return the left, top, right, and bottom
  307. // coordinates of the rectangle
  308. REAL GetLeft() const
  309. {
  310. return X;
  311. }
  312. void SetLeft(REAL x)
  313. {
  314. X = x;
  315. }
  316. REAL GetTop() const
  317. {
  318. return Y;
  319. }
  320. void SetTop(REAL y)
  321. {
  322. Y = y;
  323. }
  324. REAL GetRight() const
  325. {
  326. return X+Width;
  327. }
  328. REAL GetBottom() const
  329. {
  330. return Y+Height;
  331. }
  332. // Determine if the rectangle is empty
  333. BOOL IsEmptyArea() const
  334. {
  335. REAL epsilon = 1.192092896e-07F; /* FLT_EPSILON */
  336. return (Width <= epsilon) || (Height <= epsilon);
  337. }
  338. BOOL Equals(IN const RectF & rect) const
  339. {
  340. return X == rect.X &&
  341. Y == rect.Y &&
  342. Width == rect.Width &&
  343. Height == rect.Height;
  344. }
  345. BOOL Contains(IN REAL x,
  346. IN REAL y) const
  347. {
  348. return x >= X && x < X+Width &&
  349. y >= Y && y < Y+Height;
  350. }
  351. BOOL Contains(IN const PointF& pt) const
  352. {
  353. return Contains(pt.X, pt.Y);
  354. }
  355. BOOL Contains(IN const RectF& rect) const
  356. {
  357. return (X <= rect.X) && (rect.GetRight() <= GetRight()) &&
  358. (Y <= rect.Y) && (rect.GetBottom() <= GetBottom());
  359. }
  360. VOID Inflate(IN REAL dx,
  361. IN REAL dy)
  362. {
  363. X -= dx;
  364. Y -= dy;
  365. Width += 2*dx;
  366. Height += 2*dy;
  367. }
  368. VOID Inflate(IN const PointF& point)
  369. {
  370. Inflate(point.X, point.Y);
  371. }
  372. // Intersect the current rect with the specified object
  373. BOOL Intersect(IN const RectF& rect)
  374. {
  375. return Intersect(*this, *this, rect);
  376. }
  377. // Intersect rect a and b and save the result into c
  378. // Notice that c may be the same object as a or b.
  379. static BOOL Intersect(OUT RectF& c,
  380. IN const RectF& a,
  381. IN const RectF& b)
  382. {
  383. REAL right = min(a.GetRight(), b.GetRight());
  384. REAL bottom = min(a.GetBottom(), b.GetBottom());
  385. REAL left = max(a.GetLeft(), b.GetLeft());
  386. REAL top = max(a.GetTop(), b.GetTop());
  387. c.X = left;
  388. c.Y = top;
  389. c.Width = right - left;
  390. c.Height = bottom - top;
  391. return !c.IsEmptyArea();
  392. }
  393. // Determine if the specified rect intersects with the
  394. // current rect object.
  395. BOOL IntersectsWith(IN const RectF& rect) const
  396. {
  397. return (GetLeft() < rect.GetRight() &&
  398. GetTop() < rect.GetBottom() &&
  399. GetRight() > rect.GetLeft() &&
  400. GetBottom() > rect.GetTop());
  401. }
  402. static BOOL Union(OUT RectF& c,
  403. IN const RectF& a,
  404. IN const RectF& b)
  405. {
  406. REAL right = max(a.GetRight(), b.GetRight());
  407. REAL bottom = max(a.GetBottom(), b.GetBottom());
  408. REAL left = min(a.GetLeft(), b.GetLeft());
  409. REAL top = min(a.GetTop(), b.GetTop());
  410. c.X = left;
  411. c.Y = top;
  412. c.Width = right - left;
  413. c.Height = bottom - top;
  414. return !c.IsEmptyArea();
  415. }
  416. VOID Offset(IN const PointF& point)
  417. {
  418. Offset(point.X, point.Y);
  419. }
  420. VOID Offset(IN REAL dx,
  421. IN REAL dy)
  422. {
  423. X += dx;
  424. Y += dy;
  425. }
  426. public:
  427. REAL X;
  428. REAL Y;
  429. REAL Width;
  430. REAL Height;
  431. };
  432. //--------------------------------------------------------------------------
  433. // Represents a rectangle in a 2D coordinate system
  434. // (integer coordinates)
  435. //--------------------------------------------------------------------------
  436. class Rect
  437. {
  438. public:
  439. // Default constructor
  440. Rect()
  441. {
  442. }
  443. Rect(bool fInit)
  444. {
  445. if (fInit) {
  446. X = Y = Width = Height = 0;
  447. }
  448. }
  449. Rect(IN INT x,
  450. IN INT y,
  451. IN INT width,
  452. IN INT height)
  453. {
  454. X = x;
  455. Y = y;
  456. Width = width;
  457. Height = height;
  458. }
  459. Rect(IN const Point& location,
  460. IN const Size& size)
  461. {
  462. X = location.X;
  463. Y = location.Y;
  464. Width = size.Width;
  465. Height = size.Height;
  466. }
  467. Rect* Clone() const
  468. {
  469. return new Rect(X, Y, Width, Height);
  470. }
  471. VOID GetLocation(OUT Point* point) const
  472. {
  473. point->X = X;
  474. point->Y = Y;
  475. }
  476. VOID GetSize(OUT Size* size) const
  477. {
  478. size->Width = Width;
  479. size->Height = Height;
  480. }
  481. VOID GetBounds(OUT Rect* rect) const
  482. {
  483. rect->X = X;
  484. rect->Y = Y;
  485. rect->Width = Width;
  486. rect->Height = Height;
  487. }
  488. // Return the left, top, right, and bottom
  489. // coordinates of the rectangle
  490. INT GetLeft() const
  491. {
  492. return X;
  493. }
  494. INT GetTop() const
  495. {
  496. return Y;
  497. }
  498. INT GetRight() const
  499. {
  500. return X+Width;
  501. }
  502. INT GetBottom() const
  503. {
  504. return Y+Height;
  505. }
  506. // Determine if the rectangle is empty
  507. BOOL IsEmptyArea() const
  508. {
  509. return (Width <= 0) || (Height <= 0);
  510. }
  511. BOOL Equals(IN const Rect & rect) const
  512. {
  513. return X == rect.X &&
  514. Y == rect.Y &&
  515. Width == rect.Width &&
  516. Height == rect.Height;
  517. }
  518. BOOL Contains(IN INT x,
  519. IN INT y) const
  520. {
  521. return x >= X && x < X+Width &&
  522. y >= Y && y < Y+Height;
  523. }
  524. BOOL Contains(IN const Point& pt) const
  525. {
  526. return Contains(pt.X, pt.Y);
  527. }
  528. BOOL Contains(IN Rect& rect) const
  529. {
  530. return (X <= rect.X) && (rect.GetRight() <= GetRight()) &&
  531. (Y <= rect.Y) && (rect.GetBottom() <= GetBottom());
  532. }
  533. VOID Inflate(IN INT dx,
  534. IN INT dy)
  535. {
  536. X -= dx;
  537. Y -= dy;
  538. Width += 2*dx;
  539. Height += 2*dy;
  540. }
  541. VOID Inflate(IN const Point& point)
  542. {
  543. Inflate(point.X, point.Y);
  544. }
  545. // Intersect the current rect with the specified object
  546. BOOL Intersect(IN const Rect& rect)
  547. {
  548. return Intersect(*this, *this, rect);
  549. }
  550. // Intersect rect a and b and save the result into c
  551. // Notice that c may be the same object as a or b.
  552. static BOOL Intersect(OUT Rect& c,
  553. IN const Rect& a,
  554. IN const Rect& b)
  555. {
  556. INT right = min(a.GetRight(), b.GetRight());
  557. INT bottom = min(a.GetBottom(), b.GetBottom());
  558. INT left = max(a.GetLeft(), b.GetLeft());
  559. INT top = max(a.GetTop(), b.GetTop());
  560. c.X = left;
  561. c.Y = top;
  562. c.Width = right - left;
  563. c.Height = bottom - top;
  564. return !c.IsEmptyArea();
  565. }
  566. // Determine if the specified rect intersects with the
  567. // current rect object.
  568. BOOL IntersectsWith(IN const Rect& rect) const
  569. {
  570. return (GetLeft() < rect.GetRight() &&
  571. GetTop() < rect.GetBottom() &&
  572. GetRight() > rect.GetLeft() &&
  573. GetBottom() > rect.GetTop());
  574. }
  575. static BOOL Union(OUT Rect& c,
  576. IN const Rect& a,
  577. IN const Rect& b)
  578. {
  579. INT right = max(a.GetRight(), b.GetRight());
  580. INT bottom = max(a.GetBottom(), b.GetBottom());
  581. INT left = min(a.GetLeft(), b.GetLeft());
  582. INT top = min(a.GetTop(), b.GetTop());
  583. c.X = left;
  584. c.Y = top;
  585. c.Width = right - left;
  586. c.Height = bottom - top;
  587. return !c.IsEmptyArea();
  588. }
  589. VOID Offset(IN const Point& point)
  590. {
  591. Offset(point.X, point.Y);
  592. }
  593. VOID Offset(IN INT dx,
  594. IN INT dy)
  595. {
  596. X += dx;
  597. Y += dy;
  598. }
  599. public:
  600. INT X;
  601. INT Y;
  602. INT Width;
  603. INT Height;
  604. };
  605. } // namespace Geometry
  606. } // namespace DirectUser
  607. #endif // DUSERX___Types_h__INCLUDED