Skip to content

Instantly share code, notes, and snippets.

@krk

krk/bezier.cs Secret

Created June 27, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krk/832ecef92193dbdcb27047ddd186d2f1 to your computer and use it in GitHub Desktop.
Save krk/832ecef92193dbdcb27047ddd186d2f1 to your computer and use it in GitHub Desktop.
private PointCollection GetBezierPoints(PointCollection pts, double tension) {
PointCollection ret = new PointCollection();
for (int i = 0; i < pts.Count; i++) {
// for first point append as is.
if (i == 0) {
ret.Add(pts[0]);
continue;
}
// for each point except first and last get B1, B2. next point.
// Last point do not have a next point.
ret.Add(GetB1(pts, i - 1, tension));
ret.Add(GetB2(pts, i - 1, tension));
ret.Add(pts[i]);
}
return ret;
}
// To use the PointCollection returned from GetBezierPoints method in Silverlight, we need to build a Path with BezierSegments in it.
private PathFigure GetBezierSegments(PointCollection pts, double tension) {
PathFigure ret = new PathFigure();
ret.Segments.Clear();
ret.IsClosed = false;
var bzPoints = GetBezierPoints(_projectedPoints, tension);
// First point is the starting point.
ret.StartPoint = bzPoints[0];
for (int i = 1; i < bzPoints.Count; i += 3) {
ret.Segments.Add(new BezierSegment() {
Point1 = bzPoints[i], // B1 control point.
Point2 = bzPoints[i + 1], // B2 control point.
Point3 = bzPoints[i + 2] // P2 start / end point.
});
}
return ret;
}
// And use this PathFigure in the MapBezier as:
// Create a new PathGeometry.
var pGeo = new PathGeometry();
// Add the Bezier PathFigure to the PathGeometry.
pGeo.Figures.Add(GetBezierSegments(_projectedPoints, Tension));
// Set data of the Path to the created PathGeometry.
((Path) EncapsulatedShape).Data = pGeo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment