-
Notifications
You must be signed in to change notification settings - Fork 0
/
DashLine.pde
69 lines (62 loc) · 1.79 KB
/
DashLine.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
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Derived from
J David Eisenberg
https://openprocessing.org/sketch/7013/#
*/
class DashLine {
float [] dashes = {5, 3, 6, 4}; //default
DashLine() {
}
/*
* Define a dashed line with given dash and gap length.
* dash - length of dashed line in pixels
* gap - space between dashes in pixels
*/
DashLine(float dash, float gap) {
float[] spacing = { dash, gap };
dashes = spacing;
}
DashLine(float[] spacing) {
dashes = spacing;
}
/*
Draw the line from x0, y0 to x1, y1
*/
void dline(float x0, float y0, float x1, float y1)
{
float distance = dist(x0, y0, x1, y1);
float [ ] xSpacing = new float[dashes.length];
float [ ] ySpacing = new float[dashes.length];
float drawn = 0.0; // amount of distance drawn
if (distance > 0)
{
int i;
boolean drawLine = true; // alternate between dashes and gaps
/*
Figure out x and y distances for each of the spacing values
I decided to trade memory for time; I'd rather allocate
a few dozen bytes than have to do a calculation every time
I draw.
*/
for (i = 0; i < dashes.length; i++)
{
xSpacing[i] = lerp(0, (x1 - x0), dashes[i] / distance);
ySpacing[i] = lerp(0, (y1 - y0), dashes[i] / distance);
}
i = 0;
while (drawn < distance)
{
if (drawLine)
{
line(x0, y0, x0 + xSpacing[i], y0 + ySpacing[i]);
}
x0 += xSpacing[i];
y0 += ySpacing[i];
/* Add distance "drawn" by this line or gap */
drawn = drawn + mag(xSpacing[i], ySpacing[i]);
i = (i + 1) % dashes.length; // cycle through array
drawLine = !drawLine; // switch between dash and gap
}
}
}
}