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-02-03 22:07:47 +00:00
|
|
|
#include "CurveIntersection.h"
|
2012-01-25 18:57:23 +00:00
|
|
|
#include "LineUtilities.h"
|
|
|
|
|
|
|
|
bool implicitLine(const _Line& line, double& slope, double& axisIntercept) {
|
2012-02-03 22:07:47 +00:00
|
|
|
_Point delta;
|
|
|
|
tangent(line, delta);
|
|
|
|
bool moreHorizontal = fabs(delta.x) > fabs(delta.y);
|
2012-01-25 18:57:23 +00:00
|
|
|
if (moreHorizontal) {
|
2012-02-03 22:07:47 +00:00
|
|
|
slope = delta.y / delta.x;
|
2012-01-25 18:57:23 +00:00
|
|
|
axisIntercept = line[0].y - slope * line[0].x;
|
|
|
|
} else {
|
2012-02-03 22:07:47 +00:00
|
|
|
slope = delta.x / delta.y;
|
2012-01-25 18:57:23 +00:00
|
|
|
axisIntercept = line[0].x - slope * line[0].y;
|
|
|
|
}
|
|
|
|
return moreHorizontal;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reduceOrder(const _Line& line, _Line& reduced) {
|
|
|
|
reduced[0] = line[0];
|
|
|
|
int different = line[0] != line[1];
|
|
|
|
reduced[1] = line[different];
|
|
|
|
return 1 + different;
|
|
|
|
}
|
2012-02-07 22:10:51 +00:00
|
|
|
|
|
|
|
void sub_divide(const _Line& line, double t1, double t2, _Line& dst) {
|
|
|
|
_Point delta;
|
|
|
|
tangent(line, delta);
|
|
|
|
dst[0].x = line[0].x - t1 * delta.x;
|
|
|
|
dst[0].y = line[0].y - t1 * delta.y;
|
|
|
|
dst[1].x = line[0].x - t2 * delta.x;
|
|
|
|
dst[1].y = line[0].y - t2 * delta.y;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
// may have this below somewhere else already:
|
2012-04-26 21:01:06 +00:00
|
|
|
// copying here because I thought it was clever
|
|
|
|
|
|
|
|
// Copyright 2001, softSurfer (www.softsurfer.com)
|
|
|
|
// This code may be freely used and modified for any purpose
|
|
|
|
// providing that this copyright notice is included with it.
|
|
|
|
// SoftSurfer makes no warranty for this code, and cannot be held
|
|
|
|
// liable for any real or imagined damage resulting from its use.
|
|
|
|
// Users of this code must verify correctness for their application.
|
|
|
|
|
|
|
|
// Assume that a class is already given for the object:
|
|
|
|
// Point with coordinates {float x, y;}
|
|
|
|
//===================================================================
|
|
|
|
|
|
|
|
// isLeft(): tests if a point is Left|On|Right of an infinite line.
|
|
|
|
// Input: three points P0, P1, and P2
|
|
|
|
// Return: >0 for P2 left of the line through P0 and P1
|
|
|
|
// =0 for P2 on the line
|
|
|
|
// <0 for P2 right of the line
|
|
|
|
// See: the January 2001 Algorithm on Area of Triangles
|
2012-06-01 17:44:28 +00:00
|
|
|
#if 0
|
2012-04-26 21:01:06 +00:00
|
|
|
float isLeft( _Point P0, _Point P1, _Point P2 )
|
|
|
|
{
|
2012-05-22 17:01:14 +00:00
|
|
|
return (float) ((P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y));
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
double t_at(const _Line& line, const _Point& pt) {
|
|
|
|
double dx = line[1].x - line[0].x;
|
|
|
|
double dy = line[1].y - line[0].y;
|
|
|
|
if (fabs(dx) > fabs(dy)) {
|
|
|
|
if (approximately_zero(dx)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (pt.x - line[0].x) / dx;
|
|
|
|
}
|
|
|
|
if (approximately_zero(dy)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (pt.y - line[0].y) / dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setMinMax(double x, int flags, double& minX, double& maxX) {
|
|
|
|
if (minX > x && (flags & (kFindTopMin | kFindBottomMin))) {
|
|
|
|
minX = x;
|
|
|
|
}
|
|
|
|
if (maxX < x && (flags & (kFindTopMax | kFindBottomMax))) {
|
|
|
|
maxX = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void x_at(const _Point& p1, const _Point& p2, double top, double bottom,
|
|
|
|
int flags, double& minX, double& maxX) {
|
2013-01-04 19:41:13 +00:00
|
|
|
if (AlmostEqualUlps(p1.y, p2.y)) {
|
2012-07-02 20:27:02 +00:00
|
|
|
// It should be OK to bail early in this case. There's another edge
|
2012-08-23 18:14:13 +00:00
|
|
|
// which shares this end point which can intersect without failing to
|
2012-07-02 20:27:02 +00:00
|
|
|
// have a slope ... maybe
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// p2.x is always greater than p1.x -- the part of points (p1, p2) are
|
|
|
|
// moving from the start of the cubic towards its end.
|
|
|
|
// if p1.y < p2.y, minX can be affected
|
2012-08-23 18:14:13 +00:00
|
|
|
// if p1.y > p2.y, maxX can be affected
|
2012-07-02 20:27:02 +00:00
|
|
|
double slope = (p2.x - p1.x) / (p2.y - p1.y);
|
|
|
|
int topFlags = flags & (kFindTopMin | kFindTopMax);
|
2012-12-10 12:50:53 +00:00
|
|
|
if (topFlags && ((top <= p1.y && top >= p2.y)
|
|
|
|
|| (top >= p1.y && top <= p2.y))) {
|
2012-07-02 20:27:02 +00:00
|
|
|
double x = p1.x + (top - p1.y) * slope;
|
|
|
|
setMinMax(x, topFlags, minX, maxX);
|
|
|
|
}
|
|
|
|
int bottomFlags = flags & (kFindBottomMin | kFindBottomMax);
|
2012-12-10 12:50:53 +00:00
|
|
|
if (bottomFlags && ((bottom <= p1.y && bottom >= p2.y)
|
|
|
|
|| (bottom >= p1.y && bottom <= p2.y))) {
|
2012-07-02 20:27:02 +00:00
|
|
|
double x = p1.x + (bottom - p1.y) * slope;
|
|
|
|
setMinMax(x, bottomFlags, minX, maxX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void xy_at_t(const _Line& line, double t, double& x, double& y) {
|
|
|
|
double one_t = 1 - t;
|
|
|
|
if (&x) {
|
|
|
|
x = one_t * line[0].x + t * line[1].x;
|
|
|
|
}
|
|
|
|
if (&y) {
|
|
|
|
y = one_t * line[0].y + t * line[1].y;
|
|
|
|
}
|
|
|
|
}
|