/* * AreaCalc.java * Calculate area of arbitrary polygone * * (c) kewler * http://kewler.ru * */ import java.awt.geom.*; public class AreaCalc { public static void main(String[] args) { Area a1 = new Area(new Rectangle2D.Float(50, 50, 200, 100)); System.out.println("area: "+area(a1)); } public static double area(Area area) { double res = 0.0; double res1 = res; float x0,x1,x2,x3; float y0,y1,y2,y3; float[] sc = new float[6]; float[] cc = new float[6]; float[] pc = new float[6]; for (PathIterator i = area.getPathIterator(null); !i.isDone(); i.next()) { int type = i.currentSegment(cc); switch (type) { case PathIterator.SEG_MOVETO: sc[0] = pc[0] = cc[0]; sc[1] = pc[1] = cc[1]; break; case PathIterator.SEG_CUBICTO: // System.out.println("CUBE"); x0 = pc[0]; y0 = pc[1]; x1 = cc[0]; y1 = cc[1]; x2 = cc[2]; y2 = cc[3]; x3 = cc[4]; y3 = cc[5]; res1 = 0.3 * y1 * x0 - 0.15 * y1 * x2 - 0.15 * y1 * x3 - 0.3 * y0 * x1 - 0.15 * y0 * x2 - 0.05 * y0 * x3 + 0.15 * y2 * x0 + 0.15 * y2 * x1 - 0.3 * y2 * x3 + 0.05 * y3 * x0 + 0.15 * y3 * x1 + 0.3 * y3 * x2; res += res1; // System.out.println("cubeto> "+res1+" >> "+res); pc[0] = cc[4]; pc[1] = cc[5]; break; case PathIterator.SEG_QUADTO: x0 = pc[0]; y0 = pc[1]; x1 = cc[0]; y1 = cc[1]; x2 = cc[2]; y2 = cc[3]; res1 = (-x0*y1 + y0*x1 - 0.5 * x0*y2 - x1*y2 + 0.5 *x2*y0 + x2*y1)/3.0; res += res1; pc[0] = cc[2]; pc[1] = cc[3]; // System.out.println("QUAD"); case PathIterator.SEG_LINETO: // res += x(i)*y(i+1) - x(i+1)*y(i) old // x(i) = pc[0] // x(i+1) = cc[0] // y(i) = pc[1] // y(i+1) = cc[1] // x(0) = sc[0] // y(0) = sc[1] // res+=pc[0]*cc[1]-cc[0]*pc[1]; res1 = (pc[1] + cc[1]) * (pc[0] - cc[0]) / 2.0; res += res1; // System.out.println("lineto> "+res1+" >> "+res); pc[0] = cc[0]; pc[1] = cc[1]; break; case PathIterator.SEG_CLOSE: // System.out.println(cc[0]+","+cc[1]+" -> "+sc[0]+","+sc[1]); res1 = (pc[1] + sc[1]) * (pc[0] - sc[0]) / 2.0; res += res1; // System.out.println("segclose> "+res1+" >> "+res); break; } // switch } // for // res/=2.0; return Math.abs(res); } }