Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a smooth curved line in WPF?

Tags:

c#

.net

canvas

wpf

I have three known positions, and currently I am driving two lines like so:

Line line = new Line
{
    StrokeThickness = 3,
    Stroke = lineColor,
    X1 = MyX,
    Y1 = MyY,
    X2 = MyX,
    Y2 = MiddleY
};

Graph.Children.Add(line);

line = new Line
{
    StrokeThickness = 3,
    Stroke = lineColor,
    X1 = MyX,
    Y1 = MiddleY,
    X2 = TargetX,
    Y2 = TargetY
};

Graph.Children.Add(line);

Here's the result:

enter image description here

So, as you can see, this is almost what I want except that I want it to be more smoothed out, just a little bit.

Now I'm looking for any way I could set three points, set some smooth/curvy level to the middle point and then draw a line with a solid color on it. Much like how I can do this in Photoshop:

enter image description here

Or at least get a similar kind of smoothness.

like image 203
Tower Avatar asked May 07 '12 18:05

Tower


2 Answers

You want to use a PathFigure, specifically with a set of BezierSegments.

like image 118
plinth Avatar answered Nov 08 '22 16:11

plinth


I think you are looking for splines

http://msdn.microsoft.com/en-us/library/554h284b.aspx

Gabe is correct that is from Forms

Under WPF you could try a PolyBezierSegment but it require 4 points. Possible you could put in three points and 1 more to shape it.

<Canvas>
    <Path Stroke="Black" StrokeThickness="10">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>    
                        <PathFigure StartPoint="100,80">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <PolyBezierSegment Points="90,200 140,200 160,200 180,200 430,190 430,280" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
</Canvas>

This results in the following curve

enter image description here

like image 44
paparazzo Avatar answered Nov 08 '22 15:11

paparazzo