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 "platform/geometry/FloatSize.h"
31 #include "platform/geometry/IntPoint.h"
32 #include "wtf/MathExtras.h"
36 typedef struct CGPoint CGPoint;
39 #import <Foundation/Foundation.h>
52 class PLATFORM_EXPORT FloatPoint {
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()) { }
60 static FloatPoint zero() { return FloatPoint(); }
62 static FloatPoint narrowPrecision(double x, double y);
64 float x() const { return m_x; }
65 float y() const { return m_y; }
67 void setX(float x) { m_x = x; }
68 void setY(float y) { m_y = y; }
69 void set(float x, float y)
74 void move(float dx, float dy)
79 void move(const IntSize& a)
84 void move(const LayoutSize&);
85 void move(const FloatSize& a)
90 void moveBy(const IntPoint& a)
95 void moveBy(const LayoutPoint&);
96 void moveBy(const FloatPoint& a)
101 void scale(float sx, float sy)
109 float dot(const FloatPoint& a) const
111 return m_x * a.x() + m_y * a.y();
114 float slopeAngleRadians() const;
115 float length() const;
116 float lengthSquared() const
118 return m_x * m_x + m_y * m_y;
121 FloatPoint expandedTo(const FloatPoint& other) const
123 return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
126 FloatPoint shrunkTo(const FloatPoint& other) const
128 return FloatPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y));
131 FloatPoint transposedPoint() const
133 return FloatPoint(m_y, m_x);
137 FloatPoint(const CGPoint&);
138 operator CGPoint() const;
139 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
140 FloatPoint(const NSPoint&);
141 operator NSPoint() const;
145 SkPoint data() const;
152 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
154 a.move(b.width(), b.height());
158 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b)
160 a.move(b.x(), b.y());
164 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
166 a.move(-b.width(), -b.height());
170 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
172 return FloatPoint(a.x() + b.width(), a.y() + b.height());
175 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b)
177 return FloatPoint(a.x() + b.x(), a.y() + b.y());
180 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
182 return FloatSize(a.x() - b.x(), a.y() - b.y());
185 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
187 return FloatPoint(a.x() - b.width(), a.y() - b.height());
190 inline FloatPoint operator-(const FloatPoint& a)
192 return FloatPoint(-a.x(), -a.y());
195 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
197 return a.x() == b.x() && a.y() == b.y();
200 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
202 return a.x() != b.x() || a.y() != b.y();
205 inline float operator*(const FloatPoint& a, const FloatPoint& b)
211 inline IntPoint roundedIntPoint(const FloatPoint& p)
213 return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y())));
216 inline IntPoint flooredIntPoint(const FloatPoint& p)
218 return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
221 inline IntPoint ceiledIntPoint(const FloatPoint& p)
223 return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y())));
226 inline IntSize flooredIntSize(const FloatPoint& p)
228 return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
231 inline FloatSize toFloatSize(const FloatPoint& a)
233 return FloatSize(a.x(), a.y());
236 PLATFORM_EXPORT float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c);
238 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect.
239 PLATFORM_EXPORT bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection);