OSDN Git Service

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