OSDN Git Service

flip ok
[psychlops/silverlight.git] / dev3 / psychlops / core / graphic / shape.cs
1 using System;\r
2 using System.Windows;\r
3 using System.Windows.Controls;\r
4 using System.Windows.Documents;\r
5 using System.Windows.Input;\r
6 using System.Windows.Media;\r
7 using System.Windows.Media.Animation;\r
8 using System.Windows.Media.Imaging;\r
9 using System.Windows.Shapes;\r
10 \r
11 \r
12 \r
13 namespace Psychlops{\r
14 \r
15         public interface Shape : Figure\r
16         {\r
17                 void draw(Color c);\r
18                 Color fill { get; set; }\r
19                 Stroke stroke { get; set; }\r
20         }\r
21         public static class ShapeExtention\r
22         {\r
23                 /*\r
24                 public static void draw(this Shape drawee)\r
25                 {\r
26                         drawee.draw(Color.white);\r
27                 }\r
28                 */\r
29         }\r
30 \r
31         public partial struct Stroke\r
32         {\r
33                 public double thick;\r
34                 public Color color;\r
35                 public void set(Color c, double t)\r
36                 {\r
37                         color = c;\r
38                         thick = t;\r
39                 }\r
40         }\r
41 \r
42         public partial class Line : Shape\r
43         {\r
44                 public Point begin, end;\r
45 \r
46                 public Line(double x1, double y1, double x2, double y2)\r
47                 {\r
48                         set(x1, y1, x2, y2);\r
49                 }\r
50                 public Line(Point v1, Point v2)\r
51                 {\r
52                         set(v1, v2);\r
53                 }\r
54                 public void set(double x1, double y1, double x2, double y2)\r
55                 {\r
56                         begin.set(x1, y1);\r
57                         end.set(x2, y2);\r
58                 }\r
59                 public void set(Point v1, Point v2)\r
60                 {\r
61                         begin = v1;\r
62                         end   = v2;\r
63                 }\r
64 \r
65                 public Figure shift(Point p)\r
66                 {\r
67                         begin += p;\r
68                         end   += p;\r
69                         return this;\r
70                 }\r
71                 public Figure centering(Point p)\r
72                 {\r
73                         double h = width, v = height;\r
74                         begin.x = p.x - h / 2.0;\r
75                         begin.y = p.y - v / 2.0;\r
76                         end.x = begin.x + h;\r
77                         end.y = begin.y + v;\r
78                         return this;\r
79                 }\r
80 \r
81                 public void draw(Color c)\r
82                 {\r
83                         Main.drawable.line(this, c);\r
84                 }\r
85                 public void draw()\r
86                 {\r
87                         Main.drawable.line(this);\r
88                 }\r
89 \r
90                 public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
91                 public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
92                 public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
93                 public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
94                 public double width { get { return Math.abs(begin.x - end.x); } }\r
95                 public double height { get { return Math.abs(begin.y - end.y); } }\r
96 \r
97                 public Color fill { get; set; }\r
98                 public Stroke stroke { get; set; }\r
99         }\r
100 \r
101 \r
102         public partial class Rectangle : Shape\r
103         {\r
104                 public Point v1, v2;\r
105 \r
106                 public Rectangle()\r
107                 {\r
108                         set(0,0);\r
109                 }\r
110                 public Rectangle(double wid, double hei)\r
111                 {\r
112                         set(wid, hei);\r
113                 }\r
114                 \r
115                 public Rectangle set(double wid, double hei)\r
116                 {\r
117                         v1.set(0, 0, 0);\r
118                         v2.set(wid, hei, 0);\r
119                         return this;\r
120                 }\r
121 \r
122                 public Figure shift(Point p)\r
123                 {\r
124                         v1 += p;\r
125                         v2 += p;\r
126                         return this;\r
127                 }\r
128                 public Figure centering(Point p)\r
129                 {\r
130                         double h = width, v = height;\r
131                         v1.x = p.x - h / 2.0;\r
132                         v1.y = p.y - v / 2.0;\r
133                         v2.x = v1.x + h;\r
134                         v2.y = v1.y + v;\r
135                         return this;\r
136                 }\r
137 \r
138                 public void draw(Color c)\r
139                 {\r
140                         Main.drawable.rect(this, c);\r
141                 }\r
142                 public void draw()\r
143                 {\r
144                         Main.drawable.rect(this);\r
145                 }\r
146 \r
147                 public double left   { get { return v1.x; } }\r
148                 public double top    { get { return v1.y; } }\r
149                 public double right  { get { return v2.x; } }\r
150                 public double bottom { get { return v2.y; } }\r
151                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
152                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
153 \r
154 \r
155                 public Color fill { get; set; }\r
156                 public Stroke stroke { get; set; }\r
157 \r
158         }\r
159 \r
160 \r
161         public partial class Ellipse : Shape\r
162         {\r
163                 public Point datum;\r
164                 public double xdiameter, ydiameter;\r
165 \r
166                 public Ellipse()\r
167                 {\r
168                         set(0,0);\r
169                 }\r
170                 public Ellipse(double wid, double hei)\r
171                 {\r
172                         set(wid, hei);\r
173                 }\r
174 \r
175                 public Ellipse set(double wid, double hei)\r
176                 {\r
177                         xdiameter = wid;\r
178                         ydiameter = hei;\r
179                         return this;\r
180                 }\r
181                 public Figure shift(Point p)\r
182                 {\r
183                         datum += p;\r
184                         return this;\r
185                 }\r
186                 public Figure centering(Point p)\r
187                 {\r
188                         datum = p;\r
189                         return this;\r
190                 }\r
191 \r
192                 public void draw(Color c)\r
193                 {\r
194                         Main.drawable.ellipse(this, c);\r
195                 }\r
196                 public void draw()\r
197                 {\r
198                         Main.drawable.ellipse(this);\r
199                 }\r
200 \r
201                 public double left { get { return datum.x - xdiameter/2.0; } }\r
202                 public double top { get { return datum.y - ydiameter / 2.0; } }\r
203                 public double right { get { return datum.x + xdiameter / 2.0; } }\r
204                 public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
205                 public double width { get { return Math.abs(xdiameter); } }\r
206                 public double height { get { return Math.abs(ydiameter); } }\r
207 \r
208                 public Color fill { get; set; }\r
209                 public Stroke stroke { get; set; }\r
210         }\r
211 \r
212 \r
213         public partial class Polygon : Shape\r
214         {\r
215                 public Point datum;\r
216                 public System.Collections.Generic.List<Point> vertices;\r
217 \r
218                 public Polygon()\r
219                 {\r
220                         vertices = new System.Collections.Generic.List<Point>();\r
221                 }\r
222                 public Polygon(double[] verts)\r
223                 {\r
224                         vertices = new System.Collections.Generic.List<Point>();\r
225                         for (int i=0; i < verts.Length; i+=2)\r
226                         {\r
227                                 vertices.Add(new Point(verts[i], verts[i+1]));\r
228                         }\r
229 \r
230                 }\r
231                 public Polygon(Point[] verts)\r
232                 {\r
233                         vertices = new System.Collections.Generic.List<Point>();\r
234                         foreach (Point p in verts)\r
235                         {\r
236                                 vertices.Add(p);\r
237                         }\r
238 \r
239                 }\r
240                 public Polygon append(Point p) { vertices.Add(p); return this; }\r
241                 public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
242                 public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
243 \r
244 \r
245                 public Figure shift(Point p)\r
246                 {\r
247                         datum += p;\r
248                         return this;\r
249                 }\r
250                 public Figure centering(Point p)\r
251                 {\r
252                         datum = p;\r
253                         return this;\r
254                 }\r
255 \r
256                 public void draw(Color c)\r
257                 {\r
258                         Main.drawable.polygon(this, c);\r
259                 }\r
260                 public void draw()\r
261                 {\r
262                         Main.drawable.polygon(this);\r
263                 }\r
264 \r
265                 public Color fill { get; set; }\r
266                 public Stroke stroke { get; set; }\r
267         }\r
268 \r
269 }