-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOne.scala
More file actions
67 lines (53 loc) · 1.78 KB
/
One.scala
File metadata and controls
67 lines (53 loc) · 1.78 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
import processing.core
import processing.core.*
import processing.core.PApplet
import scala.math.Pi
@main def runOne(args: String*): Unit =
PApplet.main("One")
class One 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)
// Drawing out all the arrows in the grid
for col <- 0 until numCols do
for row <- 0 until numRows do
val x = (col * resolution)
val y = (row * resolution)
val angle = Pi * 0.25
drawArrow(x, y, angle, resolution - 2)
/** 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 =
// To better understand what is going on here with push/popMatrix,
// check out https://processing.org/tutorials/transform2d.
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 One