java.awt.geom多边形,如何计算java.awt.geom.Area的面积?

I am looking for a way to calculate the area, in pixels, of an arbitrary instance of java.awt.geom.Area.

The background: I have Shapes in my applications that may overlap. I want to know how much one Shape overlaps another. The Shapes may be skewed, rotated, etc. If I had a function area(Shape) (or Area), I could use the intersection of two Shapes like so:

double fractionObscured(Shape bottom, Shape top) {

Area intersection = new Area(bottom);

intersection.intersect(new Area(top));

return area(intersection) / area(bottom);

}

解决方案

One approach would be to fill() each scaled and transformed Shape with a different color using a suitable AlphaComposite and count the overlapping pixels in the underlying Raster.

Addendum 1: Using this calculator to see the effect of AlphaComposite.Xor shows that the intersetion of any two opaque colors is zero.

Addendum 2: Counting pixels may have performance problems; sampling may help. If each Shape is reasonably convex, it may be possible to estimate the overlap from the ratio of the intersect() area to the sum of the areas of the Shapes' getBounds2D(). For example,

Shape s1, s2 ...

Rectangle2D r1 = s1.getBounds2D();

Rectangle2D r2 = s2.getBounds2D();

Rectangle2D r3 = new Rectangle2D.Double();

Rectangle2D.intersect(r1, r2, r3);

double overlap = area(r3) / (area(r1) + area(r2));

...

private double area(Rectangle2D r) {

return r.getWidth() * r.getHeight();

}

You may need to validate the results empirically.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值