-
Notifications
You must be signed in to change notification settings - Fork 0
/
Three.scala
108 lines (86 loc) · 2.74 KB
/
Three.scala
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
import processing.core
import processing.core.*
import processing.core.PApplet
import scala.math.Pi
import scala.math.cos
import scala.math.sin
@main def runThree(args: String*): Unit =
PApplet.main("Three")
class Three extends PApplet:
// The actual size of our canvas
val Width = 1000
val Height = 1000
// The actual size we'll work with for our flows with extra margin
val leftX = (Width * -0.5).toInt
val rightX = (Width * 1.5).toInt
val topY = (Height * -0.5).toInt
val bottomY = (Height * 1.5).toInt
// The resolution, which will impact how many cols and rows are on the canvas
val resolution = (Width * 0.01).toInt
val numCols = (rightX - leftX) / resolution
val numRows = (bottomY - topY) / resolution
val grid = Array.ofDim[Double](numCols, numRows)
override def settings(): Unit =
size(Width, Height)
override def draw(): Unit =
background(255)
for col <- 0 until numCols do
for row <- 0 until numRows do
grid(col)(row) = ((row.toDouble / numRows.toDouble) * Pi)
for col <- 0 until numCols do
for row <- 0 until numRows do
drawArrow(
col * resolution,
row * resolution,
grid(col)(row),
resolution - 2
)
var x: Double = 500
var y: Double = 300
val stepLength = 10
val steps = 300
for step <- 0 until steps do
drawPoint(x, y)
val xOffset: Double = x - leftX
val yOffset: Double = y - topY
val columnIndex: Int = (xOffset / resolution).toInt
val rowIndex: Int = (yOffset / resolution).toInt
if (columnIndex > 0 && columnIndex < grid.length)
&& (rowIndex > 0 && rowIndex < grid(columnIndex).length)
then
val gridAngle: Double = grid(columnIndex)(rowIndex)
val xStep: Double = stepLength * cos(gridAngle)
val yStep: Double = stepLength * sin(gridAngle)
x += xStep
y += yStep
save("flow-fields-article-3c.png")
exit
end draw
def drawPoint(x: Double, y: Double): Unit =
val baseWeight = g.strokeWeight
val baseStroke = g.strokeColor
stroke(255, 0, 0)
strokeWeight(10)
point(x.toFloat, y.toFloat)
strokeWeight(baseWeight)
stroke(baseStroke)
/** Given x and y coordinates we "GOTO" that part of the grid and then draw an
* arrow showing the given angle.
*/
def drawArrow(x: Double, y: Double, angle: Double, len: Double): Unit =
pushMatrix()
translate(x.toFloat, y.toFloat)
rotate(angle.toFloat)
val arrowSize = 2
val lineLength = len.toFloat - arrowSize
line(0, 0, lineLength, 0)
triangle(
lineLength,
0,
lineLength - arrowSize,
(arrowSize / 2).toFloat,
lineLength - arrowSize,
(-arrowSize / 2).toFloat
)
popMatrix()
end Three