-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLine.pde
More file actions
109 lines (97 loc) · 3.02 KB
/
Line.pde
File metadata and controls
109 lines (97 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Line and its Coordinate System
// ==============================
//
// Line in 2D cartesian spacecan be defined implicitly by equation
//
// ax + by + c = 0.
//
// Such implicit definition is associated with a _line coordinate system_. Every
// plane point (_x_, _y_) be uniquely represented in the line's (_s_, _t_)
// coordinates as follows:
//
// (x, y) = O + sT + tN,
//
// where _O_ is the origin of the coordinate system and is defined as the
// projection of cartesian origin (0, 0) onto the line, whereas,
// _T_ = (-_b_, _a_) and _N_ = (_a_, _b_) are the tangent and normal vectors of
// the line.
//
// **Note:** Different implicit definitions of the same line may yield different
// coordinate systems.
// Line Class
// ----------
//
// Represents an implicit line definition and provides routines to work with
// its coordinate system
public static class Line
{
public float a;
public float b;
public float c;
// Creates line with given coefficients
public Line(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
}
// Gets the line origin defined as the projection of the (0, 0) cartesian
// point onto the line. This is the origin of this line's coordinate system.
public PVector origin() {
float c1 = -c / (a * a + b * b);
return new PVector(a * c1, b * c1);
}
// Gets the (`-b`, `a`) vector. This is the base vector for the _s_ line
// coordinate.
public PVector tangent() {
return new PVector(-b, a);
}
// Gets the (`a`, `b`) vector. This is the base vector for the _t_ line
// coordinate.
public PVector normal() {
return new PVector(a, b);
}
// Converts point from (_x_, _y_) cartesian coordinates to (s, t) line
// coordinates
public LVector map(PVector p) {
float c1 = a * a + b * b;
float t = (a * p.x + b * p.y + c) / c1;
float c2 = c / c1 - t;
float s = abs(a) > abs(b) ? (p.y + b * c2) / a : -(p.x + a * c2) / b;
return new LVector(s, t);
}
// Converts point from (_s_, _t_) line coordinates to cartesian (_x_, _y_)
// coordinates
public PVector unmap(LVector l) {
return unmap(l.s, l.t);
}
// Converts point from (_s_, _t_) line coordinates to cartesian (_x_, _y_)
// coordinates
public PVector unmap(float s, float t) {
final float c1 = t - c / (a * a + b * b);
return new PVector(c1 * a - s * b, c1 * b + s * a);
}
// Converts point given in this line's coordinates to the coordinate
// system of another line
public LVector remap(Line other, LVector l) {
return remap(other, l.s, l.t);
}
// Converts point given in this line's coordinates to the coordinate
// system of another line
public LVector remap(Line other, float s, float t) {
return map(other.unmap(s, t));
}
}
// LVector Class
// -------------
//
// Represents a point in line (_s_, _t_) coordinates
public static class LVector
{
public float s;
public float t;
// Creates new point with given coordinates
public LVector(float s, float t) {
this.s = s;
this.t = t;
}
}