int i; double s = 0.0; double s1; Point gpt = new Point(); long lx = 0; long ly = 0;
if(npoints < 3) return null; // 個々の3角形について for(i = 2; i < npoints; i++) { // 3角形の面積を求める。 s1 = Math.abs(triangleArea(xpoints[0], ypoints[0], xpoints[i-1], xpoints[i-1], xpoints[i], ypoints[i])); // 3角形の重心を求める。 Point pt = centerOfGravity(xpoints[0], ypoints[0], xpoints[i-1], ypoints[i-1], xpoints[i], ypoints[i]); // 位置*重心の和を求める。 lx += (s1 * pt.x); ly += (s1 * pt.y); // 面積の和を求める s += s1; }
// 重心を求める。 gpt.x = (int)(lx / s); gpt.y = (int)(ly / s);
/** * 三角形の重心を計算する関数は以下のように実装します。 */ private static Point centerOfGravity(int x1, int y1, int x2, int y2, int x3, int y3) { int x = (x1+x2+x3)/3; int y = (y1+y2+y3)/3; return new Point(x, y); }
/** * 三角形の面積を計算する関数は以下のように実装します。 */ private static double triangleArea(int ax, int ay, int bx, int by, int cx, int cy) { return (double)((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))/2.0; }
|