OSDN Git Service

Bヘッダのない形式への部分的対応
[karinto/karinto.git] / Karinto / Point.cs
1 /*\r
2  *      Karinto Library Project\r
3  *\r
4  *      This software is distributed under a zlib-style license.\r
5  *      See license.txt for more information.\r
6  */\r
7 \r
8 using System;\r
9 using System.Collections.Generic;\r
10 using Karinto.Xml;\r
11 using Karinto.Numeric;\r
12 \r
13 namespace Karinto\r
14 {\r
15     /// <summary>\r
16     ///     2次元座標を示す\r
17     /// </summary>\r
18     public class Point\r
19     {\r
20         #region type definition\r
21 \r
22         private class XComparerClass : IComparer<Point>\r
23         {\r
24             public int Compare(Point a, Point b)\r
25             {\r
26                 return a.X.CompareTo(b.X);\r
27             }\r
28         }\r
29 \r
30         private class YComparerClass : IComparer<Point>\r
31         {\r
32             public int Compare(Point a, Point b)\r
33             {\r
34                 return a.Y.CompareTo(b.Y);\r
35             }\r
36         }\r
37         #endregion\r
38 \r
39         #region private or protected fields\r
40 \r
41         private static IComparer<Point> xComparer = new XComparerClass();\r
42         private static IComparer<Point> yComparer = new YComparerClass();\r
43 \r
44         #endregion\r
45 \r
46         #region constructors\r
47 \r
48         public Point()\r
49         {\r
50         }\r
51 \r
52         public Point(double x, double y)\r
53         {\r
54             X = x;\r
55             Y = y;\r
56         }\r
57 \r
58         public Point(Point point)\r
59         {\r
60             X = point.X;\r
61             Y = point.Y;\r
62         }\r
63         #endregion\r
64 \r
65         #region operators\r
66         /// <summary>\r
67         ///     定数倍\r
68         /// </summary>\r
69         /// <param name="p">点</param>\r
70         /// <param name="scale">スケール</param>\r
71         /// <returns>定数倍後の新たなインスタンス</returns>\r
72         public static Point operator *(Point point, double scale)\r
73         {\r
74             return new Point(point.X * scale, point.Y * scale);\r
75         }\r
76 \r
77         /// <summary>\r
78         ///     定数倍\r
79         /// </summary>\r
80         /// <param name="scale">スケール</param>\r
81         /// <param name="point">定数倍される点</param>\r
82         /// <returns>定数倍後の新たなインスタンス</returns>\r
83         public static Point operator *(double scale, Point point)\r
84         {\r
85             return new Point(point.X * scale, point.Y * scale);\r
86         }\r
87 \r
88         public static implicit operator Point(Complex complex)\r
89         {\r
90             return new Point(complex.Real, complex.Imaginary);\r
91         }\r
92 \r
93         public static implicit operator Complex(Point point)\r
94         {\r
95             return new Complex(point.X, point.Y);\r
96         }\r
97 \r
98         #endregion\r
99 \r
100         #region properties\r
101 \r
102         public double X\r
103         {\r
104             get;\r
105             set;\r
106         }\r
107 \r
108         public double Y\r
109         {\r
110             get;\r
111             set;\r
112         }\r
113 \r
114         /// <summary>\r
115         ///     原点からの距離\r
116         /// </summary>\r
117         public double Distance\r
118         {\r
119             get\r
120             {\r
121                 return Math.Sqrt(X * X + Y * Y);\r
122             }\r
123         }\r
124 \r
125         /// <summary>\r
126         ///     原点からの距離\r
127         /// </summary>\r
128         public double Norm\r
129         {\r
130             get\r
131             {\r
132                 return Math.Sqrt(X * X + Y * Y);\r
133             }\r
134         }\r
135 \r
136         /// <summary>\r
137         ///     偏角\r
138         /// </summary>\r
139         public double Argument\r
140         {\r
141             get\r
142             {\r
143                 return Math.Atan2(Y, X);\r
144             }\r
145         }\r
146 \r
147         public static IComparer<Point> XComparer\r
148         {\r
149             get\r
150             {\r
151                 return xComparer;\r
152             }\r
153         }\r
154 \r
155         public static IComparer<Point> YComparer\r
156         {\r
157             get\r
158             {\r
159                 return yComparer;\r
160             }\r
161         }\r
162 \r
163         #endregion\r
164 \r
165         #region public methods\r
166         /// <summary>\r
167         ///     XとYを入れ換える\r
168         /// </summary>\r
169         public void SwapXY()\r
170         {\r
171             double x = X;\r
172             X = Y;\r
173             Y = x;\r
174         }\r
175 \r
176         /// <summary>\r
177         ///     加算(ベクトル合成) \r
178         /// </summary>\r
179         /// <param name="left">点1</param>\r
180         /// <param name="right">点2</param>\r
181         /// <returns>加算後の新たなインスタンス</returns>\r
182         public static Point operator +(Point left, Point right)\r
183         {\r
184             return new Point(left.X + right.X, left.Y + right.Y);\r
185         }\r
186 \r
187         /// <summary>\r
188         ///     X, Y独立に定数倍する\r
189         /// </summary>\r
190         /// <param name="scale">X, Yそれぞれに掛ける係数からなる点</param>\r
191         public Point Stretch(Point scale)\r
192         {\r
193             return new Point(X * scale.X, Y * scale.Y);\r
194         }\r
195 \r
196         /// <summary>\r
197         ///     点列の各要素を定数倍する\r
198         /// </summary>\r
199         /// <param name="xScale">Xに掛ける係数</param>\r
200         /// <param name="yScale">Yに掛ける係数</param>\r
201         public Point Stretch(double xScale, double yScale)\r
202         {\r
203             return new Point(X * xScale, Y * yScale);\r
204         }\r
205 \r
206         public override string ToString()\r
207         {\r
208             return "Point ( " +\r
209                             X.ToString("R") + ", " + Y.ToString("R") + " )";\r
210         }\r
211 \r
212         #endregion\r
213     }\r
214 \r
215 }\r