OSDN Git Service

3 to 4
[psychlops/silverlight.git] / dev4 / 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 : Internal.PrimitiveFigure\r
16         {\r
17                 Color fill { get; set; }\r
18                 Stroke stroke { get; set; }\r
19         }\r
20         public static class ShapeExtention\r
21         {\r
22                 public static void draw(this Shape drawee, Color c)\r
23                 {\r
24                         Color tmp_col = drawee.fill;\r
25                         Stroke tmp_strk = drawee.stroke;\r
26                         drawee.fill = c;\r
27                         drawee.stroke = new Stroke();\r
28                         drawee.draw();\r
29                         drawee.fill = tmp_col;\r
30                         drawee.stroke = tmp_strk;\r
31                 }\r
32                 public static void draw(this Shape drawee, Stroke strk)\r
33                 {\r
34                         Color tmp_col = drawee.fill;\r
35                         Stroke tmp_strk = drawee.stroke;\r
36                         drawee.fill = new Color(0,0,1,1);\r
37                         drawee.stroke = strk;\r
38                         drawee.draw();\r
39                         drawee.fill = tmp_col;\r
40                         drawee.stroke = tmp_strk;\r
41                 }\r
42         }\r
43 \r
44         public partial struct Stroke\r
45         {\r
46                 public double thick;\r
47                 public Color color;\r
48                 public Stroke(Color c, double t)\r
49                 {\r
50                         color = c;\r
51                         thick = t;\r
52                 }\r
53                 public void set(Color c, double t)\r
54                 {\r
55                         color = c;\r
56                         thick = t;\r
57                 }\r
58         }\r
59 \r
60         public partial class Line : Shape\r
61         {\r
62                 public Point begin, end;\r
63                 public Point datum\r
64                 {\r
65                         get { return begin; }\r
66                         set { begin = value; }\r
67                 }\r
68 \r
69                 public Line(double x1, double y1, double x2, double y2)\r
70                 {\r
71                         set(x1, y1, x2, y2);\r
72                 }\r
73                 public Line(Point v1, Point v2)\r
74                 {\r
75                         set(v1, v2);\r
76                 }\r
77                 public void set(double x1, double y1, double x2, double y2)\r
78                 {\r
79                         begin.set(x1, y1);\r
80                         end.set(x2, y2);\r
81                 }\r
82                 public void set(Point v1, Point v2)\r
83                 {\r
84                         begin = v1;\r
85                         end   = v2;\r
86                 }\r
87 \r
88                 public Figure shift(Point p)\r
89                 {\r
90                         begin += p;\r
91                         end   += p;\r
92                         return this;\r
93                 }\r
94                 public Figure centering(Point p)\r
95                 {\r
96                         double h = width, v = height;\r
97                         begin.x = p.x - h / 2.0;\r
98                         begin.y = p.y - v / 2.0;\r
99                         end.x = begin.x + h;\r
100                         end.y = begin.y + v;\r
101                         return this;\r
102                 }\r
103 \r
104                 public void draw()\r
105                 {\r
106                         Main.drawable.line(this);\r
107                 }\r
108 \r
109                 public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
110                 public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
111                 public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
112                 public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
113                 public double width { get { return Math.abs(begin.x - end.x); } }\r
114                 public double height { get { return Math.abs(begin.y - end.y); } }\r
115                 public double getLeft() { return left; }\r
116                 public double getTop() { return top; }\r
117                 public double getRight() { return right; }\r
118                 public double getBottom() { return bottom; }\r
119                 public double getWidth() { return width; }\r
120                 public double getHeight() { return height; }\r
121 \r
122                 public Color fill { get; set; }\r
123                 public Stroke stroke { get; set; }\r
124         }\r
125 \r
126 \r
127         public partial class Rectangle : Shape\r
128         {\r
129                 public Point v1, v2;\r
130 \r
131                 public Rectangle()\r
132                 {\r
133                         set(0,0);\r
134                 }\r
135                 public Rectangle(double wid, double hei)\r
136                 {\r
137                         set(wid, hei);\r
138                 }\r
139                 public Rectangle(Rectangle another)\r
140                 {\r
141                         v1 = another.v1;\r
142                         v2 = another.v2;\r
143                 }\r
144                 public Rectangle set(double wid, double hei)\r
145                 {\r
146                         v1.set(0, 0, 0);\r
147                         v2.set(wid, hei, 0);\r
148                         return this;\r
149                 }\r
150                 Rectangle set(Point po1, Point po2) {\r
151                         v1 = po1;\r
152                         v2 = po2;\r
153                         return this;\r
154                 }\r
155                 public Rectangle set(double l, double t, double r, double b)\r
156                 {\r
157                         v1.set(l, t, 0);\r
158                         v2.set(r, b, 0);\r
159                         return this;\r
160                 }\r
161                 public Rectangle set(Rectangle another)\r
162                 {\r
163                         v1 = another.v1;\r
164                         v2 = another.v2;\r
165                         return this;\r
166                 }\r
167 \r
168                 public Rectangle resize(double width, double height)\r
169                 {\r
170                         Point po = center;\r
171                         set(width, height);\r
172                         centering(po);\r
173                         return this;\r
174                 }\r
175 \r
176 \r
177                 public Point datum\r
178                 {\r
179                         get { return v1; }\r
180                         set { double w = width, h = height; v1 = value; v2 = v1 + new Point(w,h); }\r
181                 }\r
182                 public Rectangle move_to(Point p) { datum = p; return this; }\r
183                 public Rectangle move_to(double x, double y, double z) { datum = new Point(x, y, z); return this; }\r
184                 public Rectangle locate(Point p) { datum = p; return this; }\r
185                 public Rectangle locate(double x, double y, double z) { datum = new Point(x, y, z); return this; }\r
186 \r
187                 public Figure shift(Point p)\r
188                 {\r
189                         v1 += p;\r
190                         v2 += p;\r
191                         return this;\r
192                 }\r
193                 public Figure centering(Point p)\r
194                 {\r
195                         double h = width, v = height;\r
196                         v1.x = p.x - h / 2.0;\r
197                         v1.y = p.y - v / 2.0;\r
198                         v2.x = v1.x + h;\r
199                         v2.y = v1.y + v;\r
200                         return this;\r
201                 }\r
202 \r
203                 public void draw()\r
204                 {\r
205                         Main.drawable.rect(this);\r
206                 }\r
207                 public bool include(double x, double y)\r
208                 {\r
209                         return (top <= y) && (left <= x) && (bottom >= y) && (right >= x);\r
210                 }\r
211                 public bool include(Point p)\r
212                 {\r
213                         return (top <= p.y) && (left <= p.x) && (bottom >= p.y) && (right >= p.x);\r
214                 }\r
215                 public bool include(Rectangle rect)\r
216                 {\r
217                         return (top <= rect.top) && (left <= rect.left) && (bottom >= rect.bottom) && (right >= rect.right);\r
218                 }\r
219 \r
220                 public Rectangle alignLeft(double lef)\r
221                 {\r
222                         return move_to(lef, getTop(), datum.z);\r
223                 }\r
224                 public Rectangle alignTop(double to_)\r
225                 {\r
226                         return move_to(getLeft(), to_, datum.z);\r
227                 }\r
228                 public Rectangle alignRight(double rig)\r
229                 {\r
230                         return move_to(rig - getWidth(), getTop(), datum.z);\r
231                 }\r
232                 public Rectangle alignBottom(double bot)\r
233                 {\r
234                         return move_to(getLeft(), bot - getHeight(), datum.z);\r
235                 }\r
236 \r
237                 public void clipped_by(Rectangle source)\r
238                 {\r
239                         double t = top, l = left, b = bottom, r = right;\r
240                         if (top < source.top) { t = source.top; }\r
241                         if (left < source.left) { l = source.left; }\r
242                         if (bottom > source.bottom) { b = source.bottom; }\r
243                         if (right > source.right) { r = source.right; }\r
244                         set(l, t, r, b);\r
245                 }\r
246                 public void clip(Rectangle target)\r
247                 {\r
248                         double t = top, l = left, b = bottom, r = right;\r
249                         if (top < target.top) { t = target.top; }\r
250                         if (left < target.left) { l = target.left; }\r
251                         if (bottom > target.bottom) { b = target.bottom; }\r
252                         if (right > target.right) { r = target.right; }\r
253                         set(l, t, r, b);\r
254                 }\r
255 \r
256 \r
257                 public double left   { get { return v1.x; } }\r
258                 public double top    { get { return v1.y; } }\r
259                 public double right  { get { return v2.x; } }\r
260                 public double bottom { get { return v2.y; } }\r
261                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
262                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
263                 public Point center {\r
264                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
265                         set { centering(value); }\r
266                 }\r
267                 public double getLeft() { return left; }\r
268                 public double getTop() { return top; }\r
269                 public double getRight() { return right; }\r
270                 public double getBottom() { return bottom; }\r
271                 public double getWidth() { return width; }\r
272                 public double getHeight() { return height; }\r
273                 public Point getCenter() { return center; }\r
274 \r
275 \r
276 \r
277                 public Color fill { get; set; }\r
278                 public Stroke stroke { get; set; }\r
279 \r
280                 public override string ToString()\r
281                 {\r
282                         return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
283                 }\r
284         }\r
285 \r
286 \r
287         public partial class Ellipse : Shape\r
288         {\r
289                 public Point datum { get; set; }\r
290                 public double xdiameter, ydiameter;\r
291 \r
292                 public Ellipse()\r
293                 {\r
294                         set(0,0);\r
295                 }\r
296                 public Ellipse(double wid, double hei)\r
297                 {\r
298                         set(wid, hei);\r
299                 }\r
300 \r
301                 public Ellipse set(double wid, double hei)\r
302                 {\r
303                         xdiameter = wid;\r
304                         ydiameter = hei;\r
305                         return this;\r
306                 }\r
307                 public Figure shift(Point p)\r
308                 {\r
309                         datum += p;\r
310                         return this;\r
311                 }\r
312                 public Figure centering(Point p)\r
313                 {\r
314                         datum = p;\r
315                         return this;\r
316                 }\r
317 \r
318                 public void draw()\r
319                 {\r
320                         Main.drawable.ellipse(this);\r
321                 }\r
322 \r
323                 public double left { get { return datum.x - xdiameter/2.0; } }\r
324                 public double top { get { return datum.y - ydiameter / 2.0; } }\r
325                 public double right { get { return datum.x + xdiameter / 2.0; } }\r
326                 public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
327                 public double width { get { return Math.abs(xdiameter); } }\r
328                 public double height { get { return Math.abs(ydiameter); } }\r
329                 public Point center\r
330                 {\r
331                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
332                         set { centering(value); }\r
333                 }\r
334                 public double getLeft() { return left; }\r
335                 public double getTop() { return top; }\r
336                 public double getRight() { return right; }\r
337                 public double getBottom() { return bottom; }\r
338                 public double getWidth() { return width; }\r
339                 public double getHeight() { return height; }\r
340                 public Point getCenter() { return center; }\r
341 \r
342                 public Color fill { get; set; }\r
343                 public Stroke stroke { get; set; }\r
344         }\r
345 \r
346 \r
347         public partial class Polygon : Shape\r
348         {\r
349                 public Point datum { get; set; }\r
350                 public System.Collections.Generic.List<Point> vertices;\r
351 \r
352                 public Polygon()\r
353                 {\r
354                         vertices = new System.Collections.Generic.List<Point>();\r
355                 }\r
356                 public Polygon(double[] verts)\r
357                 {\r
358                         vertices = new System.Collections.Generic.List<Point>();\r
359                         for (int i=0; i < verts.Length; i+=2)\r
360                         {\r
361                                 vertices.Add(new Point(verts[i], verts[i+1]));\r
362                         }\r
363 \r
364                 }\r
365                 public Polygon(Point[] verts)\r
366                 {\r
367                         vertices = new System.Collections.Generic.List<Point>();\r
368                         foreach (Point p in verts)\r
369                         {\r
370                                 vertices.Add(p);\r
371                         }\r
372 \r
373                 }\r
374                 public Polygon append(Point p) { vertices.Add(p); return this; }\r
375                 public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
376                 public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
377 \r
378 \r
379                 public Figure shift(Point p)\r
380                 {\r
381                         datum = datum + p;\r
382                         return this;\r
383                 }\r
384                 public Figure centering(Point p)\r
385                 {\r
386                         datum = p;\r
387                         return this;\r
388                 }\r
389 \r
390                 public void draw()\r
391                 {\r
392                         Main.drawable.polygon(this);\r
393                 }\r
394 \r
395                 public Color fill { get; set; }\r
396                 public Stroke stroke { get; set; }\r
397         }\r
398 \r
399 }