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 27 #ifndef FloatPoint_h 28 #define FloatPoint_h 29 30 #include "core/platform/graphics/FloatSize.h" 31 #include "core/platform/graphics/IntPoint.h" 32 #include "wtf/MathExtras.h" 33 34 #if OS(DARWIN) 35 typedef struct CGPoint CGPoint; 36 37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 38 typedef struct CGPoint NSPoint; 39 #else 40 typedef struct _NSPoint NSPoint; 41 #endif 42 #endif 43 44 struct SkPoint; 45 46 namespace WebCore { 47 48 class AffineTransform; 49 class TransformationMatrix; 50 class IntPoint; 51 class IntSize; 52 class LayoutPoint; 53 class LayoutSize; 54 55 class FloatPoint { 56 public: 57 FloatPoint() : m_x(0), m_y(0) { } 58 FloatPoint(float x, float y) : m_x(x), m_y(y) { } 59 FloatPoint(const IntPoint&); 60 FloatPoint(const LayoutPoint&); 61 explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.height()) { } 62 63 static FloatPoint zero() { return FloatPoint(); } 64 65 static FloatPoint narrowPrecision(double x, double y); 66 67 float x() const { return m_x; } 68 float y() const { return m_y; } 69 70 void setX(float x) { m_x = x; } 71 void setY(float y) { m_y = y; } 72 void set(float x, float y) 73 { 74 m_x = x; 75 m_y = y; 76 } 77 void move(float dx, float dy) 78 { 79 m_x += dx; 80 m_y += dy; 81 } 82 void move(const IntSize& a) 83 { 84 m_x += a.width(); 85 m_y += a.height(); 86 } 87 void move(const LayoutSize&); 88 void move(const FloatSize& a) 89 { 90 m_x += a.width(); 91 m_y += a.height(); 92 } 93 void moveBy(const IntPoint& a) 94 { 95 m_x += a.x(); 96 m_y += a.y(); 97 } 98 void moveBy(const LayoutPoint&); 99 void moveBy(const FloatPoint& a) 100 { 101 m_x += a.x(); 102 m_y += a.y(); 103 } 104 void scale(float sx, float sy) 105 { 106 m_x *= sx; 107 m_y *= sy; 108 } 109 110 void normalize(); 111 112 float dot(const FloatPoint& a) const 113 { 114 return m_x * a.x() + m_y * a.y(); 115 } 116 117 float slopeAngleRadians() const; 118 float length() const; 119 float lengthSquared() const 120 { 121 return m_x * m_x + m_y * m_y; 122 } 123 124 FloatPoint expandedTo(const FloatPoint& other) const 125 { 126 return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y)); 127 } 128 129 FloatPoint transposedPoint() const 130 { 131 return FloatPoint(m_y, m_x); 132 } 133 134 #if OS(DARWIN) 135 FloatPoint(const CGPoint&); 136 operator CGPoint() const; 137 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 138 FloatPoint(const NSPoint&); 139 operator NSPoint() const; 140 #endif 141 #endif 142 143 operator SkPoint() const; 144 145 FloatPoint matrixTransform(const TransformationMatrix&) const; 146 FloatPoint matrixTransform(const AffineTransform&) const; 147 148 private: 149 float m_x, m_y; 150 }; 151 152 153 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b) 154 { 155 a.move(b.width(), b.height()); 156 return a; 157 } 158 159 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b) 160 { 161 a.move(b.x(), b.y()); 162 return a; 163 } 164 165 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b) 166 { 167 a.move(-b.width(), -b.height()); 168 return a; 169 } 170 171 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b) 172 { 173 return FloatPoint(a.x() + b.width(), a.y() + b.height()); 174 } 175 176 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b) 177 { 178 return FloatPoint(a.x() + b.x(), a.y() + b.y()); 179 } 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 186 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b) 187 { 188 return FloatPoint(a.x() - b.width(), a.y() - b.height()); 189 } 190 191 inline FloatPoint operator-(const FloatPoint& a) 192 { 193 return FloatPoint(-a.x(), -a.y()); 194 } 195 196 inline bool operator==(const FloatPoint& a, const FloatPoint& b) 197 { 198 return a.x() == b.x() && a.y() == b.y(); 199 } 200 201 inline bool operator!=(const FloatPoint& a, const FloatPoint& b) 202 { 203 return a.x() != b.x() || a.y() != b.y(); 204 } 205 206 inline float operator*(const FloatPoint& a, const FloatPoint& b) 207 { 208 // dot product 209 return a.dot(b); 210 } 211 212 inline IntPoint roundedIntPoint(const FloatPoint& p) 213 { 214 return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y()))); 215 } 216 217 inline IntPoint flooredIntPoint(const FloatPoint& p) 218 { 219 return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); 220 } 221 222 inline IntPoint ceiledIntPoint(const FloatPoint& p) 223 { 224 return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y()))); 225 } 226 227 inline IntSize flooredIntSize(const FloatPoint& p) 228 { 229 return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); 230 } 231 232 inline FloatSize toFloatSize(const FloatPoint& a) 233 { 234 return FloatSize(a.x(), a.y()); 235 } 236 237 float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c); 238 239 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect. 240 bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection); 241 242 } 243 244 #endif 245