OSDN Git Service

Pointクラスを一般化
authorkimikage <kimikage_ceo@hotmail.com>
Wed, 18 Apr 2012 07:24:20 +0000 (16:24 +0900)
committerkimikage <kimikage_ceo@hotmail.com>
Wed, 18 Apr 2012 07:24:20 +0000 (16:24 +0900)
Karinto/Point.cs

index f8e059b..e28120f 100755 (executable)
@@ -15,9 +15,9 @@ namespace Karinto
     /// <summary>\r
     ///     2次元座標を示す\r
     /// </summary>\r
-    public class Point\r
+    public class Point : Point<double>\r
     {\r
-        #region type definition\r
+        #region type definitions\r
 \r
         private class XComparerClass : IComparer<Point>\r
         {\r
@@ -50,15 +50,13 @@ namespace Karinto
         }\r
 \r
         public Point(double x, double y)\r
+            : base(x, y)\r
         {\r
-            X = x;\r
-            Y = y;\r
         }\r
 \r
         public Point(Point point)\r
+            : base(point)\r
         {\r
-            X = point.X;\r
-            Y = point.Y;\r
         }\r
         #endregion\r
 \r
@@ -99,18 +97,6 @@ namespace Karinto
 \r
         #region properties\r
 \r
-        public double X\r
-        {\r
-            get;\r
-            set;\r
-        }\r
-\r
-        public double Y\r
-        {\r
-            get;\r
-            set;\r
-        }\r
-\r
         /// <summary>\r
         ///     原点からの距離\r
         /// </summary>\r
@@ -144,7 +130,7 @@ namespace Karinto
             }\r
         }\r
 \r
-        public static IComparer<Point> XComparer\r
+        public new static IComparer<Point> XComparer\r
         {\r
             get\r
             {\r
@@ -152,7 +138,7 @@ namespace Karinto
             }\r
         }\r
 \r
-        public static IComparer<Point> YComparer\r
+        public new static IComparer<Point> YComparer\r
         {\r
             get\r
             {\r
@@ -163,15 +149,6 @@ namespace Karinto
         #endregion\r
 \r
         #region public methods\r
-        /// <summary>\r
-        ///     XとYを入れ換える\r
-        /// </summary>\r
-        public void SwapXY()\r
-        {\r
-            double x = X;\r
-            X = Y;\r
-            Y = x;\r
-        }\r
 \r
         /// <summary>\r
         ///     加算(ベクトル合成) \r
@@ -212,4 +189,167 @@ namespace Karinto
         #endregion\r
     }\r
 \r
+    /// <summary>\r
+    ///     2次元座標を示す\r
+    /// </summary>\r
+    public class Point<T>\r
+        where T : IComparable, new()\r
+    {\r
+        #region type definitions\r
+\r
+        private class XComparerClass : IComparer<Point<T>>\r
+        {\r
+            public int Compare(Point<T> a, Point<T> b)\r
+            {\r
+                return a.X.CompareTo(b.X);\r
+            }\r
+        }\r
+\r
+        private class YComparerClass : IComparer<Point<T>>\r
+        {\r
+            public int Compare(Point<T> a, Point<T> b)\r
+            {\r
+                return a.Y.CompareTo(b.Y);\r
+            }\r
+        }\r
+        #endregion\r
+\r
+        #region private or protected fields\r
+\r
+        private static IComparer<Point<T>> xComparer = new XComparerClass();\r
+        private static IComparer<Point<T>> yComparer = new YComparerClass();\r
+\r
+        #endregion\r
+\r
+        #region constructors\r
+\r
+        public Point()\r
+        {\r
+        }\r
+\r
+        public Point(T x, T y)\r
+        {\r
+            X = x;\r
+            Y = y;\r
+        }\r
+\r
+        public Point(Point<T> point)\r
+        {\r
+            X = point.X;\r
+            Y = point.Y;\r
+        }\r
+        #endregion\r
+\r
+        #region operators\r
+        /// <summary>\r
+        ///     定数倍\r
+        /// </summary>\r
+        /// <param name="p">点</param>\r
+        /// <param name="scale">スケール</param>\r
+        /// <returns>定数倍後の新たなインスタンス</returns>\r
+        public static Point<T> operator *(Point<T> point, T scale)\r
+        {\r
+            T newX = Operator<T>.Mul(point.X, scale);\r
+            T newY = Operator<T>.Mul(point.Y, scale);\r
+            return new Point<T>(newX, newY);\r
+        }\r
+\r
+        /// <summary>\r
+        ///     定数倍\r
+        /// </summary>\r
+        /// <param name="scale">スケール</param>\r
+        /// <param name="point">定数倍される点</param>\r
+        /// <returns>定数倍後の新たなインスタンス</returns>\r
+        public static Point<T> operator *(T scale, Point<T> point)\r
+        {\r
+            return point * scale;\r
+        }\r
+\r
+        #endregion\r
+\r
+        #region properties\r
+\r
+        public T X\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public T Y\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public static IComparer<Point<T>> XComparer\r
+        {\r
+            get\r
+            {\r
+                return xComparer;\r
+            }\r
+        }\r
+\r
+        public static IComparer<Point<T>> YComparer\r
+        {\r
+            get\r
+            {\r
+                return yComparer;\r
+            }\r
+        }\r
+\r
+        #endregion\r
+\r
+        #region public methods\r
+        /// <summary>\r
+        ///     XとYを入れ換える\r
+        /// </summary>\r
+        public void SwapXY()\r
+        {\r
+            T x = X;\r
+            X = Y;\r
+            Y = x;\r
+        }\r
+\r
+        /// <summary>\r
+        ///     加算(ベクトル合成) \r
+        /// </summary>\r
+        /// <param name="left">点1</param>\r
+        /// <param name="right">点2</param>\r
+        /// <returns>加算後の新たなインスタンス</returns>\r
+        public static Point<T> operator +(Point<T> left, Point<T> right)\r
+        {\r
+            T newX = Operator<T>.Add(left.X, right.X);\r
+            T newY = Operator<T>.Add(left.Y, right.Y);\r
+            return new Point<T>(newX, newY);\r
+        }\r
+\r
+        /// <summary>\r
+        ///     X, Y独立に定数倍する\r
+        /// </summary>\r
+        /// <param name="scale">X, Yそれぞれに掛ける係数からなる点</param>\r
+        public Point<T> Stretch(Point<T> scale)\r
+        {\r
+            return Stretch(scale.X, scale.Y);\r
+        }\r
+\r
+        /// <summary>\r
+        ///     点列の各要素を定数倍する\r
+        /// </summary>\r
+        /// <param name="xScale">Xに掛ける係数</param>\r
+        /// <param name="yScale">Yに掛ける係数</param>\r
+        public Point<T> Stretch(T xScale, T yScale)\r
+        {\r
+            T newX = Operator<T>.Mul(X, xScale);\r
+            T newY = Operator<T>.Add(Y, yScale);\r
+            return new Point<T>(newX, newY);\r
+        }\r
+\r
+        public override string ToString()\r
+        {\r
+            return "Point ( " +\r
+                            X.ToString() + ", " + Y.ToString() + " )";\r
+        }\r
+\r
+        #endregion\r
+    }\r
 }\r