Created
December 16, 2019 10:51
-
-
Save blaugold/e4528c05adb7d47b4ed9fd95a8cdace8 to your computer and use it in GitHub Desktop.
Util for smoothing of line joins, with quadratic beziers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
class QuadraticBezier { | |
final Offset p0; | |
final Offset p1; | |
final Offset p2; | |
/// Returns a list of [QuadraticBezier]s which represent smooth line joins between knots. | |
/// | |
/// To reduce the smoothing at the joins reduce [maxJoinSegmentLength]. | |
static List<QuadraticBezier> lineJoins({ | |
List<Offset> line, | |
maxJoinSegmentLength = 10.0, | |
}) { | |
assert(line != null); | |
assert(maxJoinSegmentLength != null); | |
if (line.length < 3) return []; | |
final joins = List<QuadraticBezier>(); | |
Offset joinSegment(Offset from, Offset to) { | |
// Divide by half to ensure lhs and rhs segments do not overlap. | |
var segment = (to - from) / 2; | |
var segmentLength = segment.distance; | |
if (segmentLength > maxJoinSegmentLength) { | |
segment /= (segmentLength / maxJoinSegmentLength); | |
} | |
return segment; | |
} | |
for (var i = 1; i < line.length - 1; i++) { | |
final prev = line[i - 1]; | |
final cur = line[i]; | |
final next = line[i + 1]; | |
var lhsSegment = joinSegment(prev, cur); | |
var rhsSegment = joinSegment(cur, next); | |
final p0 = cur - lhsSegment; | |
final p1 = cur; | |
final p2 = cur + rhsSegment; | |
joins.add(QuadraticBezier(p0: p0, p1: p1, p2: p2)); | |
} | |
return joins; | |
} | |
QuadraticBezier({ | |
this.p0, | |
this.p1, | |
this.p2, | |
}) : assert(p0 != null), | |
assert(p1 != null), | |
assert(p2 != null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment