2012-08-27 14:11:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-01-10 21:46:10 +00:00
|
|
|
#include "DataTypes.h"
|
|
|
|
|
|
|
|
// Sources
|
|
|
|
// computer-aided design - volume 22 number 9 november 1990 pp 538 - 549
|
|
|
|
// online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
// This turns a line segment into a parameterized line, of the form
|
|
|
|
// ax + by + c = 0
|
|
|
|
// When a^2 + b^2 == 1, the line is normalized.
|
|
|
|
// The distance to the line for (x, y) is d(x,y) = ax + by + c
|
|
|
|
//
|
|
|
|
// Note that the distances below are not necessarily normalized. To get the true
|
|
|
|
// distance, it's necessary to either call normalize() after xxxEndPoints(), or
|
|
|
|
// divide the result of xxxDistance() by sqrt(normalSquared())
|
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
class LineParameters {
|
|
|
|
public:
|
|
|
|
void cubicEndPoints(const Cubic& pts) {
|
2012-08-31 20:55:07 +00:00
|
|
|
cubicEndPoints(pts, 0, 3);
|
2012-01-10 21:46:10 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
void cubicEndPoints(const Cubic& pts, int s, int e) {
|
2012-08-31 20:55:07 +00:00
|
|
|
a = approximately_pin(pts[s].y - pts[e].y);
|
|
|
|
b = approximately_pin(pts[e].x - pts[s].x);
|
2012-01-10 21:46:10 +00:00
|
|
|
c = pts[s].x * pts[e].y - pts[e].x * pts[s].y;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
void lineEndPoints(const _Line& pts) {
|
2012-08-31 20:55:07 +00:00
|
|
|
a = approximately_pin(pts[0].y - pts[1].y);
|
|
|
|
b = approximately_pin(pts[1].x - pts[0].x);
|
2012-01-10 21:46:10 +00:00
|
|
|
c = pts[0].x * pts[1].y - pts[1].x * pts[0].y;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
void quadEndPoints(const Quadratic& pts) {
|
2012-08-31 20:55:07 +00:00
|
|
|
quadEndPoints(pts, 0, 2);
|
2012-01-10 21:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void quadEndPoints(const Quadratic& pts, int s, int e) {
|
2012-08-31 20:55:07 +00:00
|
|
|
a = approximately_pin(pts[s].y - pts[e].y);
|
|
|
|
b = approximately_pin(pts[e].x - pts[s].x);
|
2012-01-10 21:46:10 +00:00
|
|
|
c = pts[s].x * pts[e].y - pts[e].x * pts[s].y;
|
|
|
|
}
|
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
double normalSquared() const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return a * a + b * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool normalize() {
|
|
|
|
double normal = sqrt(normalSquared());
|
2012-09-18 20:08:37 +00:00
|
|
|
if (approximately_zero(normal)) {
|
2012-01-10 21:46:10 +00:00
|
|
|
a = b = c = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
double reciprocal = 1 / normal;
|
|
|
|
a *= reciprocal;
|
|
|
|
b *= reciprocal;
|
|
|
|
c *= reciprocal;
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
void cubicDistanceY(const Cubic& pts, Cubic& distance) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
double oneThird = 1 / 3.0;
|
|
|
|
for (int index = 0; index < 4; ++index) {
|
|
|
|
distance[index].x = index * oneThird;
|
|
|
|
distance[index].y = a * pts[index].x + b * pts[index].y + c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
void quadDistanceY(const Quadratic& pts, Quadratic& distance) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
double oneHalf = 1 / 2.0;
|
|
|
|
for (int index = 0; index < 3; ++index) {
|
|
|
|
distance[index].x = index * oneHalf;
|
|
|
|
distance[index].y = a * pts[index].x + b * pts[index].y + c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
void controlPtDistance(const Cubic& pts, double distance[2]) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
for (int index = 0; index < 2; ++index) {
|
|
|
|
distance[index] = a * pts[index + 1].x + b * pts[index + 1].y + c;
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
void controlPtDistance(const Cubic& pts, int i, int j, double distance[2]) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
distance[0] = a * pts[i].x + b * pts[i].y + c;
|
|
|
|
distance[1] = a * pts[j].x + b * pts[j].y + c;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
double controlPtDistance(const Quadratic& pts) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return a * pts[1].x + b * pts[1].y + c;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
double pointDistance(const _Point& pt) const {
|
2012-01-10 21:46:10 +00:00
|
|
|
return a * pt.x + b * pt.y + c;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
|
2012-08-31 20:55:07 +00:00
|
|
|
double dx() const {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
double dy() const {
|
|
|
|
return -a;
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:46:10 +00:00
|
|
|
private:
|
|
|
|
double a;
|
|
|
|
double b;
|
|
|
|
double c;
|
|
|
|
};
|