Skip to content

Commit

Permalink
Support drawing polylines and polyArrows using the VectorBuilder (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Jun 28, 2023
1 parent 6a722ba commit c8f237a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ fun PointList.mapPoints(gen: (p: Point) -> Point): PointList {
}

fun List<Point>.toPointArrayList(): PointArrayList = PointArrayList(size).also { for (p in this) it.add(p) }
fun Array<out Point>.toPointArrayList(): PointArrayList = PointArrayList(size).also { for (p in this) it.add(p) }

open class PointArrayList(capacity: Int = 7) : PointList, Extra by Extra.Mixin() {
override var closed: Boolean = false
private val data = FloatArrayList(capacity * 2)
override val size get() = data.size / 2

fun isEmpty() = size == 0
fun isNotEmpty() = size != 0

fun clear(): PointArrayList {
data.clear()
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import korlibs.math.roundDecimalPlaces
import kotlin.math.*

sealed interface IVectorArrayList : Extra {
fun isEmpty(): Boolean = size == 0
fun isNotEmpty(): Boolean = size != 0

val closed: Boolean
val size: Int
val dimensions: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ interface VectorBuilder {
for (i in 1 until path.size) lineTo(path[i])
if (close) close()
}
fun polygon(path: List<Point>, close: Boolean = true) = polygon(path.toPointArrayList(), close = close)
fun polygon(vararg path: Point, close: Boolean = true) = polygon(path.toPointArrayList(), close = close)

fun polyline(points: PointList, close: Boolean = false): Unit = polygon(points, close = close)
fun polyline(points: List<Point>, close: Boolean = false): Unit = polyline(points.toPointArrayList(), close = close)
fun polyline(vararg points: Point, close: Boolean = false): Unit = polyline(points.toPointArrayList(), close = close)

fun moveToH(x: Float) = moveTo(Point(x, lastPos.yF))
fun moveToV(y: Float) = moveTo(Point(lastPos.xF, y))
Expand Down Expand Up @@ -276,8 +282,6 @@ interface VectorBuilder {
}

fun <T> transformed(m: Matrix, block: VectorBuilder.() -> T): T = block(this.transformed(m))


}

private fun VectorBuilder._regularPolygonStar(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package korlibs.math.geom.vector

import korlibs.math.annotations.*
import korlibs.math.geom.*

fun VectorBuilder.arrowTo(p: Point, capEnd: ArrowCap = ArrowCap.Line(null), capStart: ArrowCap = ArrowCap.NoCap) {
Expand Down Expand Up @@ -42,3 +43,18 @@ interface ArrowCap {
override fun VectorBuilder.append(p0: Point, p1: Point, width: Float) = circle(p1, radius ?: (10f))
}
}

/** Creates a polyline from [points] adding arrow caps ([capEnd] and [capStart]) in each segment. Useful for displaying directed graphs */
fun VectorBuilder.polyArrows(points: PointList, capEnd: ArrowCap = ArrowCap.Line(), capStart: ArrowCap = ArrowCap.NoCap) {
if (points.isEmpty()) return
moveTo(points[0])
for (n in 1 until points.size) arrowTo(points[n], capEnd, capStart)
}

/** Creates a polyline from [points] adding arrow caps ([capEnd] and [capStart]) in each segment. Useful for displaying directed graphs */
@OptIn(KormaExperimental::class)
fun VectorBuilder.polyArrows(vararg points: Point, capEnd: ArrowCap = ArrowCap.Line(), capStart: ArrowCap = ArrowCap.NoCap) =
polyArrows(pointArrayListOf(*points), capEnd, capStart)
/** Creates a polyline from [points] adding arrow caps ([capEnd] and [capStart]) in each segment. Useful for displaying directed graphs */
fun VectorBuilder.polyArrows(points: List<Point>, capEnd: ArrowCap = ArrowCap.Line(), capStart: ArrowCap = ArrowCap.NoCap) =
polyArrows(points.toPointArrayList(), capEnd, capStart)

0 comments on commit c8f237a

Please sign in to comment.