1 /* 2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. 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 27 #ifndef IntPoint_h 28 #define IntPoint_h 29 30 #include "platform/geometry/IntSize.h" 31 #include "wtf/MathExtras.h" 32 33 #if OS(MACOSX) 34 typedef struct CGPoint CGPoint; 35 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 36 typedef struct CGPoint NSPoint; 37 #else 38 typedef struct _NSPoint NSPoint; 39 #endif 40 #endif 41 42 namespace WebCore { 43 44 class PLATFORM_EXPORT IntPoint { 45 public: 46 IntPoint() : m_x(0), m_y(0) { } 47 IntPoint(int x, int y) : m_x(x), m_y(y) { } 48 explicit IntPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { } 49 50 static IntPoint zero() { return IntPoint(); } 51 52 int x() const { return m_x; } 53 int y() const { return m_y; } 54 55 void setX(int x) { m_x = x; } 56 void setY(int y) { m_y = y; } 57 58 void move(const IntSize& s) { move(s.width(), s.height()); } 59 void moveBy(const IntPoint& offset) { move(offset.x(), offset.y()); } 60 void move(int dx, int dy) { m_x += dx; m_y += dy; } 61 void scale(float sx, float sy) 62 { 63 m_x = lroundf(static_cast<float>(m_x * sx)); 64 m_y = lroundf(static_cast<float>(m_y * sy)); 65 } 66 67 IntPoint expandedTo(const IntPoint& other) const 68 { 69 return IntPoint(m_x > other.m_x ? m_x : other.m_x, 70 m_y > other.m_y ? m_y : other.m_y); 71 } 72 73 IntPoint shrunkTo(const IntPoint& other) const 74 { 75 return IntPoint(m_x < other.m_x ? m_x : other.m_x, 76 m_y < other.m_y ? m_y : other.m_y); 77 } 78 79 int distanceSquaredToPoint(const IntPoint&) const; 80 81 void clampNegativeToZero() 82 { 83 *this = expandedTo(zero()); 84 } 85 86 IntPoint transposedPoint() const 87 { 88 return IntPoint(m_y, m_x); 89 } 90 91 #if OS(MACOSX) 92 explicit IntPoint(const CGPoint&); // don't do this implicitly since it's lossy 93 operator CGPoint() const; 94 95 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 96 explicit IntPoint(const NSPoint&); // don't do this implicitly since it's lossy 97 operator NSPoint() const; 98 #endif 99 #endif 100 101 private: 102 int m_x, m_y; 103 }; 104 105 inline IntPoint& operator+=(IntPoint& a, const IntSize& b) 106 { 107 a.move(b.width(), b.height()); 108 return a; 109 } 110 111 inline IntPoint& operator-=(IntPoint& a, const IntSize& b) 112 { 113 a.move(-b.width(), -b.height()); 114 return a; 115 } 116 117 inline IntPoint operator+(const IntPoint& a, const IntSize& b) 118 { 119 return IntPoint(a.x() + b.width(), a.y() + b.height()); 120 } 121 122 inline IntPoint operator+(const IntPoint& a, const IntPoint& b) 123 { 124 return IntPoint(a.x() + b.x(), a.y() + b.y()); 125 } 126 127 inline IntSize operator-(const IntPoint& a, const IntPoint& b) 128 { 129 return IntSize(a.x() - b.x(), a.y() - b.y()); 130 } 131 132 inline IntPoint operator-(const IntPoint& a, const IntSize& b) 133 { 134 return IntPoint(a.x() - b.width(), a.y() - b.height()); 135 } 136 137 inline IntPoint operator-(const IntPoint& point) 138 { 139 return IntPoint(-point.x(), -point.y()); 140 } 141 142 inline bool operator==(const IntPoint& a, const IntPoint& b) 143 { 144 return a.x() == b.x() && a.y() == b.y(); 145 } 146 147 inline bool operator!=(const IntPoint& a, const IntPoint& b) 148 { 149 return a.x() != b.x() || a.y() != b.y(); 150 } 151 152 inline IntSize toIntSize(const IntPoint& a) 153 { 154 return IntSize(a.x(), a.y()); 155 } 156 157 inline int IntPoint::distanceSquaredToPoint(const IntPoint& point) const 158 { 159 return ((*this) - point).diagonalLengthSquared(); 160 } 161 162 } // namespace WebCore 163 164 #endif // IntPoint_h 165