Sample code for drawing a spiral in WPF? [closed]
I searched on Google I found nothing. Do you know some ?
Found the spiral equation ( you have to decide wich one, different kind of spiral exists ) ie: http://mathworld.wolfram.com/ArchimedesSpiral.html that one is presented in polar coordinates. Given so you need to approximate it, for example by lines. This is the way I will go. So I can post some code just as an example, I wrote in a scratch new wpf application,and I removed the default grid from the xaml ( necessary if you want to test soon the code ) :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Path p = new Path();
p.Data = CreateSpiralGeometry(1000, new Point() { X = 200, Y = 180 },Math.PI*10, 100);
p.Stroke = Brushes.Black;
AddChild(p);
}
private PathGeometry CreateSpiralGeometry(int nOfSteps, Point startPoint, double tetha, double alpha)
{
PathFigure spiral = new PathFigure();
spiral.StartPoint = startPoint;
for(int i=0;i<nOfSteps;++i)
{
var t = (tetha/nOfSteps)*i;
var a = (alpha/nOfSteps)*i;
Point to = new Point(){X=startPoint.X+a*Math.Cos(t), Y=startPoint.Y+a*Math.Sin(t)};
spiral.Segments.Add(new LineSegment(to,true));
}
return new PathGeometry(new PathFigure[]{ spiral});
}
}
精彩评论