OSDN Git Service

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