-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPieSlice.cs
1218 lines (1155 loc) · 51.7 KB
/
PieSlice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace System.Drawing.PieChart {
/// <summary>
/// Object representing 3D pie.
/// </summary>
public class PieSlice : IDisposable, ICloneable {
/// <summary>
/// Initializes an empty instance of <c>PieSlice</c>.
/// </summary>
protected PieSlice() : base() {
}
/// <summary>
/// Initializes a new instance of flat <c>PieSlice</c> class with given
/// bounds and visual style.
/// </summary>
/// <param name="xBoundingRect">
/// x-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="yBoundingRect">
/// y-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="widthBoundingRect">
/// Width of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="heightBoundingRect">
/// Height of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="startAngle">
/// Starting angle (in degrees) of the pie slice.
/// </param>
/// <param name="sweepAngle">
/// Sweep angle (in degrees) of the pie slice.
/// </param>
/// <param name="surfaceColor">
/// Color used to paint the pie slice.
/// </param>
public PieSlice(float xBoundingRect, float yBoundingRect, float widthBoundingRect, float heightBoundingRect, float startAngle, float sweepAngle, Color surfaceColor)
: this (xBoundingRect, yBoundingRect, widthBoundingRect, heightBoundingRect, 0F, startAngle, sweepAngle, surfaceColor, ShadowStyle.NoShadow, EdgeColorType.NoEdge) {
}
/// <summary>
/// Initializes a new instance of <c>PieSlice</c> class with given
/// bounds and visual style.
/// </summary>
/// <param name="xBoundingRect">
/// x-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="yBoundingRect">
/// y-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="widthBoundingRect">
/// Width of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="heightBoundingRect">
/// Height of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="sliceHeight">
/// Height of the pie slice.
/// </param>
/// <param name="startAngle">
/// Starting angle (in degrees) of the pie slice.
/// </param>
/// <param name="sweepAngle">
/// Sweep angle (in degrees) of the pie slice.
/// </param>
/// <param name="surfaceColor">
/// Color used to paint the pie slice.
/// </param>
/// <param name="shadowStyle">
/// Shadow style used for slice rendering.
/// </param>
/// <param name="edgeColorType">
/// Edge color style used for slice rendering.
/// </param>
public PieSlice(float xBoundingRect, float yBoundingRect, float widthBoundingRect, float heightBoundingRect, float sliceHeight, float startAngle, float sweepAngle, Color surfaceColor, ShadowStyle shadowStyle, EdgeColorType edgeColorType) : this() {
// set some persistent values
m_actualStartAngle = startAngle;
m_actualSweepAngle = sweepAngle;
m_surfaceColor = surfaceColor;
m_shadowStyle = shadowStyle;
m_edgeColorType = edgeColorType;
// create pens for rendering
Color edgeLineColor = EdgeColor.GetRenderingColor(edgeColorType, surfaceColor);
m_pen = new Pen(edgeLineColor);
m_pen.LineJoin = LineJoin.Round;
InitializePieSlice(xBoundingRect, yBoundingRect, widthBoundingRect, heightBoundingRect, sliceHeight);
}
/// <summary>
/// Initializes a new instance of <c>PieSlice</c> class with given
/// bounds and visual style.
/// </summary>
/// <param name="boundingRect">
/// Bounding rectangle used to draw the top surface of the slice.
/// </param>
/// <param name="sliceHeight">
/// Pie slice height.
/// </param>
/// <param name="startAngle">
/// Starting angle (in degrees) of the pie slice.
/// </param>
/// <param name="sweepAngle">
/// Sweep angle (in degrees) of the pie slice.
/// </param>
/// <param name="surfaceColor">
/// Color used to render pie slice surface.
/// </param>
/// <param name="shadowStyle">
/// Shadow style used in rendering.
/// </param>
/// <param name="edgeColorType">
/// Edge color type used for rendering.
/// </param>
public PieSlice(RectangleF boundingRect, float sliceHeight, float startAngle, float sweepAngle, Color surfaceColor, ShadowStyle shadowStyle, EdgeColorType edgeColorType)
: this(boundingRect.X, boundingRect.Y, boundingRect.Width, boundingRect.Height, sliceHeight, startAngle, sweepAngle, surfaceColor, shadowStyle, edgeColorType) {
}
/// <summary>
/// Initializes a new instance of <c>PieSlice</c> class with given
/// bounds and visual style.
/// </summary>
/// <param name="xBoundingRect">
/// x-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="yBoundingRect">
/// y-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="widthBoundingRect">
/// Width of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="heightBoundingRect">
/// Height of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="sliceHeight">
/// Height of the pie slice.
/// </param>
/// <param name="startAngle">
/// Starting angle (in degrees) of the pie slice.
/// </param>
/// <param name="sweepAngle">
/// Sweep angle (in degrees) of the pie slice.
/// </param>
/// <param name="surfaceColor">
/// Color used to render pie slice surface.
/// </param>
/// <param name="shadowStyle">
/// Shadow style used in rendering.
/// </param>
/// <param name="edgeColorType">
/// Edge color type used for rendering.
/// </param>
/// <param name="edgeLineWidth">
/// Edge line width.
/// </param>
public PieSlice(float xBoundingRect, float yBoundingRect, float widthBoundingRect, float heightBoundingRect, float sliceHeight, float startAngle, float sweepAngle, Color surfaceColor, ShadowStyle shadowStyle, EdgeColorType edgeColorType, float edgeLineWidth)
: this (xBoundingRect, yBoundingRect, widthBoundingRect, heightBoundingRect, sliceHeight, startAngle, sweepAngle, surfaceColor, shadowStyle, edgeColorType) {
m_pen.Width = edgeLineWidth;
}
/// <summary>
/// Initializes a new instance of <c>PieSlice</c> class with given
/// bounds and visual style.
/// </summary>
/// <param name="boundingRect">
/// Bounding rectangle used to draw the top surface of the pie slice.
/// </param>
/// <param name="sliceHeight">
/// Pie slice height.
/// </param>
/// <param name="startAngle">
/// Starting angle (in degrees) of the pie slice.
/// </param>
/// <param name="sweepAngle">
/// Sweep angle (in degrees) of the pie slice.
/// </param>
/// <param name="surfaceColor">
/// Color used to render pie slice surface.
/// </param>
/// <param name="shadowStyle">
/// Shadow style used in rendering.
/// </param>
/// <param name="edgeColorType">
/// Edge color type used for rendering.
/// </param>
/// <param name="edgeLineWidth">
/// Edge line width.
/// </param>
public PieSlice(Rectangle boundingRect, float sliceHeight, float startAngle, float sweepAngle, Color surfaceColor, ShadowStyle shadowStyle, EdgeColorType edgeColorType, float edgeLineWidth)
: this(boundingRect.X, boundingRect.Y, boundingRect.Width, boundingRect.Height, sliceHeight, startAngle, sweepAngle, surfaceColor, shadowStyle, edgeColorType, edgeLineWidth) {
}
// TODO: 容易造成 mem leak。建议用 Dispose() 改写
/// <summary>
/// <c>Finalize</c> implementation
/// </summary>
~PieSlice() {
Dispose(false);
}
/// <summary>
/// Implementation of <c>IDisposable</c> interface.
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes of all resources used by <c>PieSlice</c> object.
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (!m_disposed) {
if (disposing) {
Debug.Assert(m_pen != null);
m_pen.Dispose();
DisposeBrushes();
Debug.Assert(m_startSide != null);
m_startSide.Dispose();
Debug.Assert(m_endSide != null);
m_endSide.Dispose();
}
m_disposed = true;
}
}
/// <summary>
/// Implementation of ICloneable interface.
/// </summary>
/// <returns>
/// A deep copy of this object.
/// </returns>
public object Clone() {
return new PieSlice(BoundingRectangle, SliceHeight, StartAngle, SweepAngle, m_surfaceColor, m_shadowStyle, m_edgeColorType);
}
/// <summary>
/// Gets starting angle (in degrees) of the pie slice.
/// </summary>
public float StartAngle {
get { return m_startAngle; }
}
/// <summary>
/// Gets sweep angle (in degrees) of the pie slice.
/// </summary>
public float SweepAngle {
get { return m_sweepAngle; }
}
/// <summary>
/// Gets ending angle (in degrees) of the pie slice.
/// </summary>
public float EndAngle {
get { return (m_startAngle + m_sweepAngle) % 360; }
}
public string Text {
get { return m_text; }
set { m_text = value; }
}
/// <summary>
/// Draws the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
public void Draw(Graphics graphics) {
DrawBottom(graphics);
DrawSides(graphics);
DrawTop(graphics);
}
/// <summary>
/// Checks if given pie slice contains given point.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check.
/// </param>
/// <returns>
/// <c>true</c> if point given is contained within the slice.
/// </returns>
public bool Contains(PointF point) {
if (PieSliceContainsPoint(point))
return true;
if (PeripheryContainsPoint(point))
return true;
if (m_startSide.Contains(point))
return true;
if (m_endSide.Contains(point))
return true;
return false;
}
/// <summary>
/// Evaluates the point in the middle of the slice.
/// </summary>
/// <returns>
/// <c>PointF</c> in the middle of the pie top.
/// </returns>
public virtual PointF GetTextPosition() {
if (SweepAngle >= 180)
return PeripheralPoint(m_center.X, m_center.Y, m_boundingRectangle.Width / 3, m_boundingRectangle.Height / 3, GetActualAngle(StartAngle) + SweepAngle / 2);
float x = (m_pointStart.X + m_pointEnd.X) / 2;
float y = (m_pointStart.Y + m_pointEnd.Y) / 2;
float angle = (float)(Math.Atan2(y - m_center.Y, x - m_center.X) * 180 / Math.PI);
return PeripheralPoint(m_center.X, m_center.Y, m_boundingRectangle.Width / 3, m_boundingRectangle.Height / 3, GetActualAngle(angle));
}
/// <summary>
/// Gets or sets the bounding rectangle.
/// </summary>
internal RectangleF BoundingRectangle {
get { return m_boundingRectangle; }
set { m_boundingRectangle = value; }
}
/// <summary>
/// Gets or sets the slice height.
/// </summary>
internal float SliceHeight {
get { return m_sliceHeight; }
set { m_sliceHeight = value; }
}
/// <summary>
/// Draws pie slice sides.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawSides(Graphics graphics) {
DrawHiddenPeriphery(graphics);
// draw wegde sides
if (StartAngle > 90 && StartAngle < 270) {
DrawEndSide(graphics);
DrawStartSide(graphics);
}
else {
DrawStartSide(graphics);
DrawEndSide(graphics);
}
DrawVisiblePeriphery(graphics);
}
/// <summary>
/// Splits a pie slice into two on the split angle.
/// </summary>
/// <param name="splitAngle">
/// Angle at which splitting is performed.
/// </param>
/// <returns>
/// An array of two pie slices.
/// </returns>
internal PieSlice[] Split(float splitAngle) {
// if split angle equals one of bounding angles, then nothing to split
if (StartAngle == splitAngle || this.EndAngle == splitAngle)
return new PieSlice[] { (PieSlice)Clone() };
float transformedSplitAngle = TransformAngle(splitAngle);
float actualStartAngle = GetActualAngle(StartAngle);
float newSweepAngle = (splitAngle - actualStartAngle + 360) % 360;
PieSlice pieSlice1 = new PieSlice(BoundingRectangle, SliceHeight, actualStartAngle, newSweepAngle, m_surfaceColor, m_shadowStyle, m_edgeColorType);
pieSlice1.InitializeSides(true, false);
newSweepAngle = GetActualAngle(EndAngle) - splitAngle;
PieSlice pieSlice2 = new PieSlice(BoundingRectangle, SliceHeight, splitAngle, newSweepAngle, m_surfaceColor, m_shadowStyle, m_edgeColorType);
pieSlice2.InitializeSides(false, true);
return new PieSlice[] { pieSlice1, pieSlice2 };
}
/// <summary>
/// Reajusts the pie slice to fit new bounding rectangle provided.
/// </summary>
/// <param name="xBoundingRect">
/// x-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="yBoundingRect">
/// y-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="widthBoundingRect">
/// Width of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="heightBoundingRect">
/// Height of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="sliceHeight">
/// Height of the pie slice.
/// </param>
internal void Readjust(float xBoundingRect, float yBoundingRect, float widthBoundingRect, float heightBoundingRect, float sliceHeight) {
InitializePieSlice(xBoundingRect, yBoundingRect, widthBoundingRect, heightBoundingRect, sliceHeight);
}
/// <summary>
/// Draws visible start side.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawStartSide(Graphics graphics) {
if (m_startSide != null) {
// checks if the side is visible
if (StartAngle > 90 && StartAngle < 270)
m_startSide.Draw(graphics, m_pen, m_brushStartSide);
else
m_startSide.Draw(graphics, m_pen, m_brushSurface);
}
}
/// <summary>
/// Draws visible end side.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawEndSide(Graphics graphics) {
if (m_endSide != null) {
// checks if the side is visible
if (EndAngle > 90 && EndAngle < 270)
m_endSide.Draw(graphics, m_pen, m_brushSurface);
else
m_endSide.Draw(graphics, m_pen, m_brushEndSide);
}
}
/// <summary>
/// Draws visible outer periphery of the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawVisiblePeriphery(Graphics graphics) {
PeripherySurfaceBounds[] peripherySurfaceBounds = GetVisiblePeripherySurfaceBounds();
foreach (PeripherySurfaceBounds surfaceBounds in peripherySurfaceBounds) {
DrawCylinderSurfaceSection(graphics, m_pen, m_brushPeripherySurface, surfaceBounds.StartAngle, surfaceBounds.EndAngle, surfaceBounds.StartPoint, surfaceBounds.EndPoint);
}
}
/// <summary>
/// Draws hidden outer periphery of the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawHiddenPeriphery(Graphics graphics) {
PeripherySurfaceBounds[] peripherySurfaceBounds = GetHiddenPeripherySurfaceBounds();
foreach (PeripherySurfaceBounds surfaceBounds in peripherySurfaceBounds) {
DrawCylinderSurfaceSection(graphics, m_pen, m_brushSurface, surfaceBounds.StartAngle, surfaceBounds.EndAngle, surfaceBounds.StartPoint, surfaceBounds.EndPoint);
}
}
/// <summary>
/// Draws the bottom of the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawBottom(Graphics graphics) {
graphics.FillPie(m_brushSurface, m_boundingRectangle.X, m_boundingRectangle.Y + m_sliceHeight, m_boundingRectangle.Width, m_boundingRectangle.Height, m_startAngle, m_sweepAngle);
graphics.DrawPie(m_pen, m_boundingRectangle.X, m_boundingRectangle.Y + m_sliceHeight, m_boundingRectangle.Width, m_boundingRectangle.Height, m_startAngle, m_sweepAngle);
}
/// <summary>
/// Draws the top of the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> used to draw the pie slice.
/// </param>
internal void DrawTop(Graphics graphics) {
graphics.FillPie(m_brushSurface, m_boundingRectangle.X, m_boundingRectangle.Y, m_boundingRectangle.Width, m_boundingRectangle.Height, m_startAngle, m_sweepAngle);
graphics.DrawPie(m_pen, m_boundingRectangle, m_startAngle, m_sweepAngle);
}
/// <summary>
/// Calculates the smallest rectangle into which this pie slice fits.
/// </summary>
/// <returns>
/// <c>RectangleF</c> into which this pie slice fits exactly.
/// </returns>
internal RectangleF GetFittingRectangle() {
RectangleF boundingRectangle = new RectangleF(m_pointStart.X, m_pointStart.Y, 0, 0);
if ((m_startAngle == 0F) || (m_startAngle + m_sweepAngle >= 360))
GraphicsUtil.IncludePointX(ref boundingRectangle, m_boundingRectangle.Right);
if ((m_startAngle <= 90) && (m_startAngle + m_sweepAngle >= 90) || (m_startAngle + m_sweepAngle >= 450))
GraphicsUtil.IncludePointY(ref boundingRectangle, m_boundingRectangle.Bottom + SliceHeight);
if ((m_startAngle <= 180) && (m_startAngle + m_sweepAngle >= 180) || (m_startAngle + m_sweepAngle >= 540))
GraphicsUtil.IncludePointX(ref boundingRectangle, m_boundingRectangle.Left);
if ((m_startAngle <= 270) && (m_startAngle + m_sweepAngle >= 270) || (m_startAngle + m_sweepAngle >= 630))
GraphicsUtil.IncludePointY(ref boundingRectangle, m_boundingRectangle.Top);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_center);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_centerBelow);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_pointStart);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_pointStartBelow);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_pointEnd);
GraphicsUtil.IncludePoint(ref boundingRectangle, m_pointEndBelow);
return boundingRectangle;
}
/// <summary>
/// Checks if given point is contained inside the pie slice.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check for.
/// </param>
/// <returns>
/// <c>true</c> if given point is inside the pie slice.
/// </returns>
internal bool PieSliceContainsPoint(PointF point) {
return PieSliceContainsPoint(point, m_boundingRectangle.X, m_boundingRectangle.Y, m_boundingRectangle.Width, m_boundingRectangle.Height, m_startAngle, m_sweepAngle);
}
/// <summary>
/// Checks if given point is contained by cylinder periphery.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check for.
/// </param>
/// <returns>
/// <c>true</c> if given point is inside the cylinder periphery.
/// </returns>
internal bool PeripheryContainsPoint(PointF point) {
PeripherySurfaceBounds[] peripherySurfaceBounds = GetVisiblePeripherySurfaceBounds();
foreach (PeripherySurfaceBounds surfaceBounds in peripherySurfaceBounds) {
if (CylinderSurfaceSectionContainsPoint(point, surfaceBounds.StartAngle, surfaceBounds.EndAngle, surfaceBounds.StartPoint, surfaceBounds.EndPoint))
return true;
}
return false;
}
/// <summary>
/// Checks if point provided is inside pie slice start cut side.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check.
/// </param>
/// <returns>
/// <c>true</c> if point is inside the start side.
/// </returns>
internal bool StartSideContainsPoint(PointF point) {
if (m_sliceHeight > 0)
return (m_startSide.Contains(point));
return false;
}
/// <summary>
/// Checks if point provided is inside pie slice end cut side.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check.
/// </param>
/// <returns>
/// <c>true</c> if point is inside the end side.
/// </returns>
internal bool EndSideContainsPoint(PointF point) {
if (m_sliceHeight > 0)
return (m_endSide.Contains(point));
return false;
}
/// <summary>
/// Checks if bottom side of the pie slice contains the point.
/// </summary>
/// <param name="point">
/// <c>PointF</c> to check.
/// </param>
/// <returns>
/// <c>true</c> if point is inside the bottom of the pie slice.
/// </returns>
internal bool BottomSurfaceSectionContainsPoint(PointF point) {
if (m_sliceHeight > 0) {
return (PieSliceContainsPoint(point, m_boundingRectangle.X, m_boundingRectangle.Y + m_sliceHeight, m_boundingRectangle.Width, m_boundingRectangle.Height, m_startAngle, m_sweepAngle));
}
return false;
}
/// <summary>
/// Creates brushes used to render the pie slice.
/// </summary>
/// <param name="surfaceColor">
/// Color used for rendering.
/// </param>
/// <param name="shadowStyle">
/// Shadow style used for rendering.
/// </param>
protected virtual void CreateSurfaceBrushes(Color surfaceColor, ShadowStyle shadowStyle) {
m_brushSurface = new SolidBrush(surfaceColor);
m_brushSurfaceHighlighted = new SolidBrush(ColorUtil.CreateColorWithCorrectedLightness(surfaceColor, ColorUtil.BrightnessEnhancementFactor1));
switch (shadowStyle) {
case ShadowStyle.NoShadow:
m_brushStartSide = m_brushEndSide = m_brushPeripherySurface = new SolidBrush(surfaceColor);
break;
case ShadowStyle.UniformShadow:
m_brushStartSide = m_brushEndSide = m_brushPeripherySurface = new SolidBrush(ColorUtil.CreateColorWithCorrectedLightness(surfaceColor, -ColorUtil.BrightnessEnhancementFactor1));
break;
case ShadowStyle.GradualShadow:
double angle = m_startAngle - 180 - s_shadowAngle;
if (angle < 0)
angle += 360;
m_brushStartSide = CreateBrushForSide(surfaceColor, angle);
angle = m_startAngle + m_sweepAngle - s_shadowAngle;
if (angle < 0)
angle += 360;
m_brushEndSide = CreateBrushForSide(surfaceColor, angle);
m_brushPeripherySurface = CreateBrushForPeriphery(surfaceColor);
break;
}
}
/// <summary>
/// Disposes brush objects.
/// </summary>
protected void DisposeBrushes() {
Debug.Assert(m_brushSurface != null);
Debug.Assert(m_brushStartSide != null);
Debug.Assert(m_brushEndSide != null);
Debug.Assert(m_brushPeripherySurface != null);
Debug.Assert(m_brushSurfaceHighlighted != null);
m_brushSurface.Dispose();
m_brushStartSide.Dispose();
m_brushEndSide.Dispose();
m_brushPeripherySurface.Dispose();
m_brushSurfaceHighlighted.Dispose();
}
/// <summary>
/// Creates a brush for start and end sides of the pie slice for
/// gradual shade.
/// </summary>
/// <param name="color">
/// Color used for pie slice rendering.
/// </param>
/// <param name="angle">
/// Angle of the surface.
/// </param>
/// <returns>
/// <c>Brush</c> object.
/// </returns>
protected virtual Brush CreateBrushForSide(Color color, double angle) {
return new SolidBrush(ColorUtil.CreateColorWithCorrectedLightness(color, -(float)(ColorUtil.BrightnessEnhancementFactor1 * (1 - 0.8 * Math.Cos(angle * Math.PI / 180)))));
}
/// <summary>
/// Creates a brush for outer periphery of the pie slice used for
/// gradual shadow.
/// </summary>
/// <param name="color">
/// Color used for pie slice rendering.
/// </param>
/// <returns>
/// <c>Brush</c> object.
/// </returns>
protected virtual Brush CreateBrushForPeriphery(Color color) {
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = new Color[] {
ColorUtil.CreateColorWithCorrectedLightness(color, -ColorUtil.BrightnessEnhancementFactor1 / 2),
color,
ColorUtil.CreateColorWithCorrectedLightness(color, -ColorUtil.BrightnessEnhancementFactor1),
};
colorBlend.Positions = new float[] { 0F, 0.1F, 1.0F };
LinearGradientBrush brush = new LinearGradientBrush(m_boundingRectangle, Color.Blue, Color.White, LinearGradientMode.Horizontal);
brush.InterpolationColors = colorBlend;
return brush;
}
/// <summary>
/// Draws the outer periphery of the pie slice.
/// </summary>
/// <param name="graphics">
/// <c>Graphics</c> object used to draw the surface.
/// </param>
/// <param name="pen">
/// <c>Pen</c> used to draw outline.
/// </param>
/// <param name="brush">
/// <c>Brush</c> used to fill the quadrilateral.
/// </param>
/// <param name="boundingRect">
/// Bounding rectangle that is used to draw the top surface of the
/// pie slice.
/// </param>
/// <param name="startAngle">
/// Start angle (in degrees) of the periphery section.
/// </param>
/// <param name="endAngle">
/// End angle (in degrees) of the periphery section.
/// </param>
/// <param name="pointStart">
/// Point representing the start of the periphery.
/// </param>
/// <param name="pointEnd">
/// Point representing the end of the periphery.
/// </param>
protected void DrawCylinderSurfaceSection(Graphics graphics, Pen pen, Brush brush, float startAngle, float endAngle, PointF pointStart, PointF pointEnd) {
GraphicsPath path = CreatePathForCylinderSurfaceSection(startAngle, endAngle, pointStart, pointEnd);
graphics.FillPath(brush, path);
graphics.DrawPath(pen, path);
}
/// <summary>
/// Transforms actual angle to angle used for rendering. They are
/// different because of perspective.
/// </summary>
/// <param name="angle">
/// Actual angle.
/// </param>
/// <returns>
/// Rendering angle.
/// </returns>
protected float TransformAngle(float angle) {
double x = m_boundingRectangle.Width * Math.Cos(angle * Math.PI / 180);
double y = m_boundingRectangle.Height * Math.Sin(angle * Math.PI / 180);
float result = (float)(Math.Atan2(y, x) * 180 / Math.PI);
if (result < 0)
return result + 360;
return result;
}
/// <summary>
/// Gets the actual angle from the rendering angle.
/// </summary>
/// <param name="transformedAngle">
/// Transformed angle for which actual angle has to be evaluated.
/// </param>
/// <returns>
/// Actual angle.
/// </returns>
protected float GetActualAngle(float transformedAngle) {
double x = m_boundingRectangle.Height * Math.Cos(transformedAngle * Math.PI / 180);
double y = m_boundingRectangle.Width * Math.Sin(transformedAngle * Math.PI / 180);
float result = (float)(Math.Atan2(y, x) * 180 / Math.PI);
if (result < 0)
return result + 360;
return result;
}
/// <summary>
/// Calculates the point on ellipse periphery for angle.
/// </summary>
/// <param name="xCenter">
/// x-coordinate of the center of the ellipse.
/// </param>
/// <param name="yCenter">
/// y-coordinate of the center of the ellipse.
/// </param>
/// <param name="semiMajor">
/// Horizontal semi-axis.
/// </param>
/// <param name="semiMinor">
/// Vertical semi-axis.
/// </param>
/// <param name="angleDegrees">
/// Angle (in degrees) for which corresponding periphery point has to
/// be obtained.
/// </param>
/// <returns>
/// <c>PointF</c> on the ellipse.
/// </returns>
protected PointF PeripheralPoint(float xCenter, float yCenter, float semiMajor, float semiMinor, float angleDegrees) {
double angleRadians = angleDegrees * Math.PI / 180;
return new PointF(xCenter + (float)(semiMajor * Math.Cos(angleRadians)), yCenter + (float)(semiMinor * Math.Sin(angleRadians)));
}
/// <summary>
/// Initializes pie bounding rectangle, pie height, corners
/// coordinates and brushes used for rendering.
/// </summary>
/// <param name="xBoundingRect">
/// x-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="yBoundingRect">
/// y-coordinate of the upper-left corner of the rectangle that is
/// used to draw the top surface of the pie slice.
/// </param>
/// <param name="widthBoundingRect">
/// Width of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="heightBoundingRect">
/// Height of the rectangle that is used to draw the top surface of
/// the pie slice.
/// </param>
/// <param name="sliceHeight">
/// Height of the pie slice.
/// </param>
private void InitializePieSlice(float xBoundingRect, float yBoundingRect, float widthBoundingRect, float heightBoundingRect, float sliceHeight) {
// stores bounding rectangle and pie slice height
m_boundingRectangle = new RectangleF(xBoundingRect, yBoundingRect, widthBoundingRect, heightBoundingRect);
m_sliceHeight = sliceHeight;
// recalculates start and sweep angle used for rendering
m_startAngle = TransformAngle(m_actualStartAngle);
m_sweepAngle = m_actualSweepAngle;
if (m_sweepAngle % 180 != 0F)
m_sweepAngle = TransformAngle(m_actualStartAngle + m_actualSweepAngle) - m_startAngle;
if (m_sweepAngle < 0)
m_sweepAngle += 360;
// creates brushes
CreateSurfaceBrushes(m_surfaceColor, m_shadowStyle);
// calculates center and end points on periphery
float xCenter = xBoundingRect + widthBoundingRect / 2;
float yCenter = yBoundingRect + heightBoundingRect / 2;
m_center = new PointF(xCenter, yCenter);
m_centerBelow = new PointF(xCenter, yCenter + sliceHeight);
m_pointStart = PeripheralPoint(xCenter, yCenter, widthBoundingRect / 2, heightBoundingRect / 2, m_actualStartAngle);
m_pointStartBelow = new PointF(m_pointStart.X, m_pointStart.Y + sliceHeight);
m_pointEnd = PeripheralPoint(xCenter, yCenter, widthBoundingRect / 2, heightBoundingRect / 2, m_actualStartAngle + m_actualSweepAngle);
m_pointEndBelow = new PointF(m_pointEnd.X, m_pointEnd.Y + sliceHeight);
InitializeSides();
}
/// <summary>
/// Initializes start and end pie slice sides.
/// </summary>
private void InitializeSides() {
InitializeSides(true, true);
}
/// <summary>
/// Initializes start and end pie slice sides.
/// </summary>
/// <param name="startSideExists">
/// Does start side exists.
/// </param>
/// <param name="endSideExists">
/// Does end side exists.
/// </param>
private void InitializeSides(bool startSideExists, bool endSideExists) {
if (startSideExists)
m_startSide = new Quadrilateral(m_center, m_pointStart, m_pointStartBelow, m_centerBelow, m_sweepAngle != 180);
else
m_startSide = Quadrilateral.Empty;
if (endSideExists)
m_endSide = new Quadrilateral(m_center, m_pointEnd, m_pointEndBelow, m_centerBelow, m_sweepAngle != 180);
else
m_endSide = Quadrilateral.Empty;
}
/// <summary>
/// Gets an array of visible periphery bounds.
/// </summary>
/// <returns>
/// Array of <c>PeripherySurfaceBounds</c> objects.
/// </returns>
private PeripherySurfaceBounds[] GetVisiblePeripherySurfaceBounds() {
ArrayList peripherySurfaceBounds = new ArrayList();
// outer periphery side is visible only when startAngle or endAngle
// is between 0 and 180 degrees
if (!(m_sweepAngle == 0 || (m_startAngle >= 180 && m_startAngle + m_sweepAngle <= 360))) {
// draws the periphery from start angle to the end angle or left
// edge, whichever comes first
if (StartAngle < 180) {
float fi1 = m_startAngle;
PointF x1 = new PointF(m_pointStart.X, m_pointStart.Y);
float fi2 = EndAngle;
PointF x2 = new PointF(m_pointEnd.X, m_pointEnd.Y);
if (m_startAngle + m_sweepAngle > 180) {
fi2 = 180;
x2.X = m_boundingRectangle.X;
x2.Y = m_center.Y;
}
peripherySurfaceBounds.Add(new PeripherySurfaceBounds(fi1, fi2, x1, x2));
}
// if lateral surface is visible from the right edge
if (m_startAngle + m_sweepAngle > 360) {
float fi1 = 0;
PointF x1 = new PointF(m_boundingRectangle.Right, m_center.Y);
float fi2 = EndAngle;
PointF x2 = new PointF(m_pointEnd.X, m_pointEnd.Y);
if (fi2 > 180) {
fi2 = 180;
x2.X = m_boundingRectangle.Left;
x2.Y = m_center.Y;
}
peripherySurfaceBounds.Add(new PeripherySurfaceBounds(fi1, fi2, x1, x2));
}
}
return (PeripherySurfaceBounds[])peripherySurfaceBounds.ToArray(typeof(PeripherySurfaceBounds));
}
/// <summary>
/// Gets an array of hidden periphery bounds.
/// </summary>
/// <returns>
/// Array of <c>PeripherySurfaceBounds</c> objects.
/// </returns>
private PeripherySurfaceBounds[] GetHiddenPeripherySurfaceBounds() {
ArrayList peripherySurfaceBounds = new ArrayList();
// outer periphery side is not visible when startAngle or endAngle
// is between 180 and 360 degrees
if (!(m_sweepAngle == 0 || (m_startAngle >= 0 && m_startAngle + m_sweepAngle <= 180))) {
// draws the periphery from start angle to the end angle or right
// edge, whichever comes first
if (m_startAngle + m_sweepAngle > 180) {
float fi1 = m_startAngle;
PointF x1 = new PointF(m_pointStart.X, m_pointStart.Y);
float fi2 = m_startAngle + m_sweepAngle;
PointF x2 = new PointF(m_pointEnd.X, m_pointEnd.Y);
if (fi1 < 180) {
fi1 = 180;
x1.X = m_boundingRectangle.Left;
x1.Y = m_center.Y;
}
if (fi2 > 360) {
fi2 = 360;
x2.X = m_boundingRectangle.Right;
x2.Y = m_center.Y;
}
peripherySurfaceBounds.Add(new PeripherySurfaceBounds(fi1, fi2, x1, x2));
// if pie is crossing 360 & 180 deg. boundary, we have to
// invisible peripheries
if (m_startAngle < 360 && m_startAngle + m_sweepAngle > 540) {
fi1 = 180;
x1 = new PointF(m_boundingRectangle.Left, m_center.Y);
fi2 = EndAngle;
x2 = new PointF(m_pointEnd.X, m_pointEnd.Y);
peripherySurfaceBounds.Add(new PeripherySurfaceBounds(fi1, fi2, x1, x2));
}
}
}
return (PeripherySurfaceBounds[])peripherySurfaceBounds.ToArray(typeof(PeripherySurfaceBounds));
}
/// <summary>
/// Creates <c>GraphicsPath</c> for cylinder surface section. This
/// path consists of two arcs and two vertical lines.
/// </summary>
/// <param name="startAngle">
/// Starting angle of the surface.
/// </param>
/// <param name="endAngle">
/// Ending angle of the surface.
/// </param>
/// <param name="pointStart">
/// Starting point on the cylinder surface.
/// </param>
/// <param name="pointEnd">
/// Ending point on the cylinder surface.
/// </param>
/// <returns>
/// <c>GraphicsPath</c> object representing the cylinder surface.
/// </returns>
private GraphicsPath CreatePathForCylinderSurfaceSection(float startAngle, float endAngle, PointF pointStart, PointF pointEnd) {
GraphicsPath path = new GraphicsPath();
path.AddArc(m_boundingRectangle, startAngle, endAngle - startAngle);
path.AddLine(pointEnd.X, pointEnd.Y, pointEnd.X, pointEnd.Y + m_sliceHeight);
path.AddArc(m_boundingRectangle.X, m_boundingRectangle.Y + m_sliceHeight, m_boundingRectangle.Width, m_boundingRectangle.Height, endAngle, startAngle - endAngle);
path.AddLine(pointStart.X, pointStart.Y + m_sliceHeight, pointStart.X, pointStart.Y);
return path;
}
/// <summary>
/// Checks if given point is contained within upper and lower pie
/// slice surfaces or within the outer slice brink.
/// </summary>
/// <param name="point">
/// <c>PointF</c> structure to check for.
/// </param>
/// <param name="startAngle">
/// Start angle of the slice.
/// </param>
/// <param name="endAngle">
/// End angle of the slice.
/// </param>
/// <param name="point1">
/// Starting point on the periphery.
/// </param>
/// <param name="point2">
/// Ending point on the periphery.
/// </param>
/// <returns>
/// <c>true</c> if point given is contained.
/// </returns>
private bool CylinderSurfaceSectionContainsPoint(PointF point, float startAngle, float endAngle, PointF point1, PointF point2) {
if (m_sliceHeight > 0) {