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