Draw a sine wave in WPF

It seems pretty dumb, but I'm trying my best to do it,

How to draw a sine wave in WPF

thanks

+4
source share
3 answers

Draw lines between the points that you calculate using the Math.Sin function. You will need to decide how many points per cycle to use, a trade-off between speed and accuracy. Presumably, you will also need to scale the amplitude according to the area on the screen, as the Sin function will return a value between +1 and -1.

+4
source

How do you do your "Drawing". WPF does not have OnPaint events like Winforms, which may seem a bit complicated. A way to do this in WinForms would be to use the Graphics.DrawBezier method

 e.Graphics.DrawBezier(new Pen(new SolidBrush(Color.Red)), new Point(0, 100), new Point(50, 0), new Point(50, 200), new Point(100, 100)); 

Maybe this is useful, but I'm not even sure how to draw directly on a WPF Canvas.

A quick look, since MSDN shows that it has a BezierSegment control that might be useful to you.

+3
source

If you want curves between your points, you can use PolyBezier to draw your sine wave using the PointCollection calculated using the Math.Sin method. Alternatively, you could create many BezierSegments that flow from each other. Finally, just add your PolyBezier or BezierSegments to your form using drawingarea.Children.Add (curve), where the drawing area is the grid or canvas you are drawing onto.

+2
source

Source: https://habr.com/ru/post/1285649/


All Articles