blob: 6e3b966734e083283dbcceff7691e066dfafe935 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00008#include "LineUtilities.h"
9
10bool implicitLine(const _Line& line, double& slope, double& axisIntercept) {
caryclark@google.comc6825902012-02-03 22:07:47 +000011 _Point delta;
12 tangent(line, delta);
13 bool moreHorizontal = fabs(delta.x) > fabs(delta.y);
caryclark@google.com27accef2012-01-25 18:57:23 +000014 if (moreHorizontal) {
caryclark@google.comc6825902012-02-03 22:07:47 +000015 slope = delta.y / delta.x;
caryclark@google.com27accef2012-01-25 18:57:23 +000016 axisIntercept = line[0].y - slope * line[0].x;
17 } else {
caryclark@google.comc6825902012-02-03 22:07:47 +000018 slope = delta.x / delta.y;
caryclark@google.com27accef2012-01-25 18:57:23 +000019 axisIntercept = line[0].x - slope * line[0].y;
20 }
21 return moreHorizontal;
22}
23
24int reduceOrder(const _Line& line, _Line& reduced) {
25 reduced[0] = line[0];
26 int different = line[0] != line[1];
27 reduced[1] = line[different];
28 return 1 + different;
29}
caryclark@google.com6680fb12012-02-07 22:10:51 +000030
31void sub_divide(const _Line& line, double t1, double t2, _Line& dst) {
32 _Point delta;
33 tangent(line, delta);
34 dst[0].x = line[0].x - t1 * delta.x;
35 dst[0].y = line[0].y - t1 * delta.y;
36 dst[1].x = line[0].x - t2 * delta.x;
37 dst[1].y = line[0].y - t2 * delta.y;
38}
caryclark@google.comfa0588f2012-04-26 21:01:06 +000039
rmistry@google.comd6176b02012-08-23 18:14:13 +000040// may have this below somewhere else already:
caryclark@google.comfa0588f2012-04-26 21:01:06 +000041// copying here because I thought it was clever
42
43// Copyright 2001, softSurfer (www.softsurfer.com)
44// This code may be freely used and modified for any purpose
45// providing that this copyright notice is included with it.
46// SoftSurfer makes no warranty for this code, and cannot be held
47// liable for any real or imagined damage resulting from its use.
48// Users of this code must verify correctness for their application.
49
50// Assume that a class is already given for the object:
51// Point with coordinates {float x, y;}
52//===================================================================
53
54// isLeft(): tests if a point is Left|On|Right of an infinite line.
55// Input: three points P0, P1, and P2
56// Return: >0 for P2 left of the line through P0 and P1
57// =0 for P2 on the line
58// <0 for P2 right of the line
59// See: the January 2001 Algorithm on Area of Triangles
caryclark@google.combeda3892013-02-07 13:13:41 +000060// return (float) ((P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y));
61double is_left(const _Line& line, const _Point& pt) {
62 _Point P0 = line[1] - line[0];
63 _Point P2 = pt - line[0];
64 return P0.cross(P2);
caryclark@google.comfa0588f2012-04-26 21:01:06 +000065}
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000066
caryclark@google.com8dcf1142012-07-02 20:27:02 +000067double t_at(const _Line& line, const _Point& pt) {
68 double dx = line[1].x - line[0].x;
69 double dy = line[1].y - line[0].y;
70 if (fabs(dx) > fabs(dy)) {
71 if (approximately_zero(dx)) {
72 return 0;
73 }
74 return (pt.x - line[0].x) / dx;
75 }
76 if (approximately_zero(dy)) {
77 return 0;
78 }
79 return (pt.y - line[0].y) / dy;
80}
81
82static void setMinMax(double x, int flags, double& minX, double& maxX) {
83 if (minX > x && (flags & (kFindTopMin | kFindBottomMin))) {
84 minX = x;
85 }
86 if (maxX < x && (flags & (kFindTopMax | kFindBottomMax))) {
87 maxX = x;
88 }
89}
90
91void x_at(const _Point& p1, const _Point& p2, double top, double bottom,
92 int flags, double& minX, double& maxX) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000093 if (AlmostEqualUlps(p1.y, p2.y)) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +000094 // It should be OK to bail early in this case. There's another edge
rmistry@google.comd6176b02012-08-23 18:14:13 +000095 // which shares this end point which can intersect without failing to
caryclark@google.com8dcf1142012-07-02 20:27:02 +000096 // have a slope ... maybe
97 return;
98 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000100 // p2.x is always greater than p1.x -- the part of points (p1, p2) are
101 // moving from the start of the cubic towards its end.
102 // if p1.y < p2.y, minX can be affected
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 // if p1.y > p2.y, maxX can be affected
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000104 double slope = (p2.x - p1.x) / (p2.y - p1.y);
105 int topFlags = flags & (kFindTopMin | kFindTopMax);
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000106 if (topFlags && ((top <= p1.y && top >= p2.y)
107 || (top >= p1.y && top <= p2.y))) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000108 double x = p1.x + (top - p1.y) * slope;
109 setMinMax(x, topFlags, minX, maxX);
110 }
111 int bottomFlags = flags & (kFindBottomMin | kFindBottomMax);
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000112 if (bottomFlags && ((bottom <= p1.y && bottom >= p2.y)
113 || (bottom >= p1.y && bottom <= p2.y))) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000114 double x = p1.x + (bottom - p1.y) * slope;
115 setMinMax(x, bottomFlags, minX, maxX);
116 }
117}
118
119void xy_at_t(const _Line& line, double t, double& x, double& y) {
120 double one_t = 1 - t;
121 if (&x) {
122 x = one_t * line[0].x + t * line[1].x;
123 }
124 if (&y) {
125 y = one_t * line[0].y + t * line[1].y;
126 }
127}