root/plot/Pybrary.Plot/Symbol.cs

Revision 746, 7.9 kB (checked in by mfenniak, 2 years ago)

--

Line 
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5 using System.Reflection;
6
7 namespace Pybrary.Plot
8 {
9     public enum SymbolType
10     {
11         Square, Circle, XCross, Cross, TriangleUp, Diamond,
12         TriangleDown, TriangleLeft, TriangleRight
13     };
14
15     public static class SymbolFactory
16     {
17         public static IEnumerable<SymbolType> StandardSymbols
18         {
19             get
20             {
21                 yield return SymbolType.Square;
22                 yield return SymbolType.Circle;
23                 yield return SymbolType.XCross;
24                 yield return SymbolType.Cross;
25                 yield return SymbolType.TriangleUp;
26                 yield return SymbolType.Diamond;
27                 yield return SymbolType.TriangleDown;
28                 yield return SymbolType.TriangleLeft;
29                 yield return SymbolType.TriangleRight;
30                 // Never run out - just repeat ourselves.
31                 foreach (SymbolType s in SymbolFactory.StandardSymbols)
32                     yield return s;
33             }
34         }
35
36         public static Symbol CreateSymbol(SymbolType type, PenDescription f, BrushDescription b, float size)
37         {
38             switch (type)
39             {
40                 case SymbolType.Square:
41                     return new SquareSymbol(f, b, size);
42                 case SymbolType.Circle:
43                     return new CircleSymbol(f, b, size);
44                 case SymbolType.XCross:
45                     return new XCrossSymbol(f, size);
46                 case SymbolType.Cross:
47                     return new CrossSymbol(f, size);
48                 case SymbolType.Diamond:
49                     return new DiamondSymbol(f, b, size);
50                 case SymbolType.TriangleUp:
51                     return new TriangleUpSymbol(f, b, size);
52                 case SymbolType.TriangleDown:
53                     return new TriangleDownSymbol(f, b, size);
54                 case SymbolType.TriangleLeft:
55                     return new TriangleLeftSymbol(f, b, size);
56                 case SymbolType.TriangleRight:
57                     return new TriangleRightSymbol(f, b, size);
58             }
59             return null;
60         }
61     }
62
63     public abstract class Symbol : IDisposable
64     {
65         protected float size;
66
67         public Symbol(float size)
68         {
69             this.size = size;
70         }
71
72         public abstract void DrawCenteredAt(Graphics g, PointF point);
73
74         public virtual void Dispose()
75         {
76         }
77     }
78
79     abstract class PenSymbol : Symbol
80     {
81         protected Pen p;
82         public PenSymbol(PenDescription f, float size)
83             : base(size)
84         {
85             p = f.CreatePen();
86         }
87         public override void Dispose()
88         {
89             p.Dispose();
90             p = null;
91         }
92     }
93
94     abstract class PenBrushSymbol : PenSymbol
95     {
96         protected Brush br;
97         public PenBrushSymbol(PenDescription f, BrushDescription b, float size)
98             : base(f, size)
99         {
100             this.br = b.CreateBrush();
101         }
102         public override void Dispose()
103         {
104             br.Dispose();
105             br = null;
106             base.Dispose();
107         }
108     }
109
110     class CircleSymbol : PenBrushSymbol
111     {
112         public CircleSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
113         public override void DrawCenteredAt(Graphics g, PointF point)
114         {
115             g.FillEllipse(br, point.X - (size / 2), point.Y - (size / 2), size, size);
116             g.DrawEllipse(p, point.X - (size / 2), point.Y - (size / 2), size, size);
117         }
118     }
119
120     class SquareSymbol : PenBrushSymbol
121     {
122         public SquareSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
123         public override void DrawCenteredAt(Graphics g, PointF point)
124         {
125             g.FillRectangle(br, point.X - (size / 2), point.Y - (size / 2), size, size);
126             g.DrawRectangle(p, point.X - (size / 2), point.Y - (size / 2), size, size);
127         }
128     }
129
130     class XCrossSymbol : PenSymbol
131     {
132         public XCrossSymbol(PenDescription f, float size) : base(f, size) { }
133         public override void DrawCenteredAt(Graphics g, PointF point)
134         {
135             float half = size / 2;
136             g.DrawLine(p, point.X - half, point.Y - half, point.X + half, point.Y + half);
137             g.DrawLine(p, point.X - half, point.Y + half, point.X + half, point.Y - half);
138         }
139     }
140
141     class CrossSymbol : PenSymbol
142     {
143         public CrossSymbol(PenDescription f, float size) : base(f, size) { }
144         public override void DrawCenteredAt(Graphics g, PointF point)
145         {
146             float half = size / 2;
147             g.DrawLine(p, point.X - half, point.Y, point.X + half, point.Y);
148             g.DrawLine(p, point.X, point.Y + half, point.X, point.Y - half);
149         }
150     }
151
152     class DiamondSymbol : PenBrushSymbol
153     {
154         public DiamondSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
155         public override void DrawCenteredAt(Graphics g, PointF point)
156         {
157             float half = size / 2;
158             PointF[] poly = new PointF[] {
159                 new PointF(point.X - half, point.Y),
160                 new PointF(point.X, point.Y - half),
161                 new PointF(point.X + half, point.Y),
162                 new PointF(point.X, point.Y + half)
163             };
164             g.FillPolygon(br, poly);
165             g.DrawPolygon(p, poly);
166         }
167     }
168
169     class TriangleDownSymbol : PenBrushSymbol
170     {
171         public TriangleDownSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
172         public override void DrawCenteredAt(Graphics g, PointF point)
173         {
174             float half = size / 2;
175             PointF[] poly = new PointF[] {
176                 new PointF(point.X, point.Y + half),
177                 new PointF(point.X - half, point.Y - half),
178                 new PointF(point.X + half, point.Y - half),
179             };
180             g.FillPolygon(br, poly);
181             g.DrawPolygon(p, poly);
182         }
183     }
184
185     class TriangleLeftSymbol : PenBrushSymbol
186     {
187         public TriangleLeftSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
188         public override void DrawCenteredAt(Graphics g, PointF point)
189         {
190             float half = size / 2;
191             PointF[] poly = new PointF[] {
192                 new PointF(point.X - half, point.Y),
193                 new PointF(point.X + half, point.Y - half),
194                 new PointF(point.X + half, point.Y + half),
195             };
196             g.FillPolygon(br, poly);
197             g.DrawPolygon(p, poly);
198         }
199     }
200
201     class TriangleRightSymbol : PenBrushSymbol
202     {
203         public TriangleRightSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
204         public override void DrawCenteredAt(Graphics g, PointF point)
205         {
206             float half = size / 2;
207             PointF[] poly = new PointF[] {
208                 new PointF(point.X + half, point.Y),
209                 new PointF(point.X - half, point.Y + half),
210                 new PointF(point.X - half, point.Y - half),
211             };
212             g.FillPolygon(br, poly);
213             g.DrawPolygon(p, poly);
214         }
215     }
216
217     class TriangleUpSymbol : PenBrushSymbol
218     {
219         public TriangleUpSymbol(PenDescription f, BrushDescription b, float size) : base(f, b, size) { }
220         public override void DrawCenteredAt(Graphics g, PointF point)
221         {
222             float half = size / 2;
223             PointF[] poly = new PointF[] {
224                 new PointF(point.X, point.Y - half),
225                 new PointF(point.X + half, point.Y + half),
226                 new PointF(point.X - half, point.Y + half),
227             };
228             g.FillPolygon(br, poly);
229             g.DrawPolygon(p, poly);
230         }
231     }
232 }
Note: See TracBrowser for help on using the browser.