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 "FloatSize.h" 31 #include "IntPoint.h" 32 #include <wtf/MathExtras.h> 33 34 #if USE(CG) || USE(SKIA_ON_MAC_CHROME) 35 typedef struct CGPoint CGPoint; 36 #endif 37 38 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN)) 39 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 40 typedef struct CGPoint NSPoint; 41 #else 42 typedef struct _NSPoint NSPoint; 43 #endif 44 #endif 45 46 #if PLATFORM(QT) 47 #include "qglobal.h" 48 QT_BEGIN_NAMESPACE 49 class QPointF; 50 QT_END_NAMESPACE 51 #endif 52 53 #if PLATFORM(HAIKU) 54 class BPoint; 55 #endif 56 57 #if USE(SKIA) 58 struct SkPoint; 59 #endif 60 61 namespace WebCore { 62 63 class AffineTransform; 64 class TransformationMatrix; 65 class IntPoint; 66 67 class FloatPoint { 68 public: 69 FloatPoint() : m_x(0), m_y(0) { } 70 FloatPoint(float x, float y) : m_x(x), m_y(y) { } 71 FloatPoint(const IntPoint&); 72 73 74 static FloatPoint zero() { return FloatPoint(); } 75 76 static FloatPoint narrowPrecision(double x, double y); 77 78 float x() const { return m_x; } 79 float y() const { return m_y; } 80 81 void setX(float x) { m_x = x; } 82 void setY(float y) { m_y = y; } 83 void set(float x, float y) 84 { 85 m_x = x; 86 m_y = y; 87 } 88 void move(float dx, float dy) 89 { 90 m_x += dx; 91 m_y += dy; 92 } 93 void scale(float sx, float sy) 94 { 95 m_x *= sx; 96 m_y *= sy; 97 } 98 99 void normalize(); 100 101 float dot(const FloatPoint& a) const 102 { 103 return m_x * a.x() + m_y * a.y(); 104 } 105 106 float length() const; 107 float lengthSquared() const 108 { 109 return m_x * m_x + m_y * m_y; 110 } 111 112 #if USE(CG) || USE(SKIA_ON_MAC_CHROME) 113 FloatPoint(const CGPoint&); 114 operator CGPoint() const; 115 #endif 116 117 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \ 118 || (PLATFORM(CHROMIUM) && OS(DARWIN)) 119 FloatPoint(const NSPoint&); 120 operator NSPoint() const; 121 #endif 122 123 #if PLATFORM(QT) 124 FloatPoint(const QPointF&); 125 operator QPointF() const; 126 #endif 127 128 #if PLATFORM(HAIKU) 129 FloatPoint(const BPoint&); 130 operator BPoint() const; 131 #endif 132 133 #if USE(SKIA) 134 operator SkPoint() const; 135 FloatPoint(const SkPoint&); 136 #endif 137 138 FloatPoint matrixTransform(const TransformationMatrix&) const; 139 FloatPoint matrixTransform(const AffineTransform&) const; 140 141 private: 142 float m_x, m_y; 143 }; 144 145 146 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b) 147 { 148 a.move(b.width(), b.height()); 149 return a; 150 } 151 152 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b) 153 { 154 a.move(b.x(), b.y()); 155 return a; 156 } 157 158 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b) 159 { 160 a.move(-b.width(), -b.height()); 161 return a; 162 } 163 164 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b) 165 { 166 return FloatPoint(a.x() + b.width(), a.y() + b.height()); 167 } 168 169 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b) 170 { 171 return FloatPoint(a.x() + b.x(), a.y() + b.y()); 172 } 173 174 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b) 175 { 176 return FloatSize(a.x() - b.x(), a.y() - b.y()); 177 } 178 179 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b) 180 { 181 return FloatPoint(a.x() - b.width(), a.y() - b.height()); 182 } 183 184 inline bool operator==(const FloatPoint& a, const FloatPoint& b) 185 { 186 return a.x() == b.x() && a.y() == b.y(); 187 } 188 189 inline bool operator!=(const FloatPoint& a, const FloatPoint& b) 190 { 191 return a.x() != b.x() || a.y() != b.y(); 192 } 193 194 inline float operator*(const FloatPoint& a, const FloatPoint& b) 195 { 196 // dot product 197 return a.dot(b); 198 } 199 200 inline IntPoint roundedIntPoint(const FloatPoint& p) 201 { 202 return IntPoint(static_cast<int>(roundf(p.x())), static_cast<int>(roundf(p.y()))); 203 } 204 205 float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c); 206 207 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect. 208 bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection); 209 210 } 211 212 #endif 213