以Y軸正方向爲0度,順時針遞增,我們來做一個40度角的扇形,對稱軸是Y軸。
前臺:利用Blend
1、按住shift畫出一個圓形,去掉生成的Ellipse對象的Margin、Stroke屬性,添加 Width, Height屬性值(目的爲了能比較準確的切割)。
<Ellipse Fill="#FFF4F4F5" Width="200" Height="200"/>
2、在畫一個矩形,去掉生成的Rectangle對象的VerticalAlignment屬性,修改Height屬性爲圓形半徑大小,修改Margin屬性爲距離頂部圓形半徑的距離。
<Rectangle Fill="#FFF4F4F5" Height="100" Margin="0,100,0,0"/>
3、先選擇圓形再選擇矩形後右鍵->合併->相減,就可以得到一個半圓。
4、在做一個相同的矩形,在其轉換屬性裏設置中心點中上(0.5,0),在設置其旋轉70度(Y軸左右個20度),重複3的動作,得到一個扇形。
5、重複4的動作,只是旋轉角度寫-70或是110度,得到了最終的40度扇形。
PS:最後獲得的是一個Path對象,
後臺:
方法1:這種方法就是通過XamlReader讀取上面Blend畫出的Path對象的XAML標識獲得Path(扇形)。
//引用
xaml += " xmlns=\"http://schemas.microsoft.com/client/2007\"";
//創建屬性
xaml += " Fill=\"#AE1E5E19\" Height=\"20\" RenderTransformOrigin=\"0.5,1\" Stretch=\"Uniform\"";
xaml += string.Format(" Data=\"{0}\" />", "M34.209953,0 C44.565323,3.5734599E-06 54.55307,1.5740019 63.946991,4.4958062 L68.419983,6.0092931 L34.210087,100 L0,6.0092659 L4.4728961,4.4958062 C13.86683,1.5739993 23.854574,3.1432953E-07 34.209953,0 z");
//創建路徑對象
Path path;
//<Path x:Name="ArcPath" Data=/>
path = (Path)XamlReader.Load(xaml);
方法2:通過C代碼生成,要根據角度計算偏移值。
{
/// <summary>
/// 扇形的半徑大小,更改其值會影響扇形大小。
/// </summary>
private const int Radius = 10;
public static Path GetBaseStation(double rotationAngle)
{
var path = new Path
{
Fill = new SolidColorBrush(Colors.Green),
//以扇形的兩直線連接頂點爲幾何中心進行定位和旋轉。
RenderTransformOrigin = new Point(0.5, 1),
Stretch = Stretch.None,
UseLayoutRounding = false
};
//以幾何中心爲準直線頂點的偏移值。
var offsetx = Radius * Math.Cos(20);
var offsety = Radius * Math.Sin(20);
//繪製起始直線
var startLine = new LineSegment()
{
Point = new Point(0 - offsetx, 0 - offsety)
};
//繪製弧線,一條三次方貝塞爾曲線,Point1起點,Point2爲峯值點,Point3爲終點。
var arcLine = new BezierSegment()
{
Point1 = new Point(0 - offsetx, 0 - offsety),
Point2 = new Point(0, 0 - Radius),
Point3 = new Point(0 + offsetx, 0 - offsety)
};
//繪製結束直線
var endLine = new LineSegment()
{
Point = new Point(0, 0)
};
//把三條線段集合在一起。
var segments = new PathSegmentCollection { startLine, arcLine, endLine };
//爲直線的Data賦值,由三段封閉的線段繪製成一個扇形
path.Data = new PathGeometry()
{
Figures = new PathFigureCollection() { new PathFigure()
{
StartPoint = new Point(0, 0),
Segments = segments
}}
};
//設置扇形對稱軸偏轉角度。
path.RenderTransform = new CompositeTransform() { Rotation = rotationAngle };
return path;
}
}
通過類Path path= BaseStation.GetBaseStation(90);就可以獲得對稱軸爲90度,角度爲40的扇形。
本文來自wushang923的博客,原文地址:http://blog.csdn.net/wushang923/article/details/7534691