From b41ba5fdfc41b9a25e7b9321d4c73dd1cf6f60a3 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 16 Oct 2020 21:31:06 +0200 Subject: [PATCH] [orx-shapes] Add linear operator and contour conversion to bezierpatch --- orx-shapes/src/main/kotlin/BezierPatch.kt | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/orx-shapes/src/main/kotlin/BezierPatch.kt b/orx-shapes/src/main/kotlin/BezierPatch.kt index c0878edf0..6b4f79f0b 100644 --- a/orx-shapes/src/main/kotlin/BezierPatch.kt +++ b/orx-shapes/src/main/kotlin/BezierPatch.kt @@ -71,14 +71,14 @@ class BezierPatch(val points: List>) { val f0 = List(4) { MutableList(3) { Vector2.ZERO } } for (j in 0 until 4) { for (i in 0 until 3) { - f0[j][i] = points[j][i+1] - points[j][i] + f0[j][i] = points[j][i + 1] - points[j][i] } } val f1 = List(3) { MutableList(3) { Vector2.ZERO } } for (j in 0 until 3) { for (i in 0 until 3) { - f1[j][i] = f0[j+1][i] - f0[j][i] + f1[j][i] = f0[j + 1][i] - f0[j][i] } } @@ -138,8 +138,26 @@ class BezierPatch(val points: List>) { return bezierPatch(d0, d1, d2, d3).transposed } + + val contour: ShapeContour = ShapeContour( + listOf( + Segment(points[0][0], points[0][1], points[0][2], points[0][3]), + Segment(points[0][3], points[1][3], points[2][3], points[3][3]), + Segment(points[3][3], points[3][2], points[3][1], points[3][0]), + Segment(points[3][0], points[2][0], points[1][0], points[0][0]), + ), true) + + operator fun times(scale: Double) = BezierPatch(points.map { j -> j.map { i -> i * scale } }) + operator fun div(scale: Double) = BezierPatch(points.map { j -> j.map { i -> i / scale } }) + operator fun plus(right: BezierPatch) = + BezierPatch(List(4) { j -> List(4) { i -> points[j][i] + right.points[j][i] } }) + + operator fun minus(right: BezierPatch) = + BezierPatch(List(4) { j -> List(4) { i -> points[j][i] - right.points[j][i] } }) + } + /** * Create a cubic bezier patch from 4 segments. The control points of the segments are used in row-wise fashion */