[orx-composition] Add test and demo for PR#360
This commit is contained in:
43
openrndr-demos/src/demo/kotlin/DemoClipping.kt
Normal file
43
openrndr-demos/src/demo/kotlin/DemoClipping.kt
Normal file
@@ -0,0 +1,43 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.extra.composition.ClipMode
|
||||
import org.openrndr.extra.composition.composition
|
||||
import org.openrndr.extra.composition.drawComposition
|
||||
import org.openrndr.math.Vector2
|
||||
import org.openrndr.shape.Circle
|
||||
import org.openrndr.shape.LineSegment
|
||||
import org.openrndr.shape.Shape
|
||||
|
||||
fun main() = application {
|
||||
program {
|
||||
val outline = Shape(
|
||||
listOf(
|
||||
Circle(drawer.bounds.center, 70.0).contour.reversed,
|
||||
Circle(drawer.bounds.center, 100.0).contour,
|
||||
)
|
||||
)
|
||||
|
||||
val radius = outline.bounds.dimensions.length / 2
|
||||
val off = outline.bounds.center
|
||||
val num = radius.toInt()
|
||||
|
||||
val svg = drawComposition {
|
||||
lineSegments(List(num) { segNum ->
|
||||
val yNorm = (segNum / (num - 1.0))
|
||||
val x = ((segNum % 2) * 2.0 - 1.0) * radius
|
||||
val y = (yNorm * 2.0 - 1.0) * radius
|
||||
val start = Vector2(-x, y) + off
|
||||
val end = Vector2(x, y) + off
|
||||
LineSegment(start, end)
|
||||
})
|
||||
clipMode = ClipMode.INTERSECT
|
||||
shape(outline)
|
||||
}
|
||||
extend {
|
||||
drawer.clear(ColorRGBa.PINK)
|
||||
drawer.fill = null
|
||||
drawer.shape(outline)
|
||||
drawer.composition(svg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
package org.openrndr.extra.composition
|
||||
|
||||
import org.openrndr.math.Vector2
|
||||
import org.openrndr.shape.Circle
|
||||
import org.openrndr.shape.LineSegment
|
||||
import org.openrndr.shape.Rectangle
|
||||
import org.openrndr.shape.Shape
|
||||
import kotlin.test.*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class TestComposition {
|
||||
val composition = let { _ ->
|
||||
@@ -35,4 +42,46 @@ class TestComposition {
|
||||
assertNull(composition.findImage("outer"))
|
||||
assertNull(composition.findImage("shape"))
|
||||
}
|
||||
}
|
||||
|
||||
class TestCompositionIntersections {
|
||||
val bounds = Rectangle(Vector2.ZERO, 640.0, 480.0)
|
||||
val outline = Shape(
|
||||
listOf(
|
||||
Circle(bounds.center, 70.0).contour.reversed,
|
||||
Circle(bounds.center, 100.0).contour,
|
||||
)
|
||||
)
|
||||
|
||||
val radius = outline.bounds.dimensions.length / 2
|
||||
val off = outline.bounds.center
|
||||
val num = radius.toInt()
|
||||
|
||||
@Test
|
||||
fun `use a shape as a mask for line segments`() {
|
||||
// Make sure intersections do not fail randomly, which was fixed in
|
||||
// https://github.com/openrndr/orx/commit/e8f50b3dd153ed82de121e9017cf42f6ea95ac8e
|
||||
val svg = List(100) {
|
||||
drawComposition {
|
||||
lineSegments(List(num) { segNum ->
|
||||
val yNorm = (segNum / (num - 1.0))
|
||||
val x = ((segNum % 2) * 2.0 - 1.0) * radius
|
||||
val y = (yNorm * 2.0 - 1.0) * radius
|
||||
val start = Vector2(-x, y) + off
|
||||
val end = Vector2(x, y) + off
|
||||
LineSegment(start, end)
|
||||
})
|
||||
clipMode = ClipMode.INTERSECT
|
||||
shape(outline)
|
||||
}
|
||||
}
|
||||
|
||||
val shapes = svg[50].findShapes()
|
||||
val dimensions = svg[50].bounds.dimensions
|
||||
|
||||
assertTrue(shapes.isNotEmpty())
|
||||
assertTrue(shapes.first().shape.contours.isNotEmpty())
|
||||
assertTrue(dimensions.x > 0.0)
|
||||
assertTrue(dimensions.y > 0.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user