Skip to content

Commit

Permalink
Add divine divide
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Jun 6, 2024
1 parent 0716018 commit 1aeb3dd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
2 changes: 2 additions & 0 deletions arts/rects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
![rect](rects2.png)

![rect-over](rects-over.png)

![divine-divide](divine-divide.png)
Binary file added arts/rects/divine-divide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.oblac.gart.rects.divide

import dev.oblac.gart.Gart
import dev.oblac.gart.color.Colors
import dev.oblac.gart.gfx.*
import dev.oblac.gart.math.rndi
import dev.oblac.gart.shader.createNoiseGrainFilter
import org.jetbrains.skia.*
import kotlin.math.max
import kotlin.math.min

val gart = Gart.of(
"divine-divide",
1024, 1024
)

fun main() {

val g = gart.gartvas()
val c = g.canvas
c.clear(Colors.blackColor.toColor())

val gap = 40f // we don't want any random

var rects = arrayOf(Rect(0f, 0f, gart.d.rect.width, gart.d.rect.height))
repeat(5) {
rects = divideRects(rects, gap)
}

rects.forEach { rect ->
fillRect(c, rect, Colors.lightSalmonColor, Colors.lightSalmonColor)
c.drawRect(rect, strokeOfWhite(10f).also { it.strokeCap = PaintStrokeCap.ROUND })
}

c.drawBorder(gart.d, strokeOfWhite(10f))
gart.saveImage(g)
gart.window().showImage(g)
}

fun fillRect(c: Canvas, rect: Rect, color: Color4f, colorAlt: Color4f) {
c.drawRect(rect, fillOf(Color.WHITE))

if (max(rect.width, rect.height) / min(rect.width, rect.height) > 6f) {
c.drawRect(rect.shrink(10f), fillOf(colorAlt).also { it.imageFilter = createNoiseGrainFilter(2f, rect.dimension()) })
return
}

var gap = 1f
var verticalP = rect.topLeftPoint()
var horizontalP = rect.topLeftPoint()

var i = 0f
while (true) {
i += 0.1f
gap += 0.07f * i
verticalP = if (verticalP.y < rect.bottom) {
verticalP.offset(0f, gap)
} else {
verticalP.offset(gap, 0f)
}
horizontalP = if (horizontalP.x < rect.right) {
horizontalP.offset(gap, 0f)
} else {
horizontalP.offset(0f, gap)
}

if (verticalP.x >= rect.right && horizontalP.y >= rect.bottom) {
break
}
c.drawLine(verticalP, horizontalP, strokeOf(color, 1.5f))
}
}

fun divideRects(startingRect: Array<Rect>, gap: Float): Array<Rect> {
val newRects = mutableListOf<Rect>()

startingRect.forEach { rect ->
if (rect.width < gap * 4 || rect.height < gap * 4) {
newRects.add(rect)
return@forEach
}

val divideWidth = rndi(1, (rect.width / gap).toInt()) * gap
val divideHeight = rndi(1, (rect.height / gap).toInt()) * gap

val rect1 = Rect(rect.left, rect.top, rect.left + divideWidth, rect.top + divideHeight) // top left
val rect2 = Rect(rect.left + divideWidth, rect.top, rect.right, rect.top + divideHeight) // top right
val rect3 = Rect(rect.left, rect.top + divideHeight, rect.left + divideWidth, rect.bottom) // bottom left
val rect4 = Rect(rect.left + divideWidth, rect.top + divideHeight, rect.right, rect.bottom) // bottom right

newRects.addAll(listOf(rect1, rect2, rect3, rect4))
}

return newRects.toTypedArray()
}
6 changes: 5 additions & 1 deletion gart/src/main/kotlin/dev/oblac/gart/gfx/canvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ fun Canvas.drawPointsAsCircles(points: Collection<Point>, stroke: Paint, radius:
fun Canvas.drawLine(p1: Point, p2: Point, stroke: Paint) = this.drawLine(p1.x, p1.y, p2.x, p2.y, stroke)

fun Canvas.drawBorder(d: Dimension, width: Float, color: Int) {
this.drawBorder(d, strokeOf(color, width))
}

fun Canvas.drawBorder(d: Dimension, stroke: Paint) {
val width = stroke.strokeWidth
val w2 = width / 2
val stroke = strokeOf(color, width)
this.drawLine(0f, w2, d.wf, w2, stroke)
this.drawLine(w2, w2, w2, d.hf - w2, stroke)
this.drawLine(d.wf - w2, w2, d.wf - w2, d.hf - w2, stroke)
Expand Down
2 changes: 2 additions & 0 deletions gart/src/main/kotlin/dev/oblac/gart/gfx/rectangle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ fun Rect.dimension(): Dimension {
fun Rect.topLeftPoint(): Point {
return Point(this.left, this.top)
}

fun Rect.Companion.ofPWH(left: Float, top: Float, w: Float, h: Float): Rect = Rect(left, top, left + w, top + h)

0 comments on commit 1aeb3dd

Please sign in to comment.