@@ -25,10 +25,6 @@ public partial class RingShape : Path
2525 private double _equalRadius ; // Calculated where RadiusWidth and RadiusHeight are equal
2626 private double _normalizedMinAngle ; // Normalized MinAngle between -180 and 540
2727 private double _normalizedMaxAngle ; // Normalized MaxAngle between 0 and 360
28- private double _validStartAngle ; // The validated StartAngle
29- private double _validEndAngle ; // The validated EndAngle
30- private double _radiusWidth ; // The radius Width
31- private double _radiusHeight ; // The radius Height
3228
3329 // Constructor
3430
@@ -64,53 +60,38 @@ private void UpdatePath()
6460 {
6561 if ( _isUpdating ||
6662 ActualWidth <= 0 || ActualHeight <= 0 ||
67- _radiusWidth <= 0 || _radiusHeight <= 0 )
63+ RadiusWidth <= 0 || RadiusHeight <= 0 )
6864 return ;
6965
70- UpdateSizeAndStroke ( this ) ;
71-
72- var startAngle = _validStartAngle ;
73- var endAngle = _validEndAngle ;
66+ UpdateSizeAndStroke ( ) ;
7467
7568 // If the ring is closed and complete
76- if ( endAngle >= startAngle + 360 )
69+ if ( EndAngle >= StartAngle + 360 )
7770 {
78- Data = DrawEllipse ( IsCircle , Center , _equalRadius , _radiusWidth , _radiusHeight ) ;
71+ Data = DrawEllipse ( ) ;
7972 }
8073 else
8174 {
82- this . InvalidateArrange ( ) ;
83- this . Data = DrawArc ( this , SweepDirection , IsCircle , Center , startAngle , endAngle , _equalRadius , _radiusWidth , _radiusHeight ) ;
75+ InvalidateArrange ( ) ;
76+ Data = DrawArc ( ) ;
8477 }
8578 }
8679
8780 #endregion
8881
8982 #region Drawing Updates
9083
91- /// <summary>
92- /// Updates sizes, center point and radii
93- /// </summary>
94- /// <param name="d">The DependencyObject calling the function</param>
95- public void UpdateSizeAndStroke ( DependencyObject d )
84+ public void UpdateSizeAndStroke ( )
9685 {
97- RingShape ringShape = ( RingShape ) d ;
98-
99- AdjustRadiusWidth ( ) ;
100- AdjustRadiusHeight ( ) ;
101-
102- _equalSize = CalculateEqualSize ( new ( Width , Height ) ) ;
103- _equalRadius = CalculateEqualRadius ( ) ;
104-
105- Center = new ( ringShape . Width / 2 , ringShape . Height / 2 ) ;
106-
107- CalculateAndSetNormalizedAngles ( ) ;
108-
109- _validStartAngle = ValidateAngle ( StartAngle ) ;
110- _validEndAngle = ValidateAngle ( EndAngle ) ;
86+ ValidateRadius ( ) ;
87+ CalculateEqualSize ( new ( Width , Height ) ) ;
88+ CalculateEqualRadius ( ) ;
89+ Center = new ( Width / 2.0 , Height / 2.0 ) ;
90+ CalculateNormalizedAngles ( ) ;
91+ ValidateAngles ( ) ;
11192 }
11293
113- private static EllipseGeometry DrawEllipse ( bool IsCircle , Point Center , double EqualRadius , double RadiusWidth , double RadiusHeight )
94+ private EllipseGeometry DrawEllipse ( )
11495 {
11596 EllipseGeometry eg ;
11697
@@ -119,62 +100,55 @@ private static EllipseGeometry DrawEllipse(bool IsCircle, Point Center, double E
119100 eg = new ( )
120101 {
121102 Center = Center ,
122- RadiusX = EqualRadius ,
123- RadiusY = EqualRadius ,
103+ RadiusX = _equalRadius ,
104+ RadiusY = _equalRadius ,
124105 } ;
125106 }
126107 else
127108 {
128109 eg = new ( )
129110 {
130111 Center = Center ,
131- RadiusX = RadiusWidth ,
132- RadiusY = RadiusHeight ,
112+ RadiusX = _equalRadius ,
113+ RadiusY = _equalRadius ,
133114 } ;
134115 }
135116
136117 return eg ;
137118 }
138119
139- private static PathGeometry DrawArc ( RingShape RingShape , SweepDirection SweepDirection , bool IsCircle , Point Center , double StartAngle , double EndAngle , double EqualRadius , double RadiusWidth , double RadiusHeight )
120+ private PathGeometry DrawArc ( )
140121 {
122+ var newCenter = Center ;
123+ var arcSegment = new ArcSegment ( ) ;
141124 var pathGeometry = new PathGeometry ( ) ;
142125 var pathFigure = new PathFigure ( )
143126 {
144- pathFigure . IsClosed = false ,
145- pathFigure . IsFilled = false ,
127+ IsClosed = false ,
128+ IsFilled = false ,
146129 } ;
147130
148- var newCenter = Center ;
149- var arcSegment = new ArcSegment ( ) ;
150-
151131 if ( IsCircle )
152132 {
153- var radius = EqualRadius ;
154-
155- RingShape . ActualRadiusWidth = radius ;
156- RingShape . ActualRadiusHeight = radius ;
133+ ActualRadiusWidth = _equalRadius ;
134+ ActualRadiusHeight = _equalRadius ;
157135
158136 // Start Point
159- pathFigure . StartPoint = ArcStartPoint ( SweepDirection , newCenter , StartAngle , radius , radius ) ;
137+ pathFigure . StartPoint = ArcStartPoint ( ) ;
160138
161139 // Arc Segment and End Point
162- arcSegment = CreateArcSegment ( SweepDirection , newCenter , StartAngle , EndAngle , radius , radius ) ;
140+ arcSegment = CreateArcSegment ( ) ;
163141 }
164142 else
165143 {
166- var radiusWidth = RadiusWidth ;
167- var radiusHeight = RadiusHeight ;
168-
169- RingShape . ActualRadiusWidth = radiusWidth ;
170- RingShape . ActualRadiusHeight = radiusHeight ;
144+ ActualRadiusWidth = RadiusWidth ;
145+ ActualRadiusHeight = RadiusHeight ;
171146
172147 // Start Point
173- pathFigure . StartPoint = ArcStartPoint ( SweepDirection , newCenter , StartAngle , radiusWidth , radiusHeight ) ;
174-
148+ pathFigure . StartPoint = ArcStartPoint ( ) ;
175149
176150 // Arc Segment and End Point
177- arcSegment = CreateArcSegment ( SweepDirection , newCenter , StartAngle , EndAngle , radiusWidth , radiusHeight ) ;
151+ arcSegment = CreateArcSegment ( ) ;
178152 }
179153
180154 pathFigure . Segments . Add ( arcSegment ) ;
@@ -183,18 +157,18 @@ private static PathGeometry DrawArc(RingShape RingShape, SweepDirection SweepDir
183157 return pathGeometry ;
184158 }
185159
186- private static Point ArcStartPoint ( SweepDirection SweepDirection , Point Center , double StartAngle , double RadiusWidth , double RadiusHeight )
160+ private Point ArcStartPoint ( )
187161 {
188162 return SweepDirection is SweepDirection . Counterclockwise
189163 ? new (
190164 Center . X - Math . Sin ( StartAngle * DegreesToRadians ) * RadiusWidth ,
191- Center . Y - Math . Cos ( StartAngle * DegreesToRadians ) * RadiusHeight ) ;
165+ Center . Y - Math . Cos ( StartAngle * DegreesToRadians ) * RadiusHeight )
192166 : new (
193- Center . X + Math . Sin ( StartAngle * DegreesToRadians ) * RadiusWidth ,
167+ Center . X + Math . Sin ( StartAngle * DegreesToRadians ) * RadiusWidth ,
194168 Center . Y - Math . Cos ( StartAngle * DegreesToRadians ) * RadiusHeight ) ;
195169 }
196170
197- private static ArcSegment CreateArcSegment ( SweepDirection SweepDirection , Point Center , double StartAngle , double EndAngle , double RadiusWidth , double RadiusHeight )
171+ private ArcSegment CreateArcSegment ( )
198172 {
199173 var finalArcSegment = new ArcSegment ( ) ;
200174
@@ -237,7 +211,7 @@ private static ArcSegment CreateArcSegment(SweepDirection SweepDirection , Point
237211 }
238212 }
239213
240- finalArcSegment . Size = new ( RadiusWidth , RadiusHeight ) ;
214+ finalArcSegment . Size = new ( RadiusWidth , RadiusHeight ) ;
241215
242216 return finalArcSegment ;
243217 }
@@ -246,33 +220,25 @@ private static ArcSegment CreateArcSegment(SweepDirection SweepDirection , Point
246220
247221 #region Value Calculations
248222
249- private static Size CalculateEqualSize ( Size size )
223+ private Size CalculateEqualSize ( Size size )
250224 {
251225 var smaller = Math . Min ( size . Width , size . Height ) ;
252- if ( smaller > StrokeThickness * 2 )
253- return new ( smaller , smaller ) ;
254- else
255- return new ( StrokeThickness * 2 , StrokeThickness * 2 ) ;
226+ _equalSize = smaller > StrokeThickness * 2
227+ ? new ( smaller , smaller ) ;
228+ : new ( StrokeThickness * 2 , StrokeThickness * 2 ) ;
256229 }
257230
258- private static double CalculateEqualRadius ( )
231+ private double CalculateEqualRadius ( )
259232 {
260233 var smaller = Math . Min ( RadiusWidth , RadiusHeight ) ;
261- if ( smaller <= StrokeThickness )
262- return StrokeThickness ;
263- else if ( smaller >= ( ( _equalSize . Width / 2 ) - ( StrokeThickness / 2 ) ) )
264- return ( _equalSize . Width / 2.0 ) - ( StrokeThickness / 2.0 ) ;
265- else
266- return smaller ;
234+ _equalRadius = smaller <= StrokeThickness
235+ ? StrokeThickness
236+ : smaller >= ( ( _equalSize . Width / 2.0 ) - ( StrokeThickness / 2.0 ) )
237+ ? ( _equalSize . Width / 2.0 ) - ( StrokeThickness / 2.0 )
238+ : smaller ;
267239 }
268240
269- private static Point CalculateCenter ( double Width , double Height )
270- {
271- var calculatedCenter = new Point ( ( Width / 2.0 ) , ( Height / 2.0 ) ) ;
272- return calculatedCenter ;
273- }
274-
275- private static void CalculateAndSetNormalizedAngles ( )
241+ private void CalculateNormalizedAngles ( )
276242 {
277243 var result = CalculateModulus ( MinAngle , 360 ) ;
278244 if ( result >= 180 )
@@ -290,50 +256,49 @@ private static void CalculateAndSetNormalizedAngles()
290256 _normalizedMaxAngle = result ;
291257 }
292258
293- private static double CalculateModulus ( double number , double divider )
259+ private double CalculateModulus ( double number , double divider )
294260 {
295261 // Calculate the modulus
296262 var result = number % divider ;
297263
298264 // Ensure the result is positive or zero
299- result = result < 0 ? result + divider : result ;
265+ result = result < 0.0 ? result + divider : result ;
300266
301267 return result ;
302268 }
303269
304- private double ValidateAngle ( double angle )
270+ private double ValidateAngles ( )
305271 {
306- return angle >= _normalizedMaxAngle
272+ MinAngle = MinAngle >= _normalizedMaxAngle
307273 ? _normalizedMaxAngle
308- : angle <= _normalizedMinAngle
274+ : MinAngle <= _normalizedMinAngle
309275 ? _normalizedMinAngle
310- : angle ;
311- }
276+ : MinAngle ;
312277
313- private void AdjustRadiusWidth ( )
314- {
315- var maxValue = ( Width / 2 ) - ( StrokeThickness / 2 ) ;
316- var threshold = StrokeThickness ;
317-
318- if ( RadiusWidth >= maxValue )
319- RadiusWidth = maxValue ;
320- else if ( RadiusWidth <= maxValue && RadiusWidth >= threshold )
321- RadiusWidth = RadiusWidth ;
322- else
323- RadiusWidth = threshold ;
278+ MaxAngle = MaxAngle >= _normalizedMaxAngle
279+ ? _normalizedMaxAngle
280+ : MaxAngle <= _normalizedMinAngle
281+ ? _normalizedMinAngle
282+ : MaxAngle ;
324283 }
325284
326- private void AdjustRadiusHeight ( )
285+ private void ValidateRadius ( )
327286 {
328- var maxValue = ( Height / 2 ) - ( StrokeThickness / 2 ) ;
287+ var maxWidthValue = ( Width / 2.0 ) - ( StrokeThickness / 2.0 ) ;
288+ var maxHeightValue = ( Height / 2.0 ) - ( StrokeThickness / 2.0 ) ;
329289 var threshold = StrokeThickness ;
330290
331- if ( RadiusHeight >= maxValue )
332- RadiusHeight = maxValue ;
333- else if ( RadiusHeight <= maxValue && RadiusHeight >= threshold )
334- RadiusHeight = RadiusHeight ;
335- else
336- RadiusHeight = threshold ;
291+ RadiusWidth = RadiusWidth >= maxWidthValue
292+ ? maxWidthValue
293+ : RadiusWidth <= maxWidthValue && RadiusWidth >= threshold
294+ ? RadiusWidth
295+ : threshold ;
296+
297+ RadiusHeight = RadiusHeight >= maxHeightValue
298+ ? maxHeightValue
299+ : RadiusHeight <= maxHeightValue && RadiusHeight >= threshold
300+ ? RadiusHeight
301+ : threshold ;
337302 }
338303
339304 #endregion
0 commit comments