2
* Copyright (C) 2006 The Android Open Source Project
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#ifndef SkPoint_DEFINED
18
#define SkPoint_DEFINED
25
SkIPoint holds two 32 bit integer coordinates
30
static SkIPoint Make(int32_t x, int32_t y) {
36
/** Set the x and y values of the point. */
37
void set(int32_t x, int32_t y) { fX = x; fY = y; }
39
/** Rotate the point clockwise, writing the new point into dst
40
It is legal for dst == this
42
void rotateCW(SkIPoint* dst) const;
44
/** Rotate the point clockwise, writing the new point back into the point
47
void rotateCW() { this->rotateCW(this); }
49
/** Rotate the point counter-clockwise, writing the new point into dst.
50
It is legal for dst == this
52
void rotateCCW(SkIPoint* dst) const;
54
/** Rotate the point counter-clockwise, writing the new point back into
57
void rotateCCW() { this->rotateCCW(this); }
59
/** Negate the X and Y coordinates of the point.
61
void negate() { fX = -fX; fY = -fY; }
63
/** Return a new point whose X and Y coordinates are the negative of the
66
SkIPoint operator-() const {
73
/** Add v's coordinates to this point's */
74
void operator+=(const SkIPoint& v) {
79
/** Subtract v's coordinates from this point's */
80
void operator-=(const SkIPoint& v) {
85
/** Returns true if the point's coordinates equal (x,y) */
86
bool equals(int32_t x, int32_t y) const {
87
return fX == x && fY == y;
90
friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
91
return a.fX == b.fX && a.fY == b.fY;
94
friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
95
return a.fX != b.fX || a.fY != b.fY;
98
/** Returns a new point whose coordinates are the difference between
101
friend SkIPoint operator-(const SkIPoint& a, const SkIPoint& b) {
103
v.set(a.fX - b.fX, a.fY - b.fY);
107
/** Returns a new point whose coordinates are the sum of a and b (a + b)
109
friend SkIPoint operator+(const SkIPoint& a, const SkIPoint& b) {
111
v.set(a.fX + b.fX, a.fY + b.fY);
115
/** Returns the dot product of a and b, treating them as 2D vectors
117
static int32_t DotProduct(const SkIPoint& a, const SkIPoint& b) {
118
return a.fX * b.fX + a.fY * b.fY;
121
/** Returns the cross product of a and b, treating them as 2D vectors
123
static int32_t CrossProduct(const SkIPoint& a, const SkIPoint& b) {
124
return a.fX * b.fY - a.fY * b.fX;
131
static SkPoint Make(SkScalar x, SkScalar y) {
137
/** Set the point's X and Y coordinates */
138
void set(SkScalar x, SkScalar y) { fX = x; fY = y; }
140
/** Set the point's X and Y coordinates by automatically promoting (x,y) to
143
void iset(int32_t x, int32_t y) {
144
fX = SkIntToScalar(x);
145
fY = SkIntToScalar(y);
148
/** Set the point's X and Y coordinates by automatically promoting p's
149
coordinates to SkScalar values.
151
void iset(const SkIPoint& p) {
152
fX = SkIntToScalar(p.fX);
153
fY = SkIntToScalar(p.fY);
156
/** Return the euclidian distance from (0,0) to the point
158
SkScalar length() const { return SkPoint::Length(fX, fY); }
160
/** Set the point (vector) to be unit-length in the same direction as it
161
currently is, and return its old length. If the old length is
162
degenerately small (nearly zero), do nothing and return false, otherwise
167
/** Set the point (vector) to be unit-length in the same direction as the
168
x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0)
169
then return false and do nothing, otherwise return true.
171
bool setNormalize(SkScalar x, SkScalar y);
173
/** Scale the point (vector) to have the specified length, and return that
174
length. If the original length is degenerately small (nearly zero),
175
do nothing and return false, otherwise return true.
177
bool setLength(SkScalar length);
179
/** Set the point (vector) to have the specified length in the same
180
direction as (x,y). If the vector (x,y) has a degenerate length
181
(i.e. nearly 0) then return false and do nothing, otherwise return true.
183
bool setLength(SkScalar x, SkScalar y, SkScalar length);
185
/** Scale the point's coordinates by scale, writing the answer into dst.
186
It is legal for dst == this.
188
void scale(SkScalar scale, SkPoint* dst) const;
190
/** Scale the point's coordinates by scale, writing the answer back into
193
void scale(SkScalar value) { this->scale(value, this); }
195
/** Rotate the point clockwise by 90 degrees, writing the answer into dst.
196
It is legal for dst == this.
198
void rotateCW(SkPoint* dst) const;
200
/** Rotate the point clockwise by 90 degrees, writing the answer back into
203
void rotateCW() { this->rotateCW(this); }
205
/** Rotate the point counter-clockwise by 90 degrees, writing the answer
206
into dst. It is legal for dst == this.
208
void rotateCCW(SkPoint* dst) const;
210
/** Rotate the point counter-clockwise by 90 degrees, writing the answer
213
void rotateCCW() { this->rotateCCW(this); }
215
/** Negate the point's coordinates
222
/** Returns a new point whose coordinates are the negative of the point's
224
SkPoint operator-() const {
231
/** Add v's coordinates to the point's
233
void operator+=(const SkPoint& v) {
238
/** Subtract v's coordinates from the point's
240
void operator-=(const SkPoint& v) {
245
/** Returns true if the point's coordinates equal (x,y)
247
bool equals(SkScalar x, SkScalar y) const { return fX == x && fY == y; }
249
friend bool operator==(const SkPoint& a, const SkPoint& b) {
250
return a.fX == b.fX && a.fY == b.fY;
253
friend bool operator!=(const SkPoint& a, const SkPoint& b) {
254
return a.fX != b.fX || a.fY != b.fY;
257
/** Returns a new point whose coordinates are the difference between
260
friend SkPoint operator-(const SkPoint& a, const SkPoint& b) {
262
v.set(a.fX - b.fX, a.fY - b.fY);
266
/** Returns a new point whose coordinates are the sum of a's and b's (a + b)
268
friend SkPoint operator+(const SkPoint& a, const SkPoint& b) {
270
v.set(a.fX + b.fX, a.fY + b.fY);
274
/** Returns the euclidian distance from (0,0) to (x,y)
276
static SkScalar Length(SkScalar x, SkScalar y);
278
/** Normalize pt, returning its previous length. If the prev length is too
279
small (degenerate), return 0 and leave pt unchanged.
281
static SkScalar Normalize(SkPoint* pt);
283
/** Returns the euclidian distance between a and b
285
static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
286
return Length(a.fX - b.fX, a.fY - b.fY);
289
/** Returns the dot product of a and b, treating them as 2D vectors
291
static SkScalar DotProduct(const SkPoint& a, const SkPoint& b) {
292
return SkScalarMul(a.fX, b.fX) + SkScalarMul(a.fY, b.fY);
295
/** Returns the cross product of a and b, treating them as 2D vectors
297
static SkScalar CrossProduct(const SkPoint& a, const SkPoint& b) {
298
return SkScalarMul(a.fX, b.fY) - SkScalarMul(a.fY, b.fX);
302
typedef SkPoint SkVector;