2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-08 15:49:19 +00:00
|
|
|
/* Generated by tools/bookmaker from include/core/SkPoint.h and docs/SkPoint_Reference.bmh
|
2018-07-13 12:21:59 +00:00
|
|
|
on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
|
2018-06-08 15:49:19 +00:00
|
|
|
https://skia.org/user/api/SkPoint_Reference
|
|
|
|
|
|
|
|
You may edit either file directly. Structural changes to public interfaces require
|
|
|
|
editing both files. After editing docs/SkPoint_Reference.bmh, run:
|
|
|
|
bookmaker -b docs -i include/core/SkPoint.h -p
|
|
|
|
to create an updated version of this file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkPoint_DEFINED
|
|
|
|
#define SkPoint_DEFINED
|
|
|
|
|
2011-09-27 18:03:23 +00:00
|
|
|
#include "SkMath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkScalar.h"
|
2018-02-13 17:48:57 +00:00
|
|
|
#include "../private/SkSafe32.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2017-11-09 21:03:40 +00:00
|
|
|
struct SkIPoint;
|
2018-06-08 15:49:19 +00:00
|
|
|
|
|
|
|
/** SkIVector provides an alternative name for SkIPoint. SkIVector and SkIPoint
|
|
|
|
can be used interchangeably for all purposes.
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
typedef SkIPoint SkIVector;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \struct SkIPoint
|
2018-05-30 13:20:29 +00:00
|
|
|
SkIPoint holds two 32-bit integer coordinates.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
struct SkIPoint {
|
2018-05-30 13:20:29 +00:00
|
|
|
int32_t fX; //!< x-axis value
|
|
|
|
int32_t fY; //!< y-axis value
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
/** Sets fX to x, fY to y.
|
|
|
|
|
|
|
|
@param x integer x-axis value of constructed SkIPoint
|
|
|
|
@param y integer y-axis value of constructed SkIPoint
|
|
|
|
@return SkIPoint (x, y)
|
|
|
|
*/
|
2017-11-08 16:44:31 +00:00
|
|
|
static constexpr SkIPoint Make(int32_t x, int32_t y) {
|
|
|
|
return {x, y};
|
2010-01-28 21:34:33 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns x-axis value of SkIPoint.
|
|
|
|
|
|
|
|
@return fX
|
|
|
|
*/
|
2011-03-04 22:27:10 +00:00
|
|
|
int32_t x() const { return fX; }
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
/** Returns y-axis value of SkIPoint.
|
|
|
|
|
|
|
|
@return fY
|
|
|
|
*/
|
2011-03-04 22:27:10 +00:00
|
|
|
int32_t y() const { return fY; }
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if fX and fY are both zero.
|
|
|
|
|
|
|
|
@return true if fX is zero and fY is zero
|
|
|
|
*/
|
2011-03-04 22:27:10 +00:00
|
|
|
bool isZero() const { return (fX | fY) == 0; }
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets fX to x and fY to y.
|
|
|
|
|
|
|
|
@param x new value for fX
|
|
|
|
@param y new value for fY
|
|
|
|
*/
|
2017-11-08 16:44:31 +00:00
|
|
|
void set(int32_t x, int32_t y) {
|
|
|
|
fX = x;
|
|
|
|
fY = y;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns SkIPoint changing the signs of fX and fY.
|
|
|
|
|
|
|
|
@return SkIPoint as (-fX, -fY)
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
SkIPoint operator-() const {
|
2017-11-08 16:44:31 +00:00
|
|
|
return {-fX, -fY};
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Offsets SkIPoint by ivector v. Sets SkIPoint to (fX + v.fX, fY + v.fY).
|
|
|
|
|
|
|
|
@param v ivector to add
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
void operator+=(const SkIVector& v) {
|
2018-02-13 17:48:57 +00:00
|
|
|
fX = Sk32_sat_add(fX, v.fX);
|
|
|
|
fY = Sk32_sat_add(fY, v.fY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY).
|
|
|
|
|
|
|
|
@param v ivector to subtract
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
void operator-=(const SkIVector& v) {
|
2018-02-13 17:48:57 +00:00
|
|
|
fX = Sk32_sat_sub(fX, v.fX);
|
|
|
|
fY = Sk32_sat_sub(fY, v.fY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if SkIPoint is equivalent to SkIPoint constructed from (x, y).
|
|
|
|
|
|
|
|
@param x value compared with fX
|
|
|
|
@param y value compared with fY
|
|
|
|
@return true if SkIPoint equals (x, y)
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
bool equals(int32_t x, int32_t y) const {
|
|
|
|
return fX == x && fY == y;
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if a is equivalent to b.
|
|
|
|
|
|
|
|
@param a SkIPoint to compare
|
|
|
|
@param b SkIPoint to compare
|
|
|
|
@return true if a.fX == b.fX and a.fY == b.fY
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
return a.fX == b.fX && a.fY == b.fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if a is not equivalent to b.
|
|
|
|
|
|
|
|
@param a SkIPoint to compare
|
|
|
|
@param b SkIPoint to compare
|
|
|
|
@return true if a.fX != b.fX or a.fY != b.fY
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
return a.fX != b.fX || a.fY != b.fY;
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns ivector from b to a; computed as (a.fX - b.fX, a.fY - b.fY).
|
|
|
|
|
|
|
|
Can also be used to subtract ivector from ivector, returning ivector.
|
|
|
|
|
|
|
|
@param a SkIPoint or ivector to subtract from
|
|
|
|
@param b ivector to subtract
|
|
|
|
@return ivector from b to a
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
|
2018-02-13 17:48:57 +00:00
|
|
|
return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as: (a.fX + b.fX, a.fY + b.fY).
|
|
|
|
|
|
|
|
Can also be used to offset SkIPoint b by ivector a, returning SkIPoint.
|
|
|
|
Can also be used to add ivector to ivector, returning ivector.
|
|
|
|
|
|
|
|
@param a SkIPoint or ivector to add to
|
|
|
|
@param b SkIPoint or ivector to add
|
|
|
|
@return SkIPoint equal to a offset by b
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
|
2018-02-13 17:48:57 +00:00
|
|
|
return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-09 21:03:40 +00:00
|
|
|
struct SkPoint;
|
2018-06-08 15:49:19 +00:00
|
|
|
|
|
|
|
/** SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
|
2018-06-14 17:21:08 +00:00
|
|
|
be used interchangeably for all purposes.
|
2018-06-08 15:49:19 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
typedef SkPoint SkVector;
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** \struct SkPoint
|
2018-05-30 13:20:29 +00:00
|
|
|
SkPoint holds two 32-bit floating point coordinates.
|
2018-01-03 15:00:34 +00:00
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkPoint {
|
2018-05-30 13:20:29 +00:00
|
|
|
SkScalar fX; //!< x-axis value
|
|
|
|
SkScalar fY; //!< y-axis value
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
/** Sets fX to x, fY to y. Used both to set SkPoint and vector.
|
|
|
|
|
|
|
|
@param x SkScalar x-axis value of constructed SkPoint or vector
|
|
|
|
@param y SkScalar y-axis value of constructed SkPoint or vector
|
|
|
|
@return SkPoint (x, y)
|
|
|
|
*/
|
2017-11-08 16:44:31 +00:00
|
|
|
static constexpr SkPoint Make(SkScalar x, SkScalar y) {
|
|
|
|
return {x, y};
|
2010-01-28 21:34:33 +00:00
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns x-axis value of SkPoint or vector.
|
|
|
|
|
|
|
|
@return fX
|
|
|
|
*/
|
2011-03-20 20:19:16 +00:00
|
|
|
SkScalar x() const { return fX; }
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
/** Returns y-axis value of SkPoint or vector.
|
|
|
|
|
|
|
|
@return fY
|
|
|
|
*/
|
2011-03-20 20:19:16 +00:00
|
|
|
SkScalar y() const { return fY; }
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if fX and fY are both zero.
|
|
|
|
|
|
|
|
@return true if fX is zero and fY is zero
|
|
|
|
*/
|
2012-05-29 01:40:15 +00:00
|
|
|
bool isZero() const { return (0 == fX) & (0 == fY); }
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets fX to x and fY to y.
|
|
|
|
|
|
|
|
@param x new value for fX
|
|
|
|
@param y new value for fY
|
|
|
|
*/
|
2017-11-08 16:44:31 +00:00
|
|
|
void set(SkScalar x, SkScalar y) {
|
|
|
|
fX = x;
|
|
|
|
fY = y;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets fX to x and fY to y, promoting integers to SkScalar values.
|
|
|
|
|
|
|
|
Assigning a large integer value directly to fX or fY may cause a compiler
|
|
|
|
error, triggered by narrowing conversion of int to SkScalar. This safely
|
|
|
|
casts x and y to avoid the error.
|
|
|
|
|
|
|
|
@param x new value for fX
|
|
|
|
@param y new value for fY
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void iset(int32_t x, int32_t y) {
|
|
|
|
fX = SkIntToScalar(x);
|
|
|
|
fY = SkIntToScalar(y);
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
|
|
|
|
|
|
|
|
Assigning an SkIPoint containing a large integer value directly to fX or fY may
|
|
|
|
cause a compiler error, triggered by narrowing conversion of int to SkScalar.
|
|
|
|
This safely casts p.fX and p.fY to avoid the error.
|
|
|
|
|
|
|
|
@param p SkIPoint members promoted to SkScalar
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void iset(const SkIPoint& p) {
|
|
|
|
fX = SkIntToScalar(p.fX);
|
|
|
|
fY = SkIntToScalar(p.fY);
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY.
|
|
|
|
|
|
|
|
@param pt members providing magnitude for fX and fY
|
|
|
|
*/
|
2011-05-06 19:26:26 +00:00
|
|
|
void setAbs(const SkPoint& pt) {
|
|
|
|
fX = SkScalarAbs(pt.fX);
|
|
|
|
fY = SkScalarAbs(pt.fY);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Adds offset to each SkPoint in points array with count entries.
|
|
|
|
|
|
|
|
@param points SkPoint array
|
|
|
|
@param count entries in array
|
|
|
|
@param offset vector added to points
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
static void Offset(SkPoint points[], int count, const SkVector& offset) {
|
2011-09-12 14:59:34 +00:00
|
|
|
Offset(points, count, offset.fX, offset.fY);
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Adds offset (dx, dy) to each SkPoint in points array of length count.
|
|
|
|
|
|
|
|
@param points SkPoint array
|
|
|
|
@param count entries in array
|
|
|
|
@param dx added to fX in points
|
|
|
|
@param dy added to fY in points
|
|
|
|
*/
|
2011-09-12 14:59:34 +00:00
|
|
|
static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
points[i].offset(dx, dy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Adds offset (dx, dy) to SkPoint.
|
|
|
|
|
|
|
|
@param dx added to fX
|
|
|
|
@param dy added to fY
|
|
|
|
*/
|
2011-05-06 19:26:26 +00:00
|
|
|
void offset(SkScalar dx, SkScalar dy) {
|
|
|
|
fX += dx;
|
|
|
|
fY += dy;
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:21:08 +00:00
|
|
|
/** Returns the Euclidean distance from origin, computed as:
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
sqrt(fX * fX + fY * fY)
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
@return straight-line distance to origin
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
SkScalar length() const { return SkPoint::Length(fX, fY); }
|
2018-01-03 15:00:34 +00:00
|
|
|
|
2018-06-14 17:21:08 +00:00
|
|
|
/** Returns the Euclidean distance from origin, computed as:
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
sqrt(fX * fX + fY * fY)
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
@return straight-line distance to origin
|
|
|
|
*/
|
2011-05-06 19:26:26 +00:00
|
|
|
SkScalar distanceToOrigin() const { return this->length(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY,
|
|
|
|
if possible. If prior length is nearly zero, sets vector to (0, 0) and returns
|
|
|
|
false; otherwise returns true.
|
|
|
|
|
|
|
|
@return true if former length is not zero or nearly zero
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool normalize();
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets vector to (x, y) scaled so length() returns one, and so that
|
|
|
|
(fX, fY) is proportional to (x, y). If (x, y) length is nearly zero,
|
|
|
|
sets vector to (0, 0) and returns false; otherwise returns true.
|
|
|
|
|
|
|
|
@param x proportional value for fX
|
|
|
|
@param y proportional value for fY
|
|
|
|
@return true if (x, y) length is not zero or nearly zero
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool setNormalize(SkScalar x, SkScalar y);
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Scales vector so that distanceToOrigin() returns length, if possible. If former
|
|
|
|
length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
|
|
|
|
true.
|
|
|
|
|
|
|
|
@param length straight-line distance to origin
|
|
|
|
@return true if former length is not zero or nearly zero
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool setLength(SkScalar length);
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets vector to (x, y) scaled to length, if possible. If former
|
|
|
|
length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
|
|
|
|
true.
|
|
|
|
|
|
|
|
@param x proportional value for fX
|
|
|
|
@param y proportional value for fY
|
|
|
|
@param length straight-line distance to origin
|
|
|
|
@return true if (x, y) length is not zero or nearly zero
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool setLength(SkScalar x, SkScalar y, SkScalar length);
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Sets dst to SkPoint times scale. dst may be SkPoint to modify SkPoint in place.
|
|
|
|
|
|
|
|
@param scale factor to multiply SkPoint by
|
|
|
|
@param dst storage for scaled SkPoint
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void scale(SkScalar scale, SkPoint* dst) const;
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Scales SkPoint in place by scale.
|
|
|
|
|
|
|
|
@param value factor to multiply SkPoint by
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2009-01-15 14:38:33 +00:00
|
|
|
void scale(SkScalar value) { this->scale(value, this); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Changes the sign of fX and fY.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void negate() {
|
|
|
|
fX = -fX;
|
|
|
|
fY = -fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns SkPoint changing the signs of fX and fY.
|
|
|
|
|
|
|
|
@return SkPoint as (-fX, -fY)
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
SkPoint operator-() const {
|
2017-11-08 16:44:31 +00:00
|
|
|
return {-fX, -fY};
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Adds vector v to SkPoint. Sets SkPoint to: (fX + v.fX, fY + v.fY).
|
|
|
|
|
|
|
|
@param v vector to add
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
void operator+=(const SkVector& v) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fX += v.fX;
|
|
|
|
fY += v.fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Subtracts vector v from SkPoint. Sets SkPoint to: (fX - v.fX, fY - v.fY).
|
|
|
|
|
|
|
|
@param v vector to subtract
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
void operator-=(const SkVector& v) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fX -= v.fX;
|
|
|
|
fY -= v.fY;
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns SkPoint multiplied by scale.
|
|
|
|
|
|
|
|
@param scale scalar to multiply by
|
|
|
|
@return SkPoint as (fX * scale, fY * scale)
|
|
|
|
*/
|
2015-05-12 17:37:34 +00:00
|
|
|
SkPoint operator*(SkScalar scale) const {
|
2017-11-08 16:44:31 +00:00
|
|
|
return {fX * scale, fY * scale};
|
2015-05-12 17:37:34 +00:00
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2018-07-13 12:21:59 +00:00
|
|
|
/** Multiplies SkPoint by scale. Sets SkPoint to: (fX * scale, fY * scale).
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
@param scale scalar to multiply by
|
|
|
|
@return reference to SkPoint
|
|
|
|
*/
|
2015-05-12 17:37:34 +00:00
|
|
|
SkPoint& operator*=(SkScalar scale) {
|
|
|
|
fX *= scale;
|
|
|
|
fY *= scale;
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if both fX and fY are measurable values.
|
|
|
|
|
|
|
|
@return true for values other than infinities and NaN
|
|
|
|
*/
|
2012-07-26 15:20:36 +00:00
|
|
|
bool isFinite() const {
|
|
|
|
SkScalar accum = 0;
|
|
|
|
accum *= fX;
|
|
|
|
accum *= fY;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-07-26 15:20:36 +00:00
|
|
|
// accum is either NaN or it is finite (zero).
|
2014-12-15 20:08:47 +00:00
|
|
|
SkASSERT(0 == accum || SkScalarIsNaN(accum));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-07-26 15:20:36 +00:00
|
|
|
// value==value will be true iff value is not NaN
|
|
|
|
// TODO: is it faster to say !accum or accum==accum?
|
2014-12-15 20:08:47 +00:00
|
|
|
return !SkScalarIsNaN(accum);
|
2012-07-26 15:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if SkPoint is equivalent to SkPoint constructed from (x, y).
|
|
|
|
|
|
|
|
@param x value compared with fX
|
|
|
|
@param y value compared with fY
|
|
|
|
@return true if SkPoint equals (x, y)
|
|
|
|
*/
|
2013-01-28 22:36:34 +00:00
|
|
|
bool equals(SkScalar x, SkScalar y) const {
|
|
|
|
return fX == x && fY == y;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if a is equivalent to b.
|
|
|
|
|
|
|
|
@param a SkPoint to compare
|
|
|
|
@param b SkPoint to compare
|
|
|
|
@return true if a.fX == b.fX and a.fY == b.fY
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
friend bool operator==(const SkPoint& a, const SkPoint& b) {
|
|
|
|
return a.fX == b.fX && a.fY == b.fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns true if a is not equivalent to b.
|
|
|
|
|
|
|
|
@param a SkPoint to compare
|
|
|
|
@param b SkPoint to compare
|
|
|
|
@return true if a.fX != b.fX or a.fY != b.fY
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
friend bool operator!=(const SkPoint& a, const SkPoint& b) {
|
|
|
|
return a.fX != b.fX || a.fY != b.fY;
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns vector from b to a, computed as (a.fX - b.fX, a.fY - b.fY).
|
|
|
|
|
|
|
|
Can also be used to subtract vector from SkPoint, returning SkPoint.
|
|
|
|
Can also be used to subtract vector from vector, returning vector.
|
|
|
|
|
|
|
|
@param a SkPoint to subtract from
|
|
|
|
@param b SkPoint to subtract
|
|
|
|
@return vector from b to a
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
friend SkVector operator-(const SkPoint& a, const SkPoint& b) {
|
2017-11-08 16:44:31 +00:00
|
|
|
return {a.fX - b.fX, a.fY - b.fY};
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns SkPoint resulting from SkPoint a offset by vector b, computed as: (a.fX + b.fX, a.fY + b.fY).
|
|
|
|
|
|
|
|
Can also be used to offset SkPoint b by vector a, returning SkPoint.
|
|
|
|
Can also be used to add vector to vector, returning vector.
|
|
|
|
|
|
|
|
@param a SkPoint or vector to add to
|
|
|
|
@param b SkPoint or vector to add
|
|
|
|
@return SkPoint equal to a offset by b
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
friend SkPoint operator+(const SkPoint& a, const SkVector& b) {
|
2017-11-08 16:44:31 +00:00
|
|
|
return {a.fX + b.fX, a.fY + b.fY};
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:21:08 +00:00
|
|
|
/** Returns the Euclidean distance from origin, computed as:
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
sqrt(x * x + y * y)
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
@param x component of length
|
|
|
|
@param y component of length
|
|
|
|
@return straight-line distance to origin
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
static SkScalar Length(SkScalar x, SkScalar y);
|
2010-01-28 21:34:33 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,
|
|
|
|
if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero;
|
|
|
|
otherwise, returns length of vec before vec is scaled.
|
|
|
|
|
|
|
|
Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
|
|
|
|
|
|
|
|
Note that normalize() is faster if prior length is not required.
|
2011-06-15 18:04:58 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
@param vec normalized to unit length
|
|
|
|
@return original vec length
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
static SkScalar Normalize(SkVector* vec);
|
2010-01-28 21:34:33 +00:00
|
|
|
|
2018-06-14 17:21:08 +00:00
|
|
|
/** Returns the Euclidean distance between a and b.
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
@param a line end point
|
|
|
|
@param b line end point
|
|
|
|
@return straight-line distance from a to b
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
|
|
|
|
return Length(a.fX - b.fX, a.fY - b.fY);
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns the dot product of vector a and vector b.
|
|
|
|
|
|
|
|
@param a left side of dot product
|
|
|
|
@param b right side of dot product
|
|
|
|
@return product of input magnitudes and cosine of the angle between them
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
static SkScalar DotProduct(const SkVector& a, const SkVector& b) {
|
2014-01-27 13:41:02 +00:00
|
|
|
return a.fX * b.fX + a.fY * b.fY;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns the cross product of vector a and vector b.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
a and b form three-dimensional vectors with z-axis value equal to zero. The
|
|
|
|
cross product is a three-dimensional vector with x-axis and y-axis values equal
|
|
|
|
to zero. The cross product z-axis component is returned.
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
@param a left side of cross product
|
|
|
|
@param b right side of cross product
|
|
|
|
@return area spanned by vectors signed by angle direction
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
static SkScalar CrossProduct(const SkVector& a, const SkVector& b) {
|
2014-01-27 13:41:02 +00:00
|
|
|
return a.fX * b.fY - a.fY * b.fX;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-05-06 19:26:26 +00:00
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns the cross product of vector and vec.
|
|
|
|
|
2018-02-27 15:22:04 +00:00
|
|
|
Vector and vec form three-dimensional vectors with z-axis value equal to zero.
|
|
|
|
The cross product is a three-dimensional vector with x-axis and y-axis values
|
|
|
|
equal to zero. The cross product z-axis component is returned.
|
2018-01-03 15:00:34 +00:00
|
|
|
|
|
|
|
@param vec right side of cross product
|
|
|
|
@return area spanned by vectors signed by angle direction
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
SkScalar cross(const SkVector& vec) const {
|
2011-05-06 19:26:26 +00:00
|
|
|
return CrossProduct(*this, vec);
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:00:34 +00:00
|
|
|
/** Returns the dot product of vector and vector vec.
|
|
|
|
|
|
|
|
@param vec right side of dot product
|
|
|
|
@return product of input magnitudes and cosine of the angle between them
|
|
|
|
*/
|
2017-11-09 21:03:40 +00:00
|
|
|
SkScalar dot(const SkVector& vec) const {
|
2011-05-06 19:26:26 +00:00
|
|
|
return DotProduct(*this, vec);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|