Playing around with Kotlin and computational geometry algorithms. Visualizer inside.
Playing around with Kotlin and computational geometry algorithms.
Use this Gradle dependency or Maven equivalent:
repositories {
...
maven { url "https://jitpack.io" }
}
dependencies {
// choose the modules you need:
compile 'com.github.h0tk3y.geometry:geometry:v0.1'
compile 'com.github.h0tk3y.geometry:algorithms:v0.1'
compile 'com.github.h0tk3y.geometry:visualizer:v0.1'
compile 'com.github.h0tk3y.geometry:demos:v0.1'
// or, to add all the above:
compile 'com.github.h0tk3y:geometry:v0.1'
}
Convex hull
Polygon triangulation
Segments intersection
Minkowski addition for convex shapes
Geometry primitives like left turn predicate and distance are also there.
Zoom, moving, left click, right-click and drag callbacks are implemented. Moving is done with right mouse button drag, zooming -- with mouse scroll.
How to include one into your app:
parent as JPanel
val v = Visualizer()
parent.add(v)
That's it!
Or even simpler, there's Demo
app stub which you can use:
val demo = Demo()
val v = demo.visualizer()
demo.start()
See the demos attached for more examples.
How to use:
v as Visualizer
v.add(PointDrawable(0.5, 0.5))
v.add(PointDrawable(0.1, 0.3, Color.GREEN))
v.add(SegmentDrawable(0.1, 0.3, 0.5, 0.5))
v.area = Area(0.1, 0.1, 0.9, 0.9)
v.add((0..100).map { SegmentDrawable(0.0, it * 0.01, 1.0, it * 0.01 })
v.remove({ it is PointDrawable })
v.onClick { x, y ->
println("Clicked at $x, $y.")
v.add(PointDrawable(x, y, someRandomColor()))
}
v.onDrag { x0, y0, x1, y1 ->
val p0 = Point(x0, y0)
val p1 = Point(x1, y1)
if (distance(p0, p1) >= 0.3) {
println("Dragged so long! From $x0, $y0 to $x1, $y1.")
v.add(SegmentDrawable(p0, p1))
}
}