Last active
February 12, 2022 12:00
-
-
Save thelumiereguy/35bc6ca9b8c6ac1975b49df05dbbb6a0 to your computer and use it in GitHub Desktop.
An example made with K5 Compose (Compose for Desktop) to draw nested grids, each shifted by a certain angle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.ui.geometry.Offset | |
import androidx.compose.ui.graphics.BlendMode | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.ExperimentalGraphicsApi | |
import androidx.compose.ui.graphics.PointMode | |
import androidx.compose.ui.graphics.drawscope.translate | |
import dev.romainguy.kotlin.math.Float2 | |
import dev.romainguy.kotlin.math.Mat2 | |
import k5 | |
import math.toDegrees | |
import kotlin.math.* | |
const val ROW_COLUMN_OFFSET = 50 | |
val HALF_SCALE_MATRIX = Mat2( | |
Float2(0.7f, 0f), | |
Float2(0f, 0.7f) | |
) | |
fun getRotationMatrix(angle: Float): Mat2 { | |
return Mat2( | |
x = Float2( | |
cos(angle), | |
-sin(angle) | |
), | |
y = Float2( | |
sin(angle), | |
cos(angle) | |
) | |
) | |
} | |
const val MAX_ITERATION = 10 | |
@ExperimentalGraphicsApi | |
fun showGridception() = k5 { | |
val startTimeStamp = System.currentTimeMillis() | |
val halfHeight = (size.height / 2).roundToInt() | |
val halfWidth = (size.width / 2).roundToInt() | |
show { | |
with(it) { | |
translate( | |
halfWidth.toFloat(), | |
halfHeight.toFloat() | |
) { | |
for (y in -halfHeight..halfHeight step ROW_COLUMN_OFFSET) { | |
for (x in -halfWidth..halfWidth step ROW_COLUMN_OFFSET) { | |
val pointPos = Float2( | |
x.toFloat(), | |
y.toFloat() | |
) | |
val angle = atan2( | |
pointPos.y, | |
pointPos.x | |
) + PI | |
drawPoints( | |
getPoints( | |
pointPos, | |
System.currentTimeMillis() - startTimeStamp | |
), | |
PointMode.Polygon, | |
Color.hsl( | |
angle.toFloat().toDegrees(), | |
0.5f, | |
0.5f | |
), | |
0.5f, | |
blendMode = BlendMode.Lighten | |
) | |
} | |
} | |
} | |
} | |
} | |
} | |
tailrec fun getPoints( | |
pointPos: Float2, | |
timeStampOffset: Long, | |
points: MutableList<Float2> = mutableListOf(), | |
iteration: Int = 1 | |
): List<Offset> { | |
if (iteration > MAX_ITERATION) { | |
return points.map { it.toOffset() } | |
} | |
val newPos = HALF_SCALE_MATRIX * getRotationMatrix( | |
(timeStampOffset / iteration) * 0.001f | |
) * pointPos | |
points.add(newPos) | |
return getPoints( | |
newPos, | |
timeStampOffset, | |
points, | |
iteration + 1 | |
) | |
} | |
private fun Float2.toOffset() = Offset(x, y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You would need two dependencies for this to run though. | |
K5 Compose - https://github.com/CuriousNikhil/k5-compose | |
Kotlin Math - https://github.com/romainguy/kotlin-math |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment