-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSix.scala
More file actions
90 lines (69 loc) · 2.42 KB
/
Six.scala
File metadata and controls
90 lines (69 loc) · 2.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import processing.core
import processing.core.*
import processing.core.PApplet
import scala.math.Pi
import scala.math.cos
import scala.math.sin
import scala.util.Random
@main def runSix(args: String*): Unit =
PApplet.main("Six")
class Six 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
val scaledX = col * 0.005
val scaledY = row * 0.005
// We switch this here to return the angle with perlin noise
val angle = noise(scaledX.toFloat, scaledY.toFloat) * Pi * 2
grid(col)(row) = angle
val stepLength = 100
val steps = 100
val lineCount = 1000
(0 until lineCount).foreach: count =>
drawCurve(steps, count, stepLength)
end draw
def drawCurve(steps: Int, count: Int, stepLength: Int) =
var x: Double = Random().between(0, rightX)
var y: Double = Random().between(0, bottomY)
(0 until steps).foreach: _ =>
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)
val nextX = x + xStep
val nextY = y + yStep
drawPoint(x, y)
x = nextX
y = nextY
end drawCurve
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)
end Six