-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaneSection.pde
More file actions
74 lines (60 loc) · 1.74 KB
/
Copy pathPlaneSection.pde
File metadata and controls
74 lines (60 loc) · 1.74 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
class PlaneSection
{
PVector _pos1;
PVector _pos2;
PVector _normal;
float[] _coefs = new float[4];
// Constructor to make a plane from two points (assuming Z = 0)
// The two points define the edges of the finite plane section
PlaneSection(float x1, float y1, float x2, float y2, boolean invert)
{
_pos1 = new PVector(x1, y1);
_pos2 = new PVector(x2, y2);
setCoefficients();
calculateNormal(invert);
}
PVector getPoint1()
{
return _pos1;
}
PVector getPoint2()
{
return _pos2;
}
void setCoefficients()
{
PVector v = new PVector(_pos2.x - _pos1.x, _pos2.y - _pos1.y, 0.0);
PVector z = new PVector(_pos2.x - _pos1.x, _pos2.y - _pos1.y, 1.0);
_coefs[0] = v.y*z.z - z.y*v.z;
_coefs[1] = -(v.x*z.z - z.x*v.z);
_coefs[2] = v.x*z.y - z.x*v.y;
_coefs[3] = -_coefs[0]*_pos1.x - _coefs[1]*_pos1.y - _coefs[2]*_pos1.z;
}
void calculateNormal(boolean inverted)
{
_normal = new PVector(_coefs[0], _coefs[1], _coefs[2]);
_normal.normalize();
if (inverted)
_normal.mult(-1);
}
float getDistance(PVector p)
{
float d = (_coefs[0]*p.x + _coefs[1]*p.y + _coefs[2]*p.z + _coefs[3]) / (sqrt(_coefs[0]*_coefs[0] + _coefs[1]*_coefs[1] + _coefs[2]*_coefs[2]));
return abs(d);
}
PVector getNormal()
{
return _normal;
}
void draw()
{
stroke(0, 0, 0);
strokeWeight(5);
// Plane representation:
line(_pos1.x, _pos1.y, _pos2.x, _pos2.y);
float cx = _pos1.x*0.5 + _pos2.x*0.5;
float cy = _pos1.y*0.5 + _pos2.y*0.5;
// Normal vector representation:
line(cx, cy, cx + 5.0*_normal.x, cy + 5.0*_normal.y);
}
}