Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curve on livemap #1021

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move the curve drawing curve by control points to Context2d.
  • Loading branch information
OLarionova-HORIS committed Feb 19, 2024
commit 3635fe0824c35c30f384459462ba6394783865fc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ interface Context2d {
fun setTransform(m11: Double, m12: Double, m21: Double, m22: Double, dx: Double, dy: Double)
fun setLineDash(lineDash: DoubleArray)
fun measureText(str: String): Double

// https://github.com/d3/d3/blob/9364923ee2b35ec2eb80ffc4bdac12a7930097fc/src/svg/line.js#L236
fun bSplineInterpolation(points: List<Vec<*>>) {
fun lineDot4(a: List<Double>, b: List<Double>): Double {
// returns the dot product of the given four-element vectors
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
}
// Matrix to transform basis (b-spline) control points to bezier control points.
val lineBasisBezier1 = listOf(0.0, 2.0 / 3.0, 1.0 / 3.0, 0.0)
val lineBasisBezier2 = listOf(0.0, 1.0 / 3.0, 2.0 / 3.0, 0.0)
val lineBasisBezier3 = listOf(0.0, 1.0 / 6.0, 2.0 / 3.0, 1.0 / 6.0)

val px = arrayListOf(points[0].x, points[0].x, points[0].x, points[1].x)
val py = arrayListOf(points[0].y, points[0].y, points[0].y, points[1].y)

moveTo(points[0].x, points[0].y)
lineTo(
lineDot4(lineBasisBezier3, px),
lineDot4(lineBasisBezier3, py)
)
for (i in 2..points.size) {
val curPoint = if (i < points.size) points[i] else points.last()
px.removeFirst(); px.add(curPoint.x)
py.removeFirst(); py.add(curPoint.y)
bezierCurveTo(
lineDot4(lineBasisBezier1, px),
lineDot4(lineBasisBezier1, py),
lineDot4(lineBasisBezier2, px),
lineDot4(lineBasisBezier2, py),
lineDot4(lineBasisBezier3, px),
lineDot4(lineBasisBezier3, py)
)
}
lineTo(points.last().x, points.last().y)
}
}

fun Context2d.drawImage(snapshot: Snapshot, p: Vec<*>) = drawImage(snapshot, p.x, p.y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,36 +230,11 @@ open class PathRenderer : Renderer {

class CurveRenderer : PathRenderer() {
override fun drawPath(points: List<WorldPoint>, ctx: Context2d) {
fun lineDot4(a: List<Double>, b: List<Double>): Double {
// returns the dot product of the given four-element vectors
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
if (points.size < 3) {
// linear
super.drawPath(points, ctx)
} else {
ctx.bSplineInterpolation(points)
}
// Matrix to transform basis (b-spline) control points to bezier control points.
val lineBasisBezier1 = listOf(0.0, 2.0 / 3.0, 1.0 / 3.0, 0.0)
val lineBasisBezier2 = listOf(0.0, 1.0 / 3.0, 2.0 / 3.0, 0.0)
val lineBasisBezier3 = listOf(0.0, 1.0 / 6.0, 2.0 / 3.0, 1.0 / 6.0)

val px = arrayListOf(points[0].x, points[0].x, points[0].x, points[1].x)
val py = arrayListOf(points[0].y, points[0].y, points[0].y, points[1].y)

ctx.moveTo(points[0].x, points[0].y)
ctx.lineTo(
lineDot4(lineBasisBezier3, px),
lineDot4(lineBasisBezier3, py)
)
for (i in 2..points.size) {
val curPoint = if (i < points.size) points[i] else points.last()
px.removeFirst(); px.add(curPoint.x)
py.removeFirst(); py.add(curPoint.y)
ctx.bezierCurveTo(
lineDot4(lineBasisBezier1, px),
lineDot4(lineBasisBezier1, py),
lineDot4(lineBasisBezier2, px),
lineDot4(lineBasisBezier2, py),
lineDot4(lineBasisBezier3, px),
lineDot4(lineBasisBezier3, py)
)
}
ctx.lineTo(points.last().x, points.last().y)
}
}