如何使用System.Windows.Shapes.Path绘制圆的一部分? [英] How to draw a section of a circle using System.Windows.Shapes.Path?
问题描述
我有一个Silverlight应用程序,通过实例化System.Windows.Shapes.Line实例,然后将其添加到MainCanvas.Children,在图片上绘制线条.
I have a Silverlight application where I draw lines on a picture by instantiating an instance of System.Windows.Shapes.Line and then adding it to MainCanvas.Children.
我想类似地添加一些圆弧段.如果我理解正确,我将要使用Path类.
I would like to similarly add some circle segments. If I understand correctly, I will want to use the Path class.
我有以下要求.我的圆心有(x,y)坐标.我的半径为r.我需要绘制一条圆弧,该圆弧是具有中心(x,y)和半径r的圆的子集.我有两个角度a1和a2,需要在它们之间绘制弧.
I have the following requirement. I have (x,y) coordinates for the center of the circle. I have the radius r. I need to draw an arc that is a subset of the circle with center (x,y) and radius r. I have two angles a1 and a2 between which I need to draw the arc.
我该怎么做?
推荐答案
您需要构建一个字符串,该字符串使用WPF支持的弧线来标识路径.该字符串遵循此页面上的语法 .
You'll need to build a string that identifies the path using an arc, which is supported by WPF. The string follows the syntax on this page.
但是,必须首先将参数转换为语法中给出的弧格式,因为使用的值有些不同:
However, the parameters must first be converted to the arc format given in the syntax, since the values used are a little different:
首先,如果尚未将角度 a1
和 a2
转换为弧度,则将其转换为弧度.
First, convert the angles a1
and a2
to radians, if they aren't already.
a1=a1*Math.PI/180;
a2=a2*Math.PI/180;
然后,计算圆弧的起点和终点:
Then, calculate the start and end point of the arc:
double startX = centerX+Math.Cos(a1)*r;
double startY = centerY+Math.Sin(a1)*r;
double endX = centerX+Math.Cos(a2)*r;
double endY = centerY+Math.Sin(a2)*r;
然后,计算角度之间的差是PI还是更大,然后计算a1是否小于a2.
Then, calculate whether the difference between the angles is PI or greater, then whether a1 is less than a2.
bool largeArc = Math.Abs(a2-a1)>=Math.PI;
bool sweep = (a1<a2);
最后,构造路径.
string path=String.Format(System.Globalization.CultureInfo.InvariantCulture,
"M {0},{1} A {2},{3} 0 {4} {5} {6},{7}",startX,startY,r,r,
largeArc ? 1 : 0, sweep ? 1 : 0,endX,endY);
这是用于定义路径的路径字符串,而不是Path对象.
That's the path string, not the Path object, that you'll use in defining the path.
这篇关于如何使用System.Windows.Shapes.Path绘制圆的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!