OSDN Git Service

5
[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                 public static readonly Stroke null_line = new Stroke(Color.null_color, 0);\r
63                 public static readonly Stroke hair_line = new Stroke(Color.white, 1);\r
64         }\r
65 \r
66         public partial class Line : Shape\r
67         {\r
68                 public Point begin, end;\r
69                 public Point datum\r
70                 {\r
71                         get { return begin; }\r
72                         set { begin = value; }\r
73                 }\r
74 \r
75                 //public static Line[] this[int x] { get { return Array(x); } }\r
76                 //public static Line[,] this[int x, int y] { get { return Array(x, y); } }\r
77                 public static Line[] Array(int ind)\r
78                 {\r
79                         Line[] l = new Line[ind];\r
80                         for(int i=0; i<ind; i++) { l[i] = new Line(); }\r
81                         return l;\r
82                 }\r
83                 public static Line[,] Array(int indx, int indy)\r
84                 {\r
85                         Line[,] l = new Line[indx, indy];\r
86                         for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Line(); } }\r
87                         return l;\r
88                 }\r
89 \r
90 \r
91                 public Line()\r
92                 {\r
93                         fill = Color.white;\r
94                         stroke = Stroke.hair_line;\r
95                         set(0,0,0,0);\r
96                 }\r
97                 public Line(double x1, double y1, double x2, double y2)\r
98                 {\r
99                         fill = Color.white;\r
100                         stroke = Stroke.hair_line;\r
101                         set(x1, y1, x2, y2);\r
102                 }\r
103                 public Line(Point v1, Point v2)\r
104                 {\r
105                         fill = Color.white;\r
106                         stroke = Stroke.hair_line;\r
107                         set(v1, v2);\r
108                 }\r
109                 public Line set(double x1, double y1, double x2, double y2)\r
110                 {\r
111                         begin.set(x1, y1);\r
112                         end.set(x2, y2);\r
113                         return this;\r
114                 }\r
115                 public Line set(Point v1, Point v2)\r
116                 {\r
117                         begin = v1;\r
118                         end   = v2;\r
119                         return this;\r
120                 }\r
121 \r
122                 public Figure shift(Point p)\r
123                 {\r
124                         begin += p;\r
125                         end   += 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                         begin.x = p.x - h / 2.0;\r
132                         begin.y = p.y - v / 2.0;\r
133                         end.x = begin.x + h;\r
134                         end.y = begin.y + v;\r
135                         return this;\r
136                 }\r
137 \r
138                 public virtual void draw()\r
139                 {\r
140                         Main.drawable.line(this);\r
141                 }\r
142 \r
143                 public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
144                 public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
145                 public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
146                 public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
147                 public double width { get { return Math.abs(begin.x - end.x); } }\r
148                 public double height { get { return Math.abs(begin.y - end.y); } }\r
149                 public double getLeft() { return left; }\r
150                 public double getTop() { return top; }\r
151                 public double getRight() { return right; }\r
152                 public double getBottom() { return bottom; }\r
153                 public double getWidth() { return width; }\r
154                 public double getHeight() { return height; }\r
155 \r
156                 public Color fill { get; set; }\r
157                 public Stroke stroke { get; set; }\r
158         }\r
159 \r
160         public partial class Rectangle_\r
161         {\r
162                 public Point v1, v2;\r
163 \r
164                 public double left { get { return v1.x; } }\r
165                 public double top { get { return v1.y; } }\r
166                 public double right { get { return v2.x; } }\r
167                 public double bottom { get { return v2.y; } }\r
168                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
169                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
170                 public double getLeft() { return left; }\r
171                 public double getTop() { return top; }\r
172                 public double getRight() { return right; }\r
173                 public double getBottom() { return bottom; }\r
174                 public double getWidth() { return width; }\r
175                 public double getHeight() { return height; }\r
176 \r
177 \r
178 \r
179                 public Color fill { get; set; }\r
180                 public Stroke stroke { get; set; }\r
181 \r
182                 public override string ToString()\r
183                 {\r
184                         return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
185                 }\r
186         }\r
187 \r
188 \r
189         public partial class Rectangle : Shape\r
190         {\r
191                 public Point v1, v2;\r
192 \r
193                 //public static Rectangle[] this[int x] { get { return Array(x); } }\r
194                 //public static Rectangle[,] this[int x, int y] { get { return Array(x, y); } }\r
195                 public static Rectangle[] Array(int ind)\r
196                 {\r
197                         Rectangle[] l = new Rectangle[ind];\r
198                         for (int i = 0; i < ind; i++) { l[i] = new Rectangle(); }\r
199                         return l;\r
200                 }\r
201                 public static Rectangle[,] Array(int indx, int indy)\r
202                 {\r
203                         Rectangle[,] l = new Rectangle[indx, indy];\r
204                         for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Rectangle(); } }\r
205                         return l;\r
206                 }\r
207 \r
208 \r
209                 public Rectangle()\r
210                 {\r
211                         fill = Color.white;\r
212                         stroke = Stroke.null_line;\r
213                         set(0,0);\r
214                 }\r
215                 public Rectangle(double wid, double hei)\r
216                 {\r
217                         fill = Color.white;\r
218                         stroke = Stroke.null_line;\r
219                         set(wid, hei);\r
220                 }\r
221                 public Rectangle(Rectangle another)\r
222                 {\r
223                         fill = Color.white;\r
224                         stroke = Stroke.null_line;\r
225                         v1 = another.v1;\r
226                         v2 = another.v2;\r
227                 }\r
228                 public Rectangle set(double wid, double hei)\r
229                 {\r
230                         v1.set(0, 0, 0);\r
231                         v2.set(wid, hei, 0);\r
232                         return this;\r
233                 }\r
234                 Rectangle set(Point po1, Point po2) {\r
235                         v1 = po1;\r
236                         v2 = po2;\r
237                         return this;\r
238                 }\r
239                 public Rectangle set(double l, double t, double r, double b)\r
240                 {\r
241                         v1.set(l, t, 0);\r
242                         v2.set(r, b, 0);\r
243                         return this;\r
244                 }\r
245                 public Rectangle set(Rectangle another)\r
246                 {\r
247                         v1 = another.v1;\r
248                         v2 = another.v2;\r
249                         return this;\r
250                 }\r
251 \r
252                 public Rectangle resize(double width, double height)\r
253                 {\r
254                         Point po = center;\r
255                         set(width, height);\r
256                         centering(po);\r
257                         return this;\r
258                 }\r
259 \r
260 \r
261                 public Point datum\r
262                 {\r
263                         get { return v1; }\r
264                         set { double w = width, h = height; v1 = value; v2 = v1 + new Point(w,h); }\r
265                 }\r
266                 public Rectangle move_to(Point p) { datum = p; return this; }\r
267                 public Rectangle move_to(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
268                 public Rectangle locate(Point p) { datum = p; return this; }\r
269                 public Rectangle locate(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
270 \r
271                 public Figure shift(Point p)\r
272                 {\r
273                         v1 += p;\r
274                         v2 += p;\r
275                         return this;\r
276                 }\r
277                 public Figure centering(Point p)\r
278                 {\r
279                         double h = width, v = height;\r
280                         v1.x = p.x - h / 2.0;\r
281                         v1.y = p.y - v / 2.0;\r
282                         v2.x = v1.x + h;\r
283                         v2.y = v1.y + v;\r
284                         return this;\r
285                 }\r
286 \r
287                 public virtual void draw()\r
288                 {\r
289                         Main.drawable.rect(this);\r
290                 }\r
291                 public bool include(double x, double y)\r
292                 {\r
293                         return (top <= y) && (left <= x) && (bottom >= y) && (right >= x);\r
294                 }\r
295                 public bool include(Point p)\r
296                 {\r
297                         return (top <= p.y) && (left <= p.x) && (bottom >= p.y) && (right >= p.x);\r
298                 }\r
299                 public bool include(Rectangle rect)\r
300                 {\r
301                         return (top <= rect.top) && (left <= rect.left) && (bottom >= rect.bottom) && (right >= rect.right);\r
302                 }\r
303 \r
304                 public Rectangle alignLeft(double lef)\r
305                 {\r
306                         return move_to(lef, getTop(), datum.z);\r
307                 }\r
308                 public Rectangle alignTop(double to_)\r
309                 {\r
310                         return move_to(getLeft(), to_, datum.z);\r
311                 }\r
312                 public Rectangle alignRight(double rig)\r
313                 {\r
314                         return move_to(rig - getWidth(), getTop(), datum.z);\r
315                 }\r
316                 public Rectangle alignBottom(double bot)\r
317                 {\r
318                         return move_to(getLeft(), bot - getHeight(), datum.z);\r
319                 }\r
320 \r
321                 public void clipped_by(Rectangle source)\r
322                 {\r
323                         double t = top, l = left, b = bottom, r = right;\r
324                         if (top < source.top) { t = source.top; }\r
325                         if (left < source.left) { l = source.left; }\r
326                         if (bottom > source.bottom) { b = source.bottom; }\r
327                         if (right > source.right) { r = source.right; }\r
328                         set(l, t, r, b);\r
329                 }\r
330                 public void clip(Rectangle target)\r
331                 {\r
332                         double t = top, l = left, b = bottom, r = right;\r
333                         if (top < target.top) { t = target.top; }\r
334                         if (left < target.left) { l = target.left; }\r
335                         if (bottom > target.bottom) { b = target.bottom; }\r
336                         if (right > target.right) { r = target.right; }\r
337                         set(l, t, r, b);\r
338                 }\r
339 \r
340 \r
341                 public double left   { get { return v1.x; } }\r
342                 public double top    { get { return v1.y; } }\r
343                 public double right  { get { return v2.x; } }\r
344                 public double bottom { get { return v2.y; } }\r
345                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
346                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
347                 public Point center {\r
348                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
349                         set { centering(value); }\r
350                 }\r
351                 public double getLeft() { return left; }\r
352                 public double getTop() { return top; }\r
353                 public double getRight() { return right; }\r
354                 public double getBottom() { return bottom; }\r
355                 public double getWidth() { return width; }\r
356                 public double getHeight() { return height; }\r
357                 public Point getCenter() { return center; }\r
358 \r
359 \r
360 \r
361                 public Color fill { get; set; }\r
362                 public Stroke stroke { get; set; }\r
363 \r
364                 public override string ToString()\r
365                 {\r
366                         return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
367                 }\r
368         }\r
369 \r
370 \r
371         public partial class Ellipse : Shape\r
372         {\r
373                 public Point datum { get; set; }\r
374                 public double xdiameter, ydiameter;\r
375 \r
376                 //public static Ellipse[] this[int x] { get { return Array(x); } }\r
377                 //public static Ellipse[,] this[int x, int y] { get { return Array(x, y); } }\r
378                 public static Ellipse[] Array(int ind)\r
379                 {\r
380                         Ellipse[] l = new Ellipse[ind];\r
381                         for (int i = 0; i < ind; i++) { l[i] = new Ellipse(); }\r
382                         return l;\r
383                 }\r
384                 public static Ellipse[,] Array(int indx, int indy)\r
385                 {\r
386                         Ellipse[,] l = new Ellipse[indx, indy];\r
387                         for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Ellipse(); } }\r
388                         return l;\r
389                 }\r
390 \r
391                 public Ellipse()\r
392                 {\r
393                         fill = Color.white;\r
394                         stroke = Stroke.null_line;\r
395                         set(0,0);\r
396                 }\r
397                 public Ellipse(double wid, double hei)\r
398                 {\r
399                         fill = Color.white;\r
400                         stroke = Stroke.null_line;\r
401                         set(wid, hei);\r
402                 }\r
403 \r
404                 public Ellipse set(double wid, double hei)\r
405                 {\r
406                         xdiameter = wid;\r
407                         ydiameter = hei;\r
408                         return this;\r
409                 }\r
410                 public Ellipse resize(double width, double height)\r
411                 {\r
412                         Point po = center;\r
413                         set(width, height);\r
414                         centering(po);\r
415                         return this;\r
416                 }\r
417                 public Figure shift(Point p)\r
418                 {\r
419                         datum += p;\r
420                         return this;\r
421                 }\r
422                 public Figure centering(Point p)\r
423                 {\r
424                         datum = p;\r
425                         return this;\r
426                 }\r
427 \r
428                 public virtual void draw()\r
429                 {\r
430                         Main.drawable.ellipse(this);\r
431                 }\r
432 \r
433                 public double left { get { return datum.x - xdiameter/2.0; } }\r
434                 public double top { get { return datum.y - ydiameter / 2.0; } }\r
435                 public double right { get { return datum.x + xdiameter / 2.0; } }\r
436                 public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
437                 public double width { get { return Math.abs(xdiameter); } }\r
438                 public double height { get { return Math.abs(ydiameter); } }\r
439                 public Point center\r
440                 {\r
441                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
442                         set { centering(value); }\r
443                 }\r
444                 public double getLeft() { return left; }\r
445                 public double getTop() { return top; }\r
446                 public double getRight() { return right; }\r
447                 public double getBottom() { return bottom; }\r
448                 public double getWidth() { return width; }\r
449                 public double getHeight() { return height; }\r
450                 public Point getCenter() { return center; }\r
451 \r
452                 public Color fill { get; set; }\r
453                 public Stroke stroke { get; set; }\r
454         }\r
455 \r
456 \r
457         public partial class Polygon : Shape\r
458         {\r
459                 public Point datum { get; set; }\r
460                 public System.Collections.Generic.List<Point> vertices;\r
461 \r
462                 public Polygon()\r
463                 {\r
464                         fill = Color.white;\r
465                         stroke = Stroke.null_line;\r
466                         vertices = new System.Collections.Generic.List<Point>();\r
467                 }\r
468                 public Polygon(double[] verts)\r
469                 {\r
470                         fill = Color.white;\r
471                         stroke = Stroke.null_line;\r
472                         vertices = new System.Collections.Generic.List<Point>();\r
473                         for (int i=0; i < verts.Length; i+=2)\r
474                         {\r
475                                 vertices.Add(new Point(verts[i], verts[i+1]));\r
476                         }\r
477 \r
478                 }\r
479                 public Polygon(Point[] verts)\r
480                 {\r
481                         fill = Color.white;\r
482                         stroke = Stroke.null_line;\r
483                         vertices = new System.Collections.Generic.List<Point>();\r
484                         foreach (Point p in verts)\r
485                         {\r
486                                 vertices.Add(p);\r
487                         }\r
488 \r
489                 }\r
490                 public Polygon append(Point p) { vertices.Add(p); return this; }\r
491                 public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
492                 public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
493 \r
494 \r
495                 public Figure shift(Point p)\r
496                 {\r
497                         datum = datum + p;\r
498                         return this;\r
499                 }\r
500                 public Figure centering(Point p)\r
501                 {\r
502                         datum = p;\r
503                         return this;\r
504                 }\r
505 \r
506                 public virtual void draw()\r
507                 {\r
508                         Main.drawable.polygon(this);\r
509                 }\r
510 \r
511                 public Color fill { get; set; }\r
512                 public Stroke stroke { get; set; }\r
513         }\r
514 \r
515 }