OSDN Git Service

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit/trunk@759 7cac0...
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / core / types / NyARDoublePoint2d.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.types;\r
32 \r
33 \r
34 \r
35 /**\r
36  * データ型です。\r
37  * 2次元の浮動小数点方の点を格納します。\r
38  */\r
39 public class NyARDoublePoint2d\r
40 {\r
41         public double x;\r
42         public double y;\r
43         /**\r
44          * 配列ファクトリ\r
45          * @param i_number\r
46          * @return\r
47          */\r
48         public static NyARDoublePoint2d[] createArray(int i_number)\r
49         {\r
50                 NyARDoublePoint2d[] ret=new NyARDoublePoint2d[i_number];\r
51                 for(int i=0;i<i_number;i++)\r
52                 {\r
53                         ret[i]=new NyARDoublePoint2d();\r
54                 }\r
55                 return ret;\r
56         }\r
57         public static NyARDoublePoint2d[][] create2dArray(int i_length_x,int i_length_y)\r
58         {\r
59                 NyARDoublePoint2d[][] ret=new NyARDoublePoint2d[i_length_y][i_length_x];\r
60                 for(int i=0;i<i_length_y;i++)\r
61                 {\r
62                         for(int i2=0;i2<i_length_x;i2++)\r
63                         {\r
64                                 ret[i][i2]=new NyARDoublePoint2d();\r
65                         }\r
66                 }\r
67                 return ret;\r
68         }\r
69         /**\r
70          * p1->p2と、p2->p3の直線の外積を計算します。\r
71          * @param p1\r
72          * @param p2\r
73          * @param p3\r
74          * @return\r
75          */\r
76         public final static double crossProduct3Point(NyARDoublePoint2d p1,NyARDoublePoint2d p2,NyARDoublePoint2d p3)\r
77         {\r
78                 return (p2.x-p1.x)*(p3.y-p2.y)-(p2.y-p1.y)*(p3.x-p2.x);\r
79         }\r
80         /**\r
81          * p1->p2と、p2->p3の直線の外積を計算します。\r
82          * @param p1\r
83          * @param p2\r
84          * @param p3\r
85          * @return\r
86          */\r
87         public final static double crossProduct3Point(NyARDoublePoint2d p1,NyARDoublePoint2d p2,double p3_x,double p3_y)\r
88         {\r
89                 return (p2.x-p1.x)*(p3_y-p2.y)-(p2.y-p1.y)*(p3_x-p2.x);\r
90         }\r
91 \r
92         \r
93         \r
94         /**\r
95          * 頂点配列の中央値を求めます。\r
96          * @param i_points\r
97          * @param i_number_of_data\r
98          * 配列中の有効な頂点数です。\r
99          * @param o_out\r
100          */\r
101         public final static void makeCenter(NyARDoublePoint2d[] i_points,int i_number_of_data,NyARDoublePoint2d o_out)\r
102         {\r
103                 double x,y;\r
104                 x=y=0;\r
105                 for(int i=i_number_of_data-1;i>=0;i--)\r
106                 {\r
107                         x+=i_points[i].x;\r
108                         y+=i_points[i].y;\r
109                 }\r
110                 o_out.x=x/i_number_of_data;\r
111                 o_out.x=y/i_number_of_data;\r
112         }\r
113         /**\r
114          * {@link #makeCenter}の出力型違いの関数です。\r
115          * @param i_points\r
116          * @param i_number_of_data\r
117          * @param o_out\r
118          */\r
119         public final static void makeCenter(NyARDoublePoint2d[] i_points,int i_number_of_data,NyARIntPoint2d o_out)\r
120         {\r
121                 double lx,ly;\r
122                 lx=ly=0;\r
123                 for(int i=i_number_of_data-1;i>=0;i--)\r
124                 {\r
125                         lx+=i_points[i].x;\r
126                         ly+=i_points[i].y;\r
127                 }\r
128                 o_out.x=(int)(lx/i_number_of_data);\r
129                 o_out.y=(int)(ly/i_number_of_data);\r
130         }\r
131         \r
132         /**\r
133          * コンストラクタです。\r
134          */\r
135         public NyARDoublePoint2d()\r
136         {\r
137                 this.x=0;\r
138                 this.y=0;\r
139                 return;\r
140         }\r
141         /**\r
142          * i_srcの値をthisへセットします。\r
143          * @param i_src\r
144          */\r
145         public NyARDoublePoint2d(double i_x,double i_y)\r
146         {\r
147                 this.x=i_x;\r
148                 this.y=i_y;\r
149                 return;\r
150         }\r
151         /**\r
152          * i_srcの値をthisへセットします。\r
153          * @param i_src\r
154          */\r
155         public NyARDoublePoint2d(NyARDoublePoint2d i_src)\r
156         {\r
157                 this.x=i_src.x;\r
158                 this.y=i_src.y;\r
159                 return;\r
160         }\r
161         /**\r
162          * i_srcの値をthisへセットします。\r
163          * @param i_src\r
164          */\r
165         public NyARDoublePoint2d(NyARIntPoint2d i_src)\r
166         {\r
167                 this.x=(double)i_src.x;\r
168                 this.y=(double)i_src.y;\r
169                 return;\r
170         }\r
171         /**\r
172          * p2-p1間の距離の二乗値を計算します。\r
173          * @param i_p1\r
174          * @param i_p2\r
175          * @return\r
176          */     \r
177         public final double sqDist(NyARDoublePoint2d i_p1)\r
178         {\r
179                 double x,y;\r
180                 x=this.x-i_p1.x;\r
181                 y=this.y-i_p1.y;\r
182                 return x*x+y*y;\r
183         }\r
184         public final double sqDist(NyARIntPoint2d i_p1)\r
185         {\r
186                 double x,y;\r
187                 x=this.x-i_p1.x;\r
188                 y=this.y-i_p1.y;\r
189                 return x*x+y*y;\r
190         }       \r
191         /**\r
192          * i_srcの値をthisへセットします。\r
193          * @param i_src\r
194          */\r
195         public final void setValue(NyARDoublePoint2d i_src)\r
196         {\r
197                 this.x=i_src.x;\r
198                 this.y=i_src.y;\r
199                 return;\r
200         }\r
201         /**\r
202          * i_srcの値をthisへセットします。\r
203          * @param i_src\r
204          */\r
205         public final void setValue(NyARIntPoint2d i_src)\r
206         {\r
207                 this.x=(double)i_src.x;\r
208                 this.y=(double)i_src.y;\r
209                 return;\r
210         }\r
211         public final void setValue(double x,double y)\r
212         {\r
213                 this.x=x;\r
214                 this.y=y;\r
215                 return;\r
216         }\r
217         \r
218 }\r