2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "FloatSize.h"
32 #include <wtf/MathExtras.h>
34 #if PLATFORM(BLACKBERRY)
35 namespace BlackBerry {
42 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
43 typedef struct CGPoint CGPoint;
46 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
47 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
48 typedef struct CGPoint NSPoint;
50 typedef struct _NSPoint NSPoint;
67 class AffineTransform;
68 class TransformationMatrix;
71 class FractionalLayoutPoint;
72 class FractionalLayoutSize;
76 FloatPoint() : m_x(0), m_y(0) { }
77 FloatPoint(float x, float y) : m_x(x), m_y(y) { }
78 FloatPoint(const IntPoint&);
79 FloatPoint(const FractionalLayoutPoint&);
81 static FloatPoint zero() { return FloatPoint(); }
83 static FloatPoint narrowPrecision(double x, double y);
85 float x() const { return m_x; }
86 float y() const { return m_y; }
88 void setX(float x) { m_x = x; }
89 void setY(float y) { m_y = y; }
90 void set(float x, float y)
95 void move(float dx, float dy)
100 void move(const IntSize& a)
105 void move(const FractionalLayoutSize&);
106 void move(const FloatSize& a)
111 void moveBy(const IntPoint& a)
116 void moveBy(const FractionalLayoutPoint&);
117 void moveBy(const FloatPoint& a)
122 void scale(float sx, float sy)
130 float dot(const FloatPoint& a) const
132 return m_x * a.x() + m_y * a.y();
135 float length() const;
136 float lengthSquared() const
138 return m_x * m_x + m_y * m_y;
141 FloatPoint expandedTo(const FloatPoint& other) const
143 return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
146 FloatPoint transposedPoint() const
148 return FloatPoint(m_y, m_x);
151 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
152 FloatPoint(const CGPoint&);
153 operator CGPoint() const;
156 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
157 || (PLATFORM(CHROMIUM) && OS(DARWIN))
158 FloatPoint(const NSPoint&);
159 operator NSPoint() const;
163 FloatPoint(const QPointF&);
164 operator QPointF() const;
167 #if PLATFORM(BLACKBERRY)
168 FloatPoint(const BlackBerry::Platform::FloatPoint&);
169 operator BlackBerry::Platform::FloatPoint() const;
173 operator SkPoint() const;
174 FloatPoint(const SkPoint&);
177 FloatPoint matrixTransform(const TransformationMatrix&) const;
178 FloatPoint matrixTransform(const AffineTransform&) const;
185 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
187 a.move(b.width(), b.height());
191 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b)
193 a.move(b.x(), b.y());
197 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
199 a.move(-b.width(), -b.height());
203 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
205 return FloatPoint(a.x() + b.width(), a.y() + b.height());
208 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b)
210 return FloatPoint(a.x() + b.x(), a.y() + b.y());
213 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
215 return FloatSize(a.x() - b.x(), a.y() - b.y());
218 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
220 return FloatPoint(a.x() - b.width(), a.y() - b.height());
223 inline FloatPoint operator-(const FloatPoint& a)
225 return FloatPoint(-a.x(), -a.y());
228 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
230 return a.x() == b.x() && a.y() == b.y();
233 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
235 return a.x() != b.x() || a.y() != b.y();
238 inline float operator*(const FloatPoint& a, const FloatPoint& b)
244 inline IntPoint roundedIntPoint(const FloatPoint& p)
246 return IntPoint(static_cast<int>(roundf(p.x())), static_cast<int>(roundf(p.y())));
249 inline IntPoint flooredIntPoint(const FloatPoint& p)
251 return IntPoint(static_cast<int>(p.x()), static_cast<int>(p.y()));
254 inline IntSize flooredIntSize(const FloatPoint& p)
256 return IntSize(static_cast<int>(p.x()), static_cast<int>(p.y()));
259 float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c);
261 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect.
262 bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection);