OSDN Git Service

456
[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 \r
161         public partial class Rectangle : Shape\r
162         {\r
163                 public Point v1, v2;\r
164 \r
165                 //public static Rectangle[] this[int x] { get { return Array(x); } }\r
166                 //public static Rectangle[,] this[int x, int y] { get { return Array(x, y); } }\r
167                 public static Rectangle[] Array(int ind)\r
168                 {\r
169                         Rectangle[] l = new Rectangle[ind];\r
170                         for (int i = 0; i < ind; i++) { l[i] = new Rectangle(); }\r
171                         return l;\r
172                 }\r
173                 public static Rectangle[,] Array(int indx, int indy)\r
174                 {\r
175                         Rectangle[,] l = new Rectangle[indx, indy];\r
176                         for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Rectangle(); } }\r
177                         return l;\r
178                 }\r
179 \r
180 \r
181                 public Rectangle()\r
182                 {\r
183                         fill = Color.white;\r
184                         stroke = Stroke.null_line;\r
185                         set(0,0);\r
186                 }\r
187                 public Rectangle(double wid, double hei)\r
188                 {\r
189                         fill = Color.white;\r
190                         stroke = Stroke.null_line;\r
191                         set(wid, hei);\r
192                 }\r
193                 public Rectangle(Rectangle another)\r
194                 {\r
195                         fill = Color.white;\r
196                         stroke = Stroke.null_line;\r
197                         v1 = another.v1;\r
198                         v2 = another.v2;\r
199                 }\r
200                 public Rectangle set(double wid, double hei)\r
201                 {\r
202                         v1.set(0, 0, 0);\r
203                         v2.set(wid, hei, 0);\r
204                         return this;\r
205                 }\r
206                 Rectangle set(Point po1, Point po2) {\r
207                         v1 = po1;\r
208                         v2 = po2;\r
209                         return this;\r
210                 }\r
211                 public Rectangle set(double l, double t, double r, double b)\r
212                 {\r
213                         v1.set(l, t, 0);\r
214                         v2.set(r, b, 0);\r
215                         return this;\r
216                 }\r
217                 public Rectangle set(Rectangle another)\r
218                 {\r
219                         v1 = another.v1;\r
220                         v2 = another.v2;\r
221                         return this;\r
222                 }\r
223 \r
224                 public Rectangle resize(double width, double height)\r
225                 {\r
226                         Point po = center;\r
227                         set(width, height);\r
228                         centering(po);\r
229                         return this;\r
230                 }\r
231 \r
232 \r
233                 public Point datum\r
234                 {\r
235                         get { return v1; }\r
236                         set { double w = width, h = height; v1 = value; v2 = v1 + new Point(w,h); }\r
237                 }\r
238                 public Rectangle move_to(Point p) { datum = p; return this; }\r
239                 public Rectangle move_to(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
240                 public Rectangle locate(Point p) { datum = p; return this; }\r
241                 public Rectangle locate(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
242 \r
243                 public Figure shift(Point p)\r
244                 {\r
245                         v1 += p;\r
246                         v2 += p;\r
247                         return this;\r
248                 }\r
249                 public Figure centering(Point p)\r
250                 {\r
251                         double h = width, v = height;\r
252                         v1.x = p.x - h / 2.0;\r
253                         v1.y = p.y - v / 2.0;\r
254                         v2.x = v1.x + h;\r
255                         v2.y = v1.y + v;\r
256                         return this;\r
257                 }\r
258 \r
259                 public virtual void draw()\r
260                 {\r
261                         Main.drawable.rect(this);\r
262                 }\r
263                 public bool include(double x, double y)\r
264                 {\r
265                         return (top <= y) && (left <= x) && (bottom >= y) && (right >= x);\r
266                 }\r
267                 public bool include(Point p)\r
268                 {\r
269                         return (top <= p.y) && (left <= p.x) && (bottom >= p.y) && (right >= p.x);\r
270                 }\r
271                 public bool include(Rectangle rect)\r
272                 {\r
273                         return (top <= rect.top) && (left <= rect.left) && (bottom >= rect.bottom) && (right >= rect.right);\r
274                 }\r
275 \r
276                 public Rectangle alignLeft(double lef)\r
277                 {\r
278                         return move_to(lef, getTop(), datum.z);\r
279                 }\r
280                 public Rectangle alignTop(double to_)\r
281                 {\r
282                         return move_to(getLeft(), to_, datum.z);\r
283                 }\r
284                 public Rectangle alignRight(double rig)\r
285                 {\r
286                         return move_to(rig - getWidth(), getTop(), datum.z);\r
287                 }\r
288                 public Rectangle alignBottom(double bot)\r
289                 {\r
290                         return move_to(getLeft(), bot - getHeight(), datum.z);\r
291                 }\r
292 \r
293                 public void clipped_by(Rectangle source)\r
294                 {\r
295                         double t = top, l = left, b = bottom, r = right;\r
296                         if (top < source.top) { t = source.top; }\r
297                         if (left < source.left) { l = source.left; }\r
298                         if (bottom > source.bottom) { b = source.bottom; }\r
299                         if (right > source.right) { r = source.right; }\r
300                         set(l, t, r, b);\r
301                 }\r
302                 public void clip(Rectangle target)\r
303                 {\r
304                         double t = top, l = left, b = bottom, r = right;\r
305                         if (top < target.top) { t = target.top; }\r
306                         if (left < target.left) { l = target.left; }\r
307                         if (bottom > target.bottom) { b = target.bottom; }\r
308                         if (right > target.right) { r = target.right; }\r
309                         set(l, t, r, b);\r
310                 }\r
311 \r
312 \r
313                 public double left   { get { return v1.x; } }\r
314                 public double top    { get { return v1.y; } }\r
315                 public double right  { get { return v2.x; } }\r
316                 public double bottom { get { return v2.y; } }\r
317                 public double width { get { return Math.abs(v1.x - v2.x); } }\r
318                 public double height { get { return Math.abs(v1.y - v2.y); } }\r
319                 public Point center {\r
320                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
321                         set { centering(value); }\r
322                 }\r
323                 public double getLeft() { return left; }\r
324                 public double getTop() { return top; }\r
325                 public double getRight() { return right; }\r
326                 public double getBottom() { return bottom; }\r
327                 public double getWidth() { return width; }\r
328                 public double getHeight() { return height; }\r
329                 public Point getCenter() { return center; }\r
330 \r
331 \r
332 \r
333                 public Color fill { get; set; }\r
334                 public Stroke stroke { get; set; }\r
335 \r
336                 public override string ToString()\r
337                 {\r
338                         return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
339                 }\r
340         }\r
341 \r
342 \r
343         public partial class Ellipse : Shape\r
344         {\r
345                 public Point datum { get; set; }\r
346                 public double xdiameter, ydiameter;\r
347 \r
348                 //public static Ellipse[] this[int x] { get { return Array(x); } }\r
349                 //public static Ellipse[,] this[int x, int y] { get { return Array(x, y); } }\r
350                 public static Ellipse[] Array(int ind)\r
351                 {\r
352                         Ellipse[] l = new Ellipse[ind];\r
353                         for (int i = 0; i < ind; i++) { l[i] = new Ellipse(); }\r
354                         return l;\r
355                 }\r
356                 public static Ellipse[,] Array(int indx, int indy)\r
357                 {\r
358                         Ellipse[,] l = new Ellipse[indx, indy];\r
359                         for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Ellipse(); } }\r
360                         return l;\r
361                 }\r
362 \r
363                 public Ellipse()\r
364                 {\r
365                         fill = Color.white;\r
366                         stroke = Stroke.null_line;\r
367                         set(0,0);\r
368                 }\r
369                 public Ellipse(double wid, double hei)\r
370                 {\r
371                         fill = Color.white;\r
372                         stroke = Stroke.null_line;\r
373                         set(wid, hei);\r
374                 }\r
375 \r
376                 public Ellipse set(double wid, double hei)\r
377                 {\r
378                         xdiameter = wid;\r
379                         ydiameter = hei;\r
380                         return this;\r
381                 }\r
382                 public Ellipse resize(double width, double height)\r
383                 {\r
384                         Point po = center;\r
385                         set(width, height);\r
386                         centering(po);\r
387                         return this;\r
388                 }\r
389                 public Figure shift(Point p)\r
390                 {\r
391                         datum += p;\r
392                         return this;\r
393                 }\r
394                 public Figure centering(Point p)\r
395                 {\r
396                         datum = p;\r
397                         return this;\r
398                 }\r
399 \r
400                 public virtual void draw()\r
401                 {\r
402                         Main.drawable.ellipse(this);\r
403                 }\r
404 \r
405                 public double left { get { return datum.x - xdiameter/2.0; } }\r
406                 public double top { get { return datum.y - ydiameter / 2.0; } }\r
407                 public double right { get { return datum.x + xdiameter / 2.0; } }\r
408                 public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
409                 public double width { get { return Math.abs(xdiameter); } }\r
410                 public double height { get { return Math.abs(ydiameter); } }\r
411                 public Point center\r
412                 {\r
413                         get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
414                         set { centering(value); }\r
415                 }\r
416                 public double getLeft() { return left; }\r
417                 public double getTop() { return top; }\r
418                 public double getRight() { return right; }\r
419                 public double getBottom() { return bottom; }\r
420                 public double getWidth() { return width; }\r
421                 public double getHeight() { return height; }\r
422                 public Point getCenter() { return center; }\r
423 \r
424                 public Color fill { get; set; }\r
425                 public Stroke stroke { get; set; }\r
426         }\r
427 \r
428 \r
429         public partial class Polygon : Shape\r
430         {\r
431                 public Point datum { get; set; }\r
432                 public System.Collections.Generic.List<Point> vertices;\r
433 \r
434                 public Polygon()\r
435                 {\r
436                         fill = Color.white;\r
437                         stroke = Stroke.null_line;\r
438                         vertices = new System.Collections.Generic.List<Point>();\r
439                 }\r
440                 public Polygon(double[] verts)\r
441                 {\r
442                         fill = Color.white;\r
443                         stroke = Stroke.null_line;\r
444                         vertices = new System.Collections.Generic.List<Point>();\r
445                         for (int i=0; i < verts.Length; i+=2)\r
446                         {\r
447                                 vertices.Add(new Point(verts[i], verts[i+1]));\r
448                         }\r
449 \r
450                 }\r
451                 public Polygon(Point[] verts)\r
452                 {\r
453                         fill = Color.white;\r
454                         stroke = Stroke.null_line;\r
455                         vertices = new System.Collections.Generic.List<Point>();\r
456                         foreach (Point p in verts)\r
457                         {\r
458                                 vertices.Add(p);\r
459                         }\r
460 \r
461                 }\r
462                 public Polygon append(Point p) { vertices.Add(p); return this; }\r
463                 public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
464                 public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
465 \r
466 \r
467                 public Figure shift(Point p)\r
468                 {\r
469                         datum = datum + p;\r
470                         return this;\r
471                 }\r
472                 public Figure centering(Point p)\r
473                 {\r
474                         datum = p;\r
475                         return this;\r
476                 }\r
477 \r
478                 public virtual void draw()\r
479                 {\r
480                         Main.drawable.polygon(this);\r
481                 }\r
482 \r
483                 public Color fill { get; set; }\r
484                 public Stroke stroke { get; set; }\r
485         }\r
486 \r
487 }