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 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 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) 35typedef struct CGPoint CGPoint; 36#endif 37 38#if PLATFORM(MAC) 39#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 40typedef struct CGPoint NSPoint; 41#else 42typedef struct _NSPoint NSPoint; 43#endif 44#endif // PLATFORM(MAC) 45 46namespace WebCore { 47 48class AffineTransform; 49class TransformationMatrix; 50class IntPoint; 51class IntSize; 52 53class FloatPoint { 54public: 55 FloatPoint() : m_x(0), m_y(0) { } 56 FloatPoint(float x, float y) : m_x(x), m_y(y) { } 57 FloatPoint(const IntPoint&); 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 FloatSize& a) 85 { 86 m_x += a.width(); 87 m_y += a.height(); 88 } 89 void moveBy(const IntPoint& a) 90 { 91 m_x += a.x(); 92 m_y += a.y(); 93 } 94 void moveBy(const FloatPoint& a) 95 { 96 m_x += a.x(); 97 m_y += a.y(); 98 } 99 void scale(float sx, float sy) 100 { 101 m_x *= sx; 102 m_y *= sy; 103 } 104 105 void normalize(); 106 107 float dot(const FloatPoint& a) const 108 { 109 return m_x * a.x() + m_y * a.y(); 110 } 111 112 float slopeAngleRadians() const; 113 float length() const; 114 float lengthSquared() const 115 { 116 return m_x * m_x + m_y * m_y; 117 } 118 119 FloatPoint shrunkTo(const FloatPoint& other) const 120 { 121 return FloatPoint(std::min(m_x, other.m_x), std::min(m_y, other.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 USE(CG) 135 FloatPoint(const CGPoint&); 136 operator CGPoint() const; 137#endif 138 139#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 140 FloatPoint(const NSPoint&); 141 operator NSPoint() const; 142#endif 143 144 FloatPoint matrixTransform(const TransformationMatrix&) const; 145 FloatPoint matrixTransform(const AffineTransform&) const; 146 147 void dump(WTF::PrintStream& out) const; 148 149private: 150 float m_x, m_y; 151}; 152 153 154inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b) 155{ 156 a.move(b.width(), b.height()); 157 return a; 158} 159 160inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b) 161{ 162 a.move(b.x(), b.y()); 163 return a; 164} 165 166inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b) 167{ 168 a.move(-b.width(), -b.height()); 169 return a; 170} 171 172inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b) 173{ 174 return FloatPoint(a.x() + b.width(), a.y() + b.height()); 175} 176 177inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b) 178{ 179 return FloatPoint(a.x() + b.x(), a.y() + b.y()); 180} 181 182inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b) 183{ 184 return FloatSize(a.x() - b.x(), a.y() - b.y()); 185} 186 187inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b) 188{ 189 return FloatPoint(a.x() - b.width(), a.y() - b.height()); 190} 191 192inline FloatPoint operator-(const FloatPoint& a) 193{ 194 return FloatPoint(-a.x(), -a.y()); 195} 196 197inline bool operator==(const FloatPoint& a, const FloatPoint& b) 198{ 199 return a.x() == b.x() && a.y() == b.y(); 200} 201 202inline bool operator!=(const FloatPoint& a, const FloatPoint& b) 203{ 204 return a.x() != b.x() || a.y() != b.y(); 205} 206 207inline float operator*(const FloatPoint& a, const FloatPoint& b) 208{ 209 // dot product 210 return a.dot(b); 211} 212 213inline IntPoint roundedIntPoint(const FloatPoint& p) 214{ 215 return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y()))); 216} 217 218inline IntPoint flooredIntPoint(const FloatPoint& p) 219{ 220 return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); 221} 222 223inline FloatPoint flooredToDevicePixels(const FloatPoint& p, float deviceScaleFactor) 224{ 225 return FloatPoint(floorf(p.x() * deviceScaleFactor) / deviceScaleFactor, floorf(p.y() * deviceScaleFactor) / deviceScaleFactor); 226} 227 228inline IntPoint ceiledIntPoint(const FloatPoint& p) 229{ 230 return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y()))); 231} 232 233inline FloatPoint ceiledToDevicePixels(const FloatPoint& p, float deviceScaleFactor) 234{ 235 return FloatPoint(ceilf(p.x() * deviceScaleFactor) / deviceScaleFactor, ceilf(p.y() * deviceScaleFactor) / deviceScaleFactor); 236} 237 238inline IntSize flooredIntSize(const FloatPoint& p) 239{ 240 return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))); 241} 242 243inline FloatSize toFloatSize(const FloatPoint& a) 244{ 245 return FloatSize(a.x(), a.y()); 246} 247 248inline FloatPoint toFloatPoint(const FloatSize& a) 249{ 250 return FloatPoint(a.width(), a.height()); 251} 252 253} 254 255#endif 256