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
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
/** \struct SkIPoint
|
|
|
|
|
|
|
|
SkIPoint holds two 32 bit integer coordinates
|
|
|
|
*/
|
|
|
|
struct SkIPoint {
|
|
|
|
int32_t fX, fY;
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2010-01-28 21:34:33 +00:00
|
|
|
static SkIPoint Make(int32_t x, int32_t y) {
|
|
|
|
SkIPoint pt;
|
|
|
|
pt.set(x, y);
|
|
|
|
return pt;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-03-04 22:27:10 +00:00
|
|
|
int32_t x() const { return fX; }
|
|
|
|
int32_t y() const { return fY; }
|
|
|
|
void setX(int32_t x) { fX = x; }
|
|
|
|
void setY(int32_t y) { fY = y; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true iff fX and fY are both zero.
|
|
|
|
*/
|
|
|
|
bool isZero() const { return (fX | fY) == 0; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set both fX and fY to zero. Same as set(0, 0)
|
|
|
|
*/
|
|
|
|
void setZero() { fX = fY = 0; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the x and y values of the point. */
|
|
|
|
void set(int32_t x, int32_t y) { fX = x; fY = y; }
|
|
|
|
|
|
|
|
/** Rotate the point clockwise, writing the new point into dst
|
|
|
|
It is legal for dst == this
|
|
|
|
*/
|
|
|
|
void rotateCW(SkIPoint* dst) const;
|
|
|
|
|
|
|
|
/** Rotate the point clockwise, writing the new point back into the point
|
|
|
|
*/
|
|
|
|
|
|
|
|
void rotateCW() { this->rotateCW(this); }
|
|
|
|
|
|
|
|
/** Rotate the point counter-clockwise, writing the new point into dst.
|
|
|
|
It is legal for dst == this
|
|
|
|
*/
|
|
|
|
void rotateCCW(SkIPoint* dst) const;
|
|
|
|
|
|
|
|
/** Rotate the point counter-clockwise, writing the new point back into
|
|
|
|
the point
|
|
|
|
*/
|
|
|
|
void rotateCCW() { this->rotateCCW(this); }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Negate the X and Y coordinates of the point.
|
|
|
|
*/
|
|
|
|
void negate() { fX = -fX; fY = -fY; }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return a new point whose X and Y coordinates are the negative of the
|
|
|
|
original point's
|
|
|
|
*/
|
|
|
|
SkIPoint operator-() const {
|
|
|
|
SkIPoint neg;
|
|
|
|
neg.fX = -fX;
|
|
|
|
neg.fY = -fY;
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add v's coordinates to this point's */
|
|
|
|
void operator+=(const SkIPoint& v) {
|
|
|
|
fX += v.fX;
|
|
|
|
fY += v.fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Subtract v's coordinates from this point's */
|
|
|
|
void operator-=(const SkIPoint& v) {
|
|
|
|
fX -= v.fX;
|
|
|
|
fY -= v.fY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the point's coordinates equal (x,y) */
|
|
|
|
bool equals(int32_t x, int32_t y) const {
|
|
|
|
return fX == x && fY == y;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a new point whose coordinates are the difference between
|
|
|
|
a and b (i.e. a - b)
|
|
|
|
*/
|
|
|
|
friend SkIPoint operator-(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
SkIPoint v;
|
|
|
|
v.set(a.fX - b.fX, a.fY - b.fY);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a new point whose coordinates are the sum of a and b (a + b)
|
|
|
|
*/
|
|
|
|
friend SkIPoint operator+(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
SkIPoint v;
|
|
|
|
v.set(a.fX + b.fX, a.fY + b.fY);
|
|
|
|
return v;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the dot product of a and b, treating them as 2D vectors
|
|
|
|
*/
|
|
|
|
static int32_t DotProduct(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
return a.fX * b.fX + a.fY * b.fY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the cross product of a and b, treating them as 2D vectors
|
|
|
|
*/
|
|
|
|
static int32_t CrossProduct(const SkIPoint& a, const SkIPoint& b) {
|
|
|
|
return a.fX * b.fY - a.fY * b.fX;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-15 21:27:08 +00:00
|
|
|
struct SK_API SkPoint {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar fX, fY;
|
|
|
|
|
2010-01-28 21:34:33 +00:00
|
|
|
static SkPoint Make(SkScalar x, SkScalar y) {
|
|
|
|
SkPoint pt;
|
|
|
|
pt.set(x, y);
|
|
|
|
return pt;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2011-03-20 20:19:16 +00:00
|
|
|
SkScalar x() const { return fX; }
|
|
|
|
SkScalar y() const { return fY; }
|
|
|
|
|
2012-05-29 01:40:15 +00:00
|
|
|
/**
|
|
|
|
* Returns true iff fX and fY are both zero.
|
|
|
|
*/
|
|
|
|
bool isZero() const { return (0 == fX) & (0 == fY); }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point's X and Y coordinates */
|
|
|
|
void set(SkScalar x, SkScalar y) { fX = x; fY = y; }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point's X and Y coordinates by automatically promoting (x,y) to
|
|
|
|
SkScalar values.
|
|
|
|
*/
|
|
|
|
void iset(int32_t x, int32_t y) {
|
|
|
|
fX = SkIntToScalar(x);
|
|
|
|
fY = SkIntToScalar(y);
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point's X and Y coordinates by automatically promoting p's
|
|
|
|
coordinates to SkScalar values.
|
|
|
|
*/
|
|
|
|
void iset(const SkIPoint& p) {
|
|
|
|
fX = SkIntToScalar(p.fX);
|
|
|
|
fY = SkIntToScalar(p.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
|
|
|
|
2011-05-06 19:26:26 +00:00
|
|
|
// counter-clockwise fan
|
|
|
|
void setIRectFan(int l, int t, int r, int b) {
|
|
|
|
SkPoint* v = this;
|
|
|
|
v[0].set(SkIntToScalar(l), SkIntToScalar(t));
|
|
|
|
v[1].set(SkIntToScalar(l), SkIntToScalar(b));
|
|
|
|
v[2].set(SkIntToScalar(r), SkIntToScalar(b));
|
|
|
|
v[3].set(SkIntToScalar(r), SkIntToScalar(t));
|
|
|
|
}
|
|
|
|
void setIRectFan(int l, int t, int r, int b, size_t stride);
|
|
|
|
|
|
|
|
// counter-clockwise fan
|
|
|
|
void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
|
|
|
|
SkPoint* v = this;
|
|
|
|
v[0].set(l, t);
|
|
|
|
v[1].set(l, b);
|
|
|
|
v[2].set(r, b);
|
|
|
|
v[3].set(r, t);
|
|
|
|
}
|
|
|
|
void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b, size_t stride);
|
|
|
|
|
2011-09-12 14:59:34 +00:00
|
|
|
static void Offset(SkPoint points[], int count, const SkPoint& offset) {
|
|
|
|
Offset(points, count, offset.fX, offset.fY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
points[i].offset(dx, dy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 19:26:26 +00:00
|
|
|
void offset(SkScalar dx, SkScalar dy) {
|
|
|
|
fX += dx;
|
|
|
|
fY += dy;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the euclidian distance from (0,0) to the point
|
|
|
|
*/
|
|
|
|
SkScalar length() const { return SkPoint::Length(fX, fY); }
|
2011-05-06 19:26:26 +00:00
|
|
|
SkScalar distanceToOrigin() const { return this->length(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-09-07 12:23:41 +00:00
|
|
|
/**
|
|
|
|
* Return true if the computed length of the vector is >= the internal
|
|
|
|
* tolerance (used to avoid dividing by tiny values).
|
|
|
|
*/
|
2013-11-08 20:14:16 +00:00
|
|
|
static bool CanNormalize(SkScalar dx, SkScalar dy) {
|
|
|
|
// Simple enough (and performance critical sometimes) so we inline it.
|
|
|
|
return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
|
|
|
|
}
|
2011-09-07 12:23:41 +00:00
|
|
|
|
|
|
|
bool canNormalize() const {
|
|
|
|
return CanNormalize(fX, fY);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point (vector) to be unit-length in the same direction as it
|
2011-06-15 18:04:58 +00:00
|
|
|
already points. If the point has a degenerate length (i.e. nearly 0)
|
|
|
|
then return false and do nothing; otherwise return true.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool normalize();
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point (vector) to be unit-length in the same direction as the
|
|
|
|
x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0)
|
|
|
|
then return false and do nothing, otherwise return true.
|
|
|
|
*/
|
|
|
|
bool setNormalize(SkScalar x, SkScalar y);
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Scale the point (vector) to have the specified length, and return that
|
|
|
|
length. If the original length is degenerately small (nearly zero),
|
|
|
|
do nothing and return false, otherwise return true.
|
|
|
|
*/
|
|
|
|
bool setLength(SkScalar length);
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set the point (vector) to have the specified length in the same
|
|
|
|
direction as (x,y). If the vector (x,y) has a degenerate length
|
|
|
|
(i.e. nearly 0) then return false and do nothing, otherwise return true.
|
|
|
|
*/
|
|
|
|
bool setLength(SkScalar x, SkScalar y, SkScalar length);
|
|
|
|
|
2013-11-08 20:14:16 +00:00
|
|
|
/** Same as setLength, but favoring speed over accuracy.
|
|
|
|
*/
|
|
|
|
bool setLengthFast(SkScalar length);
|
|
|
|
|
|
|
|
/** Same as setLength, but favoring speed over accuracy.
|
|
|
|
*/
|
|
|
|
bool setLengthFast(SkScalar x, SkScalar y, SkScalar length);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Scale the point's coordinates by scale, writing the answer into dst.
|
|
|
|
It is legal for dst == this.
|
|
|
|
*/
|
|
|
|
void scale(SkScalar scale, SkPoint* dst) const;
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Scale the point's coordinates by scale, writing the answer back into
|
|
|
|
the point.
|
|
|
|
*/
|
2009-01-15 14:38:33 +00:00
|
|
|
void scale(SkScalar value) { this->scale(value, this); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Rotate the point clockwise by 90 degrees, writing the answer into dst.
|
|
|
|
It is legal for dst == this.
|
|
|
|
*/
|
|
|
|
void rotateCW(SkPoint* dst) const;
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Rotate the point clockwise by 90 degrees, writing the answer back into
|
|
|
|
the point.
|
|
|
|
*/
|
|
|
|
void rotateCW() { this->rotateCW(this); }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Rotate the point counter-clockwise by 90 degrees, writing the answer
|
|
|
|
into dst. It is legal for dst == this.
|
|
|
|
*/
|
|
|
|
void rotateCCW(SkPoint* dst) const;
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Rotate the point counter-clockwise by 90 degrees, writing the answer
|
|
|
|
back into the point.
|
|
|
|
*/
|
|
|
|
void rotateCCW() { this->rotateCCW(this); }
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Negate the point's coordinates
|
|
|
|
*/
|
|
|
|
void negate() {
|
|
|
|
fX = -fX;
|
|
|
|
fY = -fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns a new point whose coordinates are the negative of the point's
|
|
|
|
*/
|
|
|
|
SkPoint operator-() const {
|
|
|
|
SkPoint neg;
|
|
|
|
neg.fX = -fX;
|
|
|
|
neg.fY = -fY;
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add v's coordinates to the point's
|
|
|
|
*/
|
|
|
|
void operator+=(const SkPoint& v) {
|
|
|
|
fX += v.fX;
|
|
|
|
fY += v.fY;
|
|
|
|
}
|
2011-03-04 22:27:10 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Subtract v's coordinates from the point's
|
|
|
|
*/
|
|
|
|
void operator-=(const SkPoint& v) {
|
|
|
|
fX -= v.fX;
|
|
|
|
fY -= v.fY;
|
|
|
|
}
|
|
|
|
|
2012-07-26 15:20:36 +00:00
|
|
|
/**
|
|
|
|
* Returns true if both X and Y are finite (not infinity or NaN)
|
|
|
|
*/
|
|
|
|
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).
|
|
|
|
SkASSERT(0 == accum || !(accum == 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?
|
|
|
|
return accum == accum;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:36:34 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the point's coordinates equal (x,y)
|
|
|
|
*/
|
|
|
|
bool equals(SkScalar x, SkScalar y) const {
|
|
|
|
return fX == x && fY == y;
|
|
|
|
}
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-04-11 17:51:01 +00:00
|
|
|
/** Return true if this point and the given point are far enough apart
|
|
|
|
such that a vector between them would be non-degenerate.
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
WARNING: Unlike the explicit tolerance version,
|
2012-04-11 17:51:01 +00:00
|
|
|
this method does not use componentwise comparison. Instead, it
|
|
|
|
uses a comparison designed to match judgments elsewhere regarding
|
|
|
|
degeneracy ("points A and B are so close that the vector between them
|
|
|
|
is essentially zero").
|
Modifying SkPath to store all verbs provided by the user, and to give
correct results for all stroke and fill modes even on the various types
of degenerate paths.
The goals of this patch include:
1. Have Skia store all of the verbs implied by path construction methods, even
if those define degenerate paths. The SVG implementation in WebKit, which is
backed by Skia, needs to know about all elements of the path, even degenerate
ones, for the correct drawing of markers and line caps. For example, in SVG you
should be able to draw a scatter plot by specifying a marker for vertices and
then giving a sequence of moveTo commands. Skia will not store the moveTos,
requiring a different storage mechanism.
2. Assuming 1, maintain the current Skia behavior. That is, make Skia robust to
degenerate paths.
3. Fix an existing bug in Skia where a degenerate moveTo-lineTo pair spits out
warnings from rasterization and produces incorrect results in inverse-fill
renderings.
4. Adds extensive testing for degenerate paths and path rendering in general.
To meet these goals, the patch I am proposing will result in minor additional
storage for degenerate paths (a few bytes per degenerate path, only if the user
defines such paths). There is also some additional overhead in the iteration
code, with the path now cleaned to remove degenerate segments as part of the
iteration process. I suspect this will also fix issues with computing normal
vectors to degenerate segments. Benchmarking suggests that this change may
result in slightly (< 1%) slower path drawing due to the checks for
degeneracy. This overhead could be removed (in fact, a significant speedup
could occur) if the results of iterating to clean up the path were cached.
This would cost memory, of course, and quite a bit of it.
BUG=398
TEST=tests/PathTest.cpp
gm/cubicpaths.cpp
gm/degeneratesegments.cpp
gm/movepaths.cpp
gm/linepaths.cpp
gm/quadpaths.cpp
Review URL: http://codereview.appspot.com/5482051
git-svn-id: http://skia.googlecode.com/svn/trunk@2901 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-12-20 15:14:18 +00:00
|
|
|
*/
|
2012-04-11 17:51:01 +00:00
|
|
|
bool equalsWithinTolerance(const SkPoint& p) const {
|
|
|
|
return !CanNormalize(fX - p.fX, fY - p.fY);
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
/** WARNING: There is no guarantee that the result will reflect judgments
|
2012-04-11 17:51:01 +00:00
|
|
|
elsewhere regarding degeneracy ("points A and B are so close that the
|
|
|
|
vector between them is essentially zero").
|
|
|
|
*/
|
|
|
|
bool equalsWithinTolerance(const SkPoint& p, SkScalar tol) const {
|
|
|
|
return SkScalarNearlyZero(fX - p.fX, tol)
|
|
|
|
&& SkScalarNearlyZero(fY - p.fY, tol);
|
Modifying SkPath to store all verbs provided by the user, and to give
correct results for all stroke and fill modes even on the various types
of degenerate paths.
The goals of this patch include:
1. Have Skia store all of the verbs implied by path construction methods, even
if those define degenerate paths. The SVG implementation in WebKit, which is
backed by Skia, needs to know about all elements of the path, even degenerate
ones, for the correct drawing of markers and line caps. For example, in SVG you
should be able to draw a scatter plot by specifying a marker for vertices and
then giving a sequence of moveTo commands. Skia will not store the moveTos,
requiring a different storage mechanism.
2. Assuming 1, maintain the current Skia behavior. That is, make Skia robust to
degenerate paths.
3. Fix an existing bug in Skia where a degenerate moveTo-lineTo pair spits out
warnings from rasterization and produces incorrect results in inverse-fill
renderings.
4. Adds extensive testing for degenerate paths and path rendering in general.
To meet these goals, the patch I am proposing will result in minor additional
storage for degenerate paths (a few bytes per degenerate path, only if the user
defines such paths). There is also some additional overhead in the iteration
code, with the path now cleaned to remove degenerate segments as part of the
iteration process. I suspect this will also fix issues with computing normal
vectors to degenerate segments. Benchmarking suggests that this change may
result in slightly (< 1%) slower path drawing due to the checks for
degeneracy. This overhead could be removed (in fact, a significant speedup
could occur) if the results of iterating to clean up the path were cached.
This would cost memory, of course, and quite a bit of it.
BUG=398
TEST=tests/PathTest.cpp
gm/cubicpaths.cpp
gm/degeneratesegments.cpp
gm/movepaths.cpp
gm/linepaths.cpp
gm/quadpaths.cpp
Review URL: http://codereview.appspot.com/5482051
git-svn-id: http://skia.googlecode.com/svn/trunk@2901 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-12-20 15:14:18 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns a new point whose coordinates are the difference between
|
|
|
|
a's and b's (a - b)
|
|
|
|
*/
|
|
|
|
friend SkPoint operator-(const SkPoint& a, const SkPoint& b) {
|
|
|
|
SkPoint v;
|
|
|
|
v.set(a.fX - b.fX, a.fY - b.fY);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a new point whose coordinates are the sum of a's and b's (a + b)
|
|
|
|
*/
|
|
|
|
friend SkPoint operator+(const SkPoint& a, const SkPoint& b) {
|
|
|
|
SkPoint v;
|
|
|
|
v.set(a.fX + b.fX, a.fY + b.fY);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the euclidian distance from (0,0) to (x,y)
|
|
|
|
*/
|
|
|
|
static SkScalar Length(SkScalar x, SkScalar y);
|
2010-01-28 21:34:33 +00:00
|
|
|
|
|
|
|
/** Normalize pt, returning its previous length. If the prev length is too
|
2011-09-07 12:23:41 +00:00
|
|
|
small (degenerate), return 0 and leave pt unchanged. This uses the same
|
|
|
|
tolerance as CanNormalize.
|
2011-06-15 18:04:58 +00:00
|
|
|
|
|
|
|
Note that this method may be significantly more expensive than
|
|
|
|
the non-static normalize(), because it has to return the previous length
|
|
|
|
of the point. If you don't need the previous length, call the
|
|
|
|
non-static normalize() method instead.
|
2010-01-28 21:34:33 +00:00
|
|
|
*/
|
|
|
|
static SkScalar Normalize(SkPoint* pt);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the euclidian distance between a and b
|
|
|
|
*/
|
|
|
|
static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
|
|
|
|
return Length(a.fX - b.fX, a.fY - b.fY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the dot product of a and b, treating them as 2D vectors
|
|
|
|
*/
|
|
|
|
static SkScalar DotProduct(const SkPoint& a, const SkPoint& 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
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the cross product of a and b, treating them as 2D vectors
|
|
|
|
*/
|
|
|
|
static SkScalar CrossProduct(const SkPoint& a, const SkPoint& 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
|
|
|
|
|
|
|
SkScalar cross(const SkPoint& vec) const {
|
|
|
|
return CrossProduct(*this, vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar dot(const SkPoint& vec) const {
|
|
|
|
return DotProduct(*this, vec);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-06 19:26:26 +00:00
|
|
|
SkScalar lengthSqd() const {
|
|
|
|
return DotProduct(*this, *this);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-06 19:26:26 +00:00
|
|
|
SkScalar distanceToSqd(const SkPoint& pt) const {
|
|
|
|
SkScalar dx = fX - pt.fX;
|
|
|
|
SkScalar dy = fY - pt.fY;
|
2014-01-27 13:41:02 +00:00
|
|
|
return dx * dx + dy * dy;
|
2011-05-06 19:26:26 +00:00
|
|
|
}
|
2011-08-23 14:39:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The side of a point relative to a line. If the line is from a to b then
|
|
|
|
* the values are consistent with the sign of (b-a) cross (pt-a)
|
|
|
|
*/
|
|
|
|
enum Side {
|
|
|
|
kLeft_Side = -1,
|
|
|
|
kOn_Side = 0,
|
|
|
|
kRight_Side = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the squared distance to the infinite line between two pts. Also
|
|
|
|
* optionally returns the side of the line that the pt falls on (looking
|
|
|
|
* along line from a to b)
|
|
|
|
*/
|
|
|
|
SkScalar distanceToLineBetweenSqd(const SkPoint& a,
|
|
|
|
const SkPoint& b,
|
|
|
|
Side* side = NULL) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the distance to the infinite line between two pts. Also
|
|
|
|
* optionally returns the side of the line that the pt falls on (looking
|
|
|
|
* along the line from a to b)
|
|
|
|
*/
|
|
|
|
SkScalar distanceToLineBetween(const SkPoint& a,
|
|
|
|
const SkPoint& b,
|
|
|
|
Side* side = NULL) const {
|
|
|
|
return SkScalarSqrt(this->distanceToLineBetweenSqd(a, b, side));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the squared distance to the line segment between pts a and b
|
|
|
|
*/
|
|
|
|
SkScalar distanceToLineSegmentBetweenSqd(const SkPoint& a,
|
2011-05-06 19:26:26 +00:00
|
|
|
const SkPoint& b) const;
|
2011-08-23 14:39:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the distance to the line segment between pts a and b.
|
|
|
|
*/
|
|
|
|
SkScalar distanceToLineSegmentBetween(const SkPoint& a,
|
2011-05-06 19:26:26 +00:00
|
|
|
const SkPoint& b) const {
|
|
|
|
return SkScalarSqrt(this->distanceToLineSegmentBetweenSqd(a, b));
|
|
|
|
}
|
2011-08-23 14:39:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make this vector be orthogonal to vec. Looking down vec the
|
|
|
|
* new vector will point in direction indicated by side (which
|
|
|
|
* must be kLeft_Side or kRight_Side).
|
|
|
|
*/
|
|
|
|
void setOrthog(const SkPoint& vec, Side side = kLeft_Side) {
|
|
|
|
// vec could be this
|
|
|
|
SkScalar tmp = vec.fX;
|
2012-02-15 16:52:51 +00:00
|
|
|
if (kRight_Side == side) {
|
2011-08-23 14:39:01 +00:00
|
|
|
fX = -vec.fY;
|
|
|
|
fY = tmp;
|
|
|
|
} else {
|
2012-02-15 16:52:51 +00:00
|
|
|
SkASSERT(kLeft_Side == side);
|
2011-08-23 14:39:01 +00:00
|
|
|
fX = vec.fY;
|
|
|
|
fY = -tmp;
|
|
|
|
}
|
|
|
|
}
|
2013-03-08 16:07:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cast-safe way to treat the point as an array of (2) SkScalars.
|
|
|
|
*/
|
|
|
|
const SkScalar* asScalars() const { return &fX; }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef SkPoint SkVector;
|
|
|
|
|
|
|
|
#endif
|