[orx-triangulation] Improve triangulation, add kotlin/js support

This commit is contained in:
Edwin Jakobs
2022-10-21 10:33:24 +02:00
parent ed6cda8cca
commit 1f16aa6a31
21 changed files with 1608 additions and 251 deletions

View File

@@ -0,0 +1,32 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.noise.scatter
import org.openrndr.extra.triangulation.delaunayTriangulation
import org.openrndr.math.Vector2
import org.openrndr.shape.Circle
fun main() {
application {
configure {
width = 800
height = 800
title = "Delaunator"
}
program {
val circle = Circle(Vector2(400.0), 250.0)
val points = circle.shape.scatter(30.0)
val delaunay = (points + circle.contour.equidistantPositions(40)).delaunayTriangulation()
val triangles = delaunay.triangles().map { it.contour }
extend {
drawer.clear(ColorRGBa.BLACK)
for ((i, triangle) in triangles.withIndex()) {
drawer.fill = ColorRGBa.PINK.shade(1.0 - i / (triangles.size * 1.2))
drawer.stroke = ColorRGBa.PINK.shade( i / (triangles.size * 1.0) + 0.1)
drawer.contour(triangle)
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.noise.poissonDiskSampling
import org.openrndr.extra.triangulation.delaunayTriangulation
import org.openrndr.math.Vector2
import org.openrndr.shape.Rectangle
fun main() {
application {
configure {
width = 800
height = 800
}
program {
val frame = Rectangle.fromCenter(Vector2(400.0), 600.0, 600.0)
val points = poissonDiskSampling(frame, 50.0).map { it + frame.corner }
val delaunay = points.delaunayTriangulation()
val halfedges = delaunay.halfedges()
val hull = delaunay.hull()
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.fill = null
drawer.stroke = ColorRGBa.PINK
drawer.contours(halfedges)
drawer.stroke = ColorRGBa.GREEN
drawer.contour(hull)
}
}
}
}

View File

@@ -0,0 +1,35 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.noise.poissonDiskSampling
import org.openrndr.extra.triangulation.delaunayTriangulation
import org.openrndr.math.Vector2
import org.openrndr.shape.Circle
import org.openrndr.shape.Rectangle
fun main() {
application {
configure {
width = 800
height = 800
}
program {
val circle = Circle(Vector2(400.0), 250.0)
val frame = Rectangle.fromCenter(Vector2(400.0), 600.0, 600.0)
val points = poissonDiskSampling(drawer.bounds, 30.0)
.filter { circle.contains(it) }
val delaunay = (points + circle.contour.equidistantPositions(40)).delaunayTriangulation()
val voronoi = delaunay.voronoiDiagram(frame)
val cells = voronoi.cellPolygons()
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.fill = null
drawer.stroke = ColorRGBa.PINK
drawer.contours(cells)
}
}
}
}

View File

@@ -0,0 +1,33 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.shapes.grid
import org.openrndr.extra.triangulation.delaunayTriangulation
import org.openrndr.shape.Circle
fun main() {
application {
configure {
width = 720
height = 720
}
program {
extend {
val r = drawer.bounds.offsetEdges(-50.0)
val grid = r.grid(8, 8).flatten()
val circles = grid.map { Circle(it.center, it.width / 4.0) }
val points = circles.flatMap { it.contour.equidistantPositions(6) }
drawer.circles(points, 5.0)
val d = points.delaunayTriangulation()
drawer.stroke = ColorRGBa.PINK
drawer.contours(d.halfedges())
drawer.stroke = ColorRGBa.YELLOW
drawer.fill = null
drawer.contours(d.voronoiDiagram(drawer.bounds.offsetEdges(-50.0)).cellPolygons())
drawer.stroke = ColorRGBa.GRAY
drawer.contours(d.triangles().map { it.contour })
}
}
}
}

View File

@@ -0,0 +1,41 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.shapes.grid
import org.openrndr.extra.triangulation.delaunayTriangulation
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.math.transforms.buildTransform
import org.openrndr.shape.Circle
fun main() {
application {
configure {
width = 750
height = 1000
}
program {
extend {
val r = drawer.bounds.offsetEdges(-100.0)
val grid = r.grid(3,6).flatten()
val circles = grid.map { Circle(Vector2.ZERO, 158.975).contour.transform(
buildTransform {
translate(it.center)
rotate(Vector3.UNIT_Z, 0.0)
}
) }
val points = circles.flatMap { it.contour.equidistantPositions(16).take(16) }
drawer.circles(points, 5.0)
val d = points.delaunayTriangulation()
drawer.stroke = ColorRGBa.PINK
drawer.contours(d.halfedges())
drawer.stroke = ColorRGBa.YELLOW
drawer.fill = ColorRGBa.GRAY.opacify(0.5)
drawer.contours(d.voronoiDiagram(drawer.bounds).cellPolygons())
drawer.stroke = ColorRGBa.GRAY
drawer.contours(d.triangles().map { it.contour })
}
}
}
}