OSDN Git Service

5
[psychlops/silverlight.git] / dev4 / psychlops / core / graphic / shape.cs
diff --git a/dev4/psychlops/core/graphic/shape.cs b/dev4/psychlops/core/graphic/shape.cs
new file mode 100644 (file)
index 0000000..9c7ed2b
--- /dev/null
@@ -0,0 +1,515 @@
+using System;\r
+using System.Windows;\r
+using System.Windows.Controls;\r
+using System.Windows.Documents;\r
+using System.Windows.Input;\r
+using System.Windows.Media;\r
+using System.Windows.Media.Animation;\r
+using System.Windows.Media.Imaging;\r
+using System.Windows.Shapes;\r
+\r
+\r
+\r
+namespace Psychlops{\r
+\r
+       public interface Shape : Internal.PrimitiveFigure\r
+       {\r
+               Color fill { get; set; }\r
+               Stroke stroke { get; set; }\r
+       }\r
+       public static class ShapeExtention\r
+       {\r
+               public static void draw(this Shape drawee, double c)\r
+               {\r
+                       drawee.draw( new Color(c) );\r
+               }\r
+               public static void draw(this Shape drawee, Color c)\r
+               {\r
+                       Color tmp_col = drawee.fill;\r
+                       Stroke tmp_strk = drawee.stroke;\r
+                       drawee.fill = c;\r
+                       drawee.stroke = new Stroke();\r
+                       drawee.draw();\r
+                       drawee.fill = tmp_col;\r
+                       drawee.stroke = tmp_strk;\r
+               }\r
+               public static void draw(this Shape drawee, Stroke strk)\r
+               {\r
+                       Color tmp_col = drawee.fill;\r
+                       Stroke tmp_strk = drawee.stroke;\r
+                       drawee.fill = new Color(0,0,1,1);\r
+                       drawee.stroke = strk;\r
+                       drawee.draw();\r
+                       drawee.fill = tmp_col;\r
+                       drawee.stroke = tmp_strk;\r
+               }\r
+       }\r
+\r
+       public partial struct Stroke\r
+       {\r
+               public double thick;\r
+               public Color color;\r
+               public Stroke(Color c, double t)\r
+               {\r
+                       color = c;\r
+                       thick = t;\r
+               }\r
+               public void set(Color c, double t)\r
+               {\r
+                       color = c;\r
+                       thick = t;\r
+               }\r
+               public static readonly Stroke null_line = new Stroke(Color.null_color, 0);\r
+               public static readonly Stroke hair_line = new Stroke(Color.white, 1);\r
+       }\r
+\r
+       public partial class Line : Shape\r
+       {\r
+               public Point begin, end;\r
+               public Point datum\r
+               {\r
+                       get { return begin; }\r
+                       set { begin = value; }\r
+               }\r
+\r
+               //public static Line[] this[int x] { get { return Array(x); } }\r
+               //public static Line[,] this[int x, int y] { get { return Array(x, y); } }\r
+               public static Line[] Array(int ind)\r
+               {\r
+                       Line[] l = new Line[ind];\r
+                       for(int i=0; i<ind; i++) { l[i] = new Line(); }\r
+                       return l;\r
+               }\r
+               public static Line[,] Array(int indx, int indy)\r
+               {\r
+                       Line[,] l = new Line[indx, indy];\r
+                       for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Line(); } }\r
+                       return l;\r
+               }\r
+\r
+\r
+               public Line()\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.hair_line;\r
+                       set(0,0,0,0);\r
+               }\r
+               public Line(double x1, double y1, double x2, double y2)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.hair_line;\r
+                       set(x1, y1, x2, y2);\r
+               }\r
+               public Line(Point v1, Point v2)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.hair_line;\r
+                       set(v1, v2);\r
+               }\r
+               public Line set(double x1, double y1, double x2, double y2)\r
+               {\r
+                       begin.set(x1, y1);\r
+                       end.set(x2, y2);\r
+                       return this;\r
+               }\r
+               public Line set(Point v1, Point v2)\r
+               {\r
+                       begin = v1;\r
+                       end   = v2;\r
+                       return this;\r
+               }\r
+\r
+               public Figure shift(Point p)\r
+               {\r
+                       begin += p;\r
+                       end   += p;\r
+                       return this;\r
+               }\r
+               public Figure centering(Point p)\r
+               {\r
+                       double h = width, v = height;\r
+                       begin.x = p.x - h / 2.0;\r
+                       begin.y = p.y - v / 2.0;\r
+                       end.x = begin.x + h;\r
+                       end.y = begin.y + v;\r
+                       return this;\r
+               }\r
+\r
+               public virtual void draw()\r
+               {\r
+                       Main.drawable.line(this);\r
+               }\r
+\r
+               public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
+               public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
+               public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
+               public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
+               public double width { get { return Math.abs(begin.x - end.x); } }\r
+               public double height { get { return Math.abs(begin.y - end.y); } }\r
+               public double getLeft() { return left; }\r
+               public double getTop() { return top; }\r
+               public double getRight() { return right; }\r
+               public double getBottom() { return bottom; }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+       }\r
+\r
+       public partial class Rectangle_\r
+       {\r
+               public Point v1, v2;\r
+\r
+               public double left { get { return v1.x; } }\r
+               public double top { get { return v1.y; } }\r
+               public double right { get { return v2.x; } }\r
+               public double bottom { get { return v2.y; } }\r
+               public double width { get { return Math.abs(v1.x - v2.x); } }\r
+               public double height { get { return Math.abs(v1.y - v2.y); } }\r
+               public double getLeft() { return left; }\r
+               public double getTop() { return top; }\r
+               public double getRight() { return right; }\r
+               public double getBottom() { return bottom; }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+\r
+\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+\r
+               public override string ToString()\r
+               {\r
+                       return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
+               }\r
+       }\r
+\r
+\r
+       public partial class Rectangle : Shape\r
+       {\r
+               public Point v1, v2;\r
+\r
+               //public static Rectangle[] this[int x] { get { return Array(x); } }\r
+               //public static Rectangle[,] this[int x, int y] { get { return Array(x, y); } }\r
+               public static Rectangle[] Array(int ind)\r
+               {\r
+                       Rectangle[] l = new Rectangle[ind];\r
+                       for (int i = 0; i < ind; i++) { l[i] = new Rectangle(); }\r
+                       return l;\r
+               }\r
+               public static Rectangle[,] Array(int indx, int indy)\r
+               {\r
+                       Rectangle[,] l = new Rectangle[indx, indy];\r
+                       for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Rectangle(); } }\r
+                       return l;\r
+               }\r
+\r
+\r
+               public Rectangle()\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       set(0,0);\r
+               }\r
+               public Rectangle(double wid, double hei)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       set(wid, hei);\r
+               }\r
+               public Rectangle(Rectangle another)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       v1 = another.v1;\r
+                       v2 = another.v2;\r
+               }\r
+               public Rectangle set(double wid, double hei)\r
+               {\r
+                       v1.set(0, 0, 0);\r
+                       v2.set(wid, hei, 0);\r
+                       return this;\r
+               }\r
+               Rectangle set(Point po1, Point po2) {\r
+                       v1 = po1;\r
+                       v2 = po2;\r
+                       return this;\r
+               }\r
+               public Rectangle set(double l, double t, double r, double b)\r
+               {\r
+                       v1.set(l, t, 0);\r
+                       v2.set(r, b, 0);\r
+                       return this;\r
+               }\r
+               public Rectangle set(Rectangle another)\r
+               {\r
+                       v1 = another.v1;\r
+                       v2 = another.v2;\r
+                       return this;\r
+               }\r
+\r
+               public Rectangle resize(double width, double height)\r
+               {\r
+                       Point po = center;\r
+                       set(width, height);\r
+                       centering(po);\r
+                       return this;\r
+               }\r
+\r
+\r
+               public Point datum\r
+               {\r
+                       get { return v1; }\r
+                       set { double w = width, h = height; v1 = value; v2 = v1 + new Point(w,h); }\r
+               }\r
+               public Rectangle move_to(Point p) { datum = p; return this; }\r
+               public Rectangle move_to(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
+               public Rectangle locate(Point p) { datum = p; return this; }\r
+               public Rectangle locate(double x, double y, double z = 0.0) { datum = new Point(x, y, z); return this; }\r
+\r
+               public Figure shift(Point p)\r
+               {\r
+                       v1 += p;\r
+                       v2 += p;\r
+                       return this;\r
+               }\r
+               public Figure centering(Point p)\r
+               {\r
+                       double h = width, v = height;\r
+                       v1.x = p.x - h / 2.0;\r
+                       v1.y = p.y - v / 2.0;\r
+                       v2.x = v1.x + h;\r
+                       v2.y = v1.y + v;\r
+                       return this;\r
+               }\r
+\r
+               public virtual void draw()\r
+               {\r
+                       Main.drawable.rect(this);\r
+               }\r
+               public bool include(double x, double y)\r
+               {\r
+                       return (top <= y) && (left <= x) && (bottom >= y) && (right >= x);\r
+               }\r
+               public bool include(Point p)\r
+               {\r
+                       return (top <= p.y) && (left <= p.x) && (bottom >= p.y) && (right >= p.x);\r
+               }\r
+               public bool include(Rectangle rect)\r
+               {\r
+                       return (top <= rect.top) && (left <= rect.left) && (bottom >= rect.bottom) && (right >= rect.right);\r
+               }\r
+\r
+               public Rectangle alignLeft(double lef)\r
+               {\r
+                       return move_to(lef, getTop(), datum.z);\r
+               }\r
+               public Rectangle alignTop(double to_)\r
+               {\r
+                       return move_to(getLeft(), to_, datum.z);\r
+               }\r
+               public Rectangle alignRight(double rig)\r
+               {\r
+                       return move_to(rig - getWidth(), getTop(), datum.z);\r
+               }\r
+               public Rectangle alignBottom(double bot)\r
+               {\r
+                       return move_to(getLeft(), bot - getHeight(), datum.z);\r
+               }\r
+\r
+               public void clipped_by(Rectangle source)\r
+               {\r
+                       double t = top, l = left, b = bottom, r = right;\r
+                       if (top < source.top) { t = source.top; }\r
+                       if (left < source.left) { l = source.left; }\r
+                       if (bottom > source.bottom) { b = source.bottom; }\r
+                       if (right > source.right) { r = source.right; }\r
+                       set(l, t, r, b);\r
+               }\r
+               public void clip(Rectangle target)\r
+               {\r
+                       double t = top, l = left, b = bottom, r = right;\r
+                       if (top < target.top) { t = target.top; }\r
+                       if (left < target.left) { l = target.left; }\r
+                       if (bottom > target.bottom) { b = target.bottom; }\r
+                       if (right > target.right) { r = target.right; }\r
+                       set(l, t, r, b);\r
+               }\r
+\r
+\r
+               public double left   { get { return v1.x; } }\r
+               public double top    { get { return v1.y; } }\r
+               public double right  { get { return v2.x; } }\r
+               public double bottom { get { return v2.y; } }\r
+               public double width { get { return Math.abs(v1.x - v2.x); } }\r
+               public double height { get { return Math.abs(v1.y - v2.y); } }\r
+               public Point center {\r
+                       get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
+                       set { centering(value); }\r
+               }\r
+               public double getLeft() { return left; }\r
+               public double getTop() { return top; }\r
+               public double getRight() { return right; }\r
+               public double getBottom() { return bottom; }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+               public Point getCenter() { return center; }\r
+\r
+\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+\r
+               public override string ToString()\r
+               {\r
+                       return "Left:" + left.ToString() + " Top:" + top.ToString() + " Right:" + right.ToString() + " Bottom:" + bottom.ToString();\r
+               }\r
+       }\r
+\r
+\r
+       public partial class Ellipse : Shape\r
+       {\r
+               public Point datum { get; set; }\r
+               public double xdiameter, ydiameter;\r
+\r
+               //public static Ellipse[] this[int x] { get { return Array(x); } }\r
+               //public static Ellipse[,] this[int x, int y] { get { return Array(x, y); } }\r
+               public static Ellipse[] Array(int ind)\r
+               {\r
+                       Ellipse[] l = new Ellipse[ind];\r
+                       for (int i = 0; i < ind; i++) { l[i] = new Ellipse(); }\r
+                       return l;\r
+               }\r
+               public static Ellipse[,] Array(int indx, int indy)\r
+               {\r
+                       Ellipse[,] l = new Ellipse[indx, indy];\r
+                       for (int i = 0; i < indx; i++) { for (int j = 0; j < indy; j++) { l[i, j] = new Ellipse(); } }\r
+                       return l;\r
+               }\r
+\r
+               public Ellipse()\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       set(0,0);\r
+               }\r
+               public Ellipse(double wid, double hei)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       set(wid, hei);\r
+               }\r
+\r
+               public Ellipse set(double wid, double hei)\r
+               {\r
+                       xdiameter = wid;\r
+                       ydiameter = hei;\r
+                       return this;\r
+               }\r
+               public Ellipse resize(double width, double height)\r
+               {\r
+                       Point po = center;\r
+                       set(width, height);\r
+                       centering(po);\r
+                       return this;\r
+               }\r
+               public Figure shift(Point p)\r
+               {\r
+                       datum += p;\r
+                       return this;\r
+               }\r
+               public Figure centering(Point p)\r
+               {\r
+                       datum = p;\r
+                       return this;\r
+               }\r
+\r
+               public virtual void draw()\r
+               {\r
+                       Main.drawable.ellipse(this);\r
+               }\r
+\r
+               public double left { get { return datum.x - xdiameter/2.0; } }\r
+               public double top { get { return datum.y - ydiameter / 2.0; } }\r
+               public double right { get { return datum.x + xdiameter / 2.0; } }\r
+               public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
+               public double width { get { return Math.abs(xdiameter); } }\r
+               public double height { get { return Math.abs(ydiameter); } }\r
+               public Point center\r
+               {\r
+                       get { return new Point((left + right) / 2, (top + bottom) / 2); }\r
+                       set { centering(value); }\r
+               }\r
+               public double getLeft() { return left; }\r
+               public double getTop() { return top; }\r
+               public double getRight() { return right; }\r
+               public double getBottom() { return bottom; }\r
+               public double getWidth() { return width; }\r
+               public double getHeight() { return height; }\r
+               public Point getCenter() { return center; }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+       }\r
+\r
+\r
+       public partial class Polygon : Shape\r
+       {\r
+               public Point datum { get; set; }\r
+               public System.Collections.Generic.List<Point> vertices;\r
+\r
+               public Polygon()\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       vertices = new System.Collections.Generic.List<Point>();\r
+               }\r
+               public Polygon(double[] verts)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       vertices = new System.Collections.Generic.List<Point>();\r
+                       for (int i=0; i < verts.Length; i+=2)\r
+                       {\r
+                               vertices.Add(new Point(verts[i], verts[i+1]));\r
+                       }\r
+\r
+               }\r
+               public Polygon(Point[] verts)\r
+               {\r
+                       fill = Color.white;\r
+                       stroke = Stroke.null_line;\r
+                       vertices = new System.Collections.Generic.List<Point>();\r
+                       foreach (Point p in verts)\r
+                       {\r
+                               vertices.Add(p);\r
+                       }\r
+\r
+               }\r
+               public Polygon append(Point p) { vertices.Add(p); return this; }\r
+               public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
+               public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
+\r
+\r
+               public Figure shift(Point p)\r
+               {\r
+                       datum = datum + p;\r
+                       return this;\r
+               }\r
+               public Figure centering(Point p)\r
+               {\r
+                       datum = p;\r
+                       return this;\r
+               }\r
+\r
+               public virtual void draw()\r
+               {\r
+                       Main.drawable.polygon(this);\r
+               }\r
+\r
+               public Color fill { get; set; }\r
+               public Stroke stroke { get; set; }\r
+       }\r
+\r
+}
\ No newline at end of file