PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/WebKit/Source/platform/geometry/FloatPoint.h

https://gitlab.com/0072016/Facebook-SDK-
C Header | 288 lines | 210 code | 48 blank | 30 comment | 7 complexity | 629e34fe660c079dd1b60ffa8efd79e0 MD5 | raw file
  1. /*
  2. * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2005 Nokia. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef FloatPoint_h
  27. #define FloatPoint_h
  28. #include "platform/geometry/FloatSize.h"
  29. #include "platform/geometry/IntPoint.h"
  30. #include "third_party/skia/include/core/SkPoint.h"
  31. #include "wtf/Allocator.h"
  32. #include "wtf/Forward.h"
  33. #include "wtf/MathExtras.h"
  34. #include <algorithm>
  35. #include <iosfwd>
  36. #if OS(MACOSX)
  37. typedef struct CGPoint CGPoint;
  38. #ifdef __OBJC__
  39. #import <Foundation/Foundation.h>
  40. #endif
  41. #endif
  42. namespace blink {
  43. class DoublePoint;
  44. class IntPoint;
  45. class IntSize;
  46. class LayoutPoint;
  47. class LayoutSize;
  48. class PLATFORM_EXPORT FloatPoint {
  49. DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
  50. public:
  51. FloatPoint() : m_x(0), m_y(0) { }
  52. FloatPoint(float x, float y) : m_x(x), m_y(y) { }
  53. FloatPoint(const IntPoint&);
  54. explicit FloatPoint(const SkPoint& point) : m_x(point.x()), m_y(point.y()) { }
  55. explicit FloatPoint(const DoublePoint&);
  56. explicit FloatPoint(const LayoutPoint&);
  57. explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.height()) { }
  58. explicit FloatPoint(const LayoutSize&);
  59. explicit FloatPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { }
  60. static FloatPoint zero() { return FloatPoint(); }
  61. static FloatPoint narrowPrecision(double x, double y);
  62. float x() const { return m_x; }
  63. float y() const { return m_y; }
  64. void setX(float x) { m_x = x; }
  65. void setY(float y) { m_y = y; }
  66. void set(float x, float y)
  67. {
  68. m_x = x;
  69. m_y = y;
  70. }
  71. void move(float dx, float dy)
  72. {
  73. m_x += dx;
  74. m_y += dy;
  75. }
  76. void move(const IntSize& a)
  77. {
  78. m_x += a.width();
  79. m_y += a.height();
  80. }
  81. void move(const LayoutSize&);
  82. void move(const FloatSize& a)
  83. {
  84. m_x += a.width();
  85. m_y += a.height();
  86. }
  87. void moveBy(const IntPoint& a)
  88. {
  89. m_x += a.x();
  90. m_y += a.y();
  91. }
  92. void moveBy(const LayoutPoint&);
  93. void moveBy(const FloatPoint& a)
  94. {
  95. m_x += a.x();
  96. m_y += a.y();
  97. }
  98. void scale(float sx, float sy)
  99. {
  100. m_x *= sx;
  101. m_y *= sy;
  102. }
  103. float dot(const FloatPoint& a) const
  104. {
  105. return m_x * a.x() + m_y * a.y();
  106. }
  107. float slopeAngleRadians() const;
  108. float length() const;
  109. float lengthSquared() const
  110. {
  111. return m_x * m_x + m_y * m_y;
  112. }
  113. FloatPoint expandedTo(const FloatPoint& other) const
  114. {
  115. return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
  116. }
  117. FloatPoint shrunkTo(const FloatPoint& other) const
  118. {
  119. return FloatPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y));
  120. }
  121. FloatPoint transposedPoint() const
  122. {
  123. return FloatPoint(m_y, m_x);
  124. }
  125. FloatPoint scaledBy(float scale) const
  126. {
  127. return FloatPoint(m_x * scale, m_y * scale);
  128. }
  129. #if OS(MACOSX)
  130. FloatPoint(const CGPoint&);
  131. operator CGPoint() const;
  132. #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
  133. FloatPoint(const NSPoint&);
  134. operator NSPoint() const;
  135. #endif
  136. #endif
  137. // Can we remove this one?
  138. SkPoint data() const;
  139. operator SkPoint() const { return SkPoint::Make(m_x, m_y); }
  140. #ifndef NDEBUG
  141. String toString() const;
  142. #endif
  143. private:
  144. float m_x, m_y;
  145. };
  146. inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
  147. {
  148. a.move(b.width(), b.height());
  149. return a;
  150. }
  151. inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b)
  152. {
  153. a.move(b.x(), b.y());
  154. return a;
  155. }
  156. inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
  157. {
  158. a.move(-b.width(), -b.height());
  159. return a;
  160. }
  161. inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
  162. {
  163. return FloatPoint(a.x() + b.width(), a.y() + b.height());
  164. }
  165. inline FloatPoint operator+(const FloatPoint& a, const IntSize& b)
  166. {
  167. return FloatPoint(a.x() + b.width(), a.y() + b.height());
  168. }
  169. inline FloatPoint operator+(const IntPoint& a, const FloatSize& b)
  170. {
  171. return FloatPoint(a.x() + b.width(), a.y() + b.height());
  172. }
  173. inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b)
  174. {
  175. return FloatPoint(a.x() + b.x(), a.y() + b.y());
  176. }
  177. inline FloatPoint operator+(const FloatPoint& a, const IntPoint& b)
  178. {
  179. return FloatPoint(a.x() + b.x(), a.y() + b.y());
  180. }
  181. inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
  182. {
  183. return FloatSize(a.x() - b.x(), a.y() - b.y());
  184. }
  185. inline FloatSize operator-(const FloatPoint& a, const IntPoint& b)
  186. {
  187. return FloatSize(a.x() - b.x(), a.y() - b.y());
  188. }
  189. inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
  190. {
  191. return FloatPoint(a.x() - b.width(), a.y() - b.height());
  192. }
  193. inline FloatPoint operator-(const FloatPoint& a)
  194. {
  195. return FloatPoint(-a.x(), -a.y());
  196. }
  197. inline bool operator==(const FloatPoint& a, const FloatPoint& b)
  198. {
  199. return a.x() == b.x() && a.y() == b.y();
  200. }
  201. inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
  202. {
  203. return a.x() != b.x() || a.y() != b.y();
  204. }
  205. inline float operator*(const FloatPoint& a, const FloatPoint& b)
  206. {
  207. // dot product
  208. return a.dot(b);
  209. }
  210. inline IntPoint roundedIntPoint(const FloatPoint& p)
  211. {
  212. return IntPoint(clampTo<int>(roundf(p.x())), clampTo<int>(roundf(p.y())));
  213. }
  214. inline IntSize roundedIntSize(const FloatPoint& p)
  215. {
  216. return IntSize(clampTo<int>(roundf(p.x())), clampTo<int>(roundf(p.y())));
  217. }
  218. inline IntPoint flooredIntPoint(const FloatPoint& p)
  219. {
  220. return IntPoint(clampTo<int>(floorf(p.x())), clampTo<int>(floorf(p.y())));
  221. }
  222. inline IntPoint ceiledIntPoint(const FloatPoint& p)
  223. {
  224. return IntPoint(clampTo<int>(ceilf(p.x())), clampTo<int>(ceilf(p.y())));
  225. }
  226. inline IntSize flooredIntSize(const FloatPoint& p)
  227. {
  228. return IntSize(clampTo<int>(floorf(p.x())), clampTo<int>(floorf(p.y())));
  229. }
  230. inline FloatSize toFloatSize(const FloatPoint& a)
  231. {
  232. return FloatSize(a.x(), a.y());
  233. }
  234. // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect.
  235. PLATFORM_EXPORT bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection);
  236. // Redeclared here to avoid ODR issues.
  237. // See platform/testing/GeometryPrinters.h.
  238. void PrintTo(const FloatPoint&, std::ostream*);
  239. } // namespace blink
  240. #endif