Pages

Monday, 10 May 2010

Drawing WPF Bezier Curves programmatically

I needed to draw a curved line between the mid-points of two shapes, centered on X1,Y1 and X2, Y2.
This method also returns a PathGeometry which is then used to determine the Intersection between the curve and the shapes.
private static PathGeometry DrawLine(Canvas canvas, double X1, double Y1, double X2, double Y2, Color color)
{
QuadraticBezierSegment qbs = new QuadraticBezierSegment(new Point(X2, Y1), new Point(X2, Y2), true);

PathSegmentCollection pscollection = new PathSegmentCollection();
pscollection.Add(qbs);

PathFigure pf = new PathFigure();
pf.Segments = pscollection;
pf.StartPoint = new Point(X1, Y1);

PathFigureCollection pfcollection = new PathFigureCollection();
pfcollection.Add(pf);

PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures = pfcollection;

Path path = new Path();
path.Data = pathGeometry;
path.Stroke = new SolidColorBrush(color);
path.StrokeThickness = 2;
Canvas.SetZIndex(path, (int)Layer.Line);
canvas.Children.Add(path);

return pathGeometry;
}

No comments: