-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment.pde
56 lines (51 loc) · 1.32 KB
/
Segment.pde
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
abstract class Segment implements Curve
{
private float angL;
private float angH;
private PVector point1;
private PVector point2;
protected PVector relP1;
protected PVector relP2;
private Collection<PVector> points;
private Collection<PVector> empty;
protected PVector point;
private boolean rollover;
protected Segment(PVector c, PVector p1, PVector p2)
{
point1 = p1;
point2 = p2;
relP1 = PVector.sub(p1, c);
relP2 = PVector.sub(p2, c);
float ang1 = (relP1.heading() + TWO_PI) % TWO_PI;
float ang2 = (relP2.heading() + TWO_PI) % TWO_PI;
angL = min(ang1, ang2);
angH = max(ang1, ang2);
rollover = (angH - angL > PI);
points = new HashSet<PVector>();
empty = new HashSet<PVector>();
point = new PVector();
points.add(point);
}
private boolean intersectsPoint(float angle, float tangent)
{
if(rollover)
{
return angle >= angH || angle <= angL;
}
return angle >= angL && angle <= angH;
}
public void draw()
{
line(point1.x, point1.y, point2.x, point2.y);
}
public Collection<PVector> getPoints(float angle, float tangent)
{
if(intersectsPoint(angle, tangent))
{
setPoint(angle, tangent);
return points;
}
return empty;
}
protected abstract void setPoint(float angle, float tangent);
}