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