1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
7
using System.Windows.Shapes;
8
using System.Windows.Media;
11
namespace ICSharpCode.Profiler.Controls
16
class PiePiece : Shape
18
#region dependency properties
20
public static readonly DependencyProperty RadiusProperty =
21
DependencyProperty.Register("RadiusProperty", typeof(double), typeof(PiePiece),
22
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
25
/// The radius of this pie piece
29
get { return (double)GetValue(RadiusProperty); }
30
set { SetValue(RadiusProperty, value); }
33
public static readonly DependencyProperty InnerRadiusProperty =
34
DependencyProperty.Register("InnerRadiusProperty", typeof(double), typeof(PiePiece),
35
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
38
/// The inner radius of this pie piece
40
public double InnerRadius
42
get { return (double)GetValue(InnerRadiusProperty); }
43
set { SetValue(InnerRadiusProperty, value); }
46
public static readonly DependencyProperty WedgeAngleProperty =
47
DependencyProperty.Register("WedgeAngleProperty", typeof(double), typeof(PiePiece),
48
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
51
/// The wedge angle of this pie piece in degrees
53
public double WedgeAngle
55
get { return (double)GetValue(WedgeAngleProperty); }
56
set { SetValue(WedgeAngleProperty, value); }
59
public static readonly DependencyProperty RotationAngleProperty =
60
DependencyProperty.Register("RotationAngleProperty", typeof(double), typeof(PiePiece),
61
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
64
/// The rotation, in degrees, from the Y axis vector of this pie piece.
66
public double RotationAngle
68
get { return (double)GetValue(RotationAngleProperty); }
69
set { SetValue(RotationAngleProperty, value); }
73
protected override Size MeasureOverride(Size constraint)
75
double size = 2 * Radius;
76
return new Size(size, size);
79
protected override Geometry DefiningGeometry
83
// Create a StreamGeometry for describing the shape
84
StreamGeometry geometry = new StreamGeometry();
85
geometry.FillRule = FillRule.EvenOdd;
87
using (StreamGeometryContext context = geometry.Open())
89
DrawGeometry(context);
92
// Freeze the geometry for performance benefits
100
/// Draws the pie piece
102
private void DrawGeometry(StreamGeometryContext context)
104
double centreX = Radius;
105
double centreY = Radius;
107
Point startPoint = new Point(centreX, centreY);
109
Point innerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, InnerRadius);
110
innerArcStartPoint.Offset(centreX, centreY);
112
Point innerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, InnerRadius);
113
innerArcEndPoint.Offset(centreX, centreY);
115
Point outerArcStartPoint = Utils.ComputeCartesianCoordinate(RotationAngle, Radius);
116
outerArcStartPoint.Offset(centreX, centreY);
118
Point outerArcEndPoint = Utils.ComputeCartesianCoordinate(RotationAngle + WedgeAngle, Radius);
119
outerArcEndPoint.Offset(centreX, centreY);
121
bool largeArc = WedgeAngle>180.0;
123
Size outerArcSize = new Size(Radius, Radius);
124
Size innerArcSize = new Size(InnerRadius, InnerRadius);
126
context.BeginFigure(innerArcStartPoint, true, true);
127
context.LineTo(outerArcStartPoint, true, true);
128
context.ArcTo(outerArcEndPoint, outerArcSize, 0, largeArc, SweepDirection.Clockwise, true, true);
129
context.LineTo(innerArcEndPoint, true, true);
130
context.ArcTo(innerArcStartPoint, innerArcSize, 0, largeArc, SweepDirection.Counterclockwise, true, true);
134
public static class Utils
138
/// Converts a coordinate from the polar coordinate system to the cartesian coordinate system.
140
/// <param name="angle"></param>
141
/// <param name="radius"></param>
142
/// <returns></returns>
143
public static Point ComputeCartesianCoordinate(double angle, double radius)
145
// convert to radians
146
double angleRad = (Math.PI / 180.0) * (angle - 90);
148
double x = radius * Math.Cos(angleRad);
149
double y = radius * Math.Sin(angleRad);
151
return new Point(x, y);