Silverlight – InkPresenter 上程式畫圓

一直以來都是使用 component 來畫圓,InkPresenter 只提供由點來組成各種形狀,那需自行畫圓時,如何取得圓弧上各點的X,Y座標呢?X: cos(角度) * r, Y: sin(角度) * r,利用此公式就可以取得圓弧上各點的X,Y座標.

XAML:
<InkPresenter x:Name="ink" Width="600" Height="600" MouseLeftButtonUp="ink_MouseLeftButtonUp">
</InkPresenter>
C#:
System.Windows.Ink.Stroke newStroke = new System.Windows.Ink.Stroke();
newStroke.DrawingAttributes.Color = Color.FromArgb(255, 0, 0, 0);
newStroke.DrawingAttributes.Width = 1;
newStroke.DrawingAttributes.Height = 1;
for (int i = 0; i <= 360; i+=5)
{
newStroke.StylusPoints.Add(
new StylusPoint()
{
X = 300 - 300 * Math.Cos(Math.PI * i / 180),
Y = 300 - 300 * Math.Sin(Math.PI * i / 180)
}
);
}
ink.Strokes.Add(newStroke);

 


其結果如下所示:


image

留言

這個網誌中的熱門文章

WPF - 深入 Style

C# – M$ Chart Control 自定 ToolTip 的顯示

Vue.js - 基礎介紹教學