如何计算java.awt.geom.Area的面积? [英] How to calculate the area of a java.awt.geom.Area?

查看:327
本文介绍了如何计算java.awt.geom.Area的面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来计算 java.awt.geom.Area 任意实例的面积(以像素为单位)。



背景:我的应用程序中有 Shape s可能会重叠。我想知道一个 Shape 与另一个重叠。如果我有一个函数 area(Shape)(或者 c),那么 Shape可能会被扭曲,旋转等等。 c $ c> Area ),我可以像这样使用两个 Shape 的交集:

  double fractionObscured(Shape bottom,Shape top){
Area intersection = new Area(bottom);
intersection.intersect(new Area(top));
返回区域(交叉点)/区域(底部);
}


解决方案

a href =http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#fill%28java.awt.Shape%29 =nofollow noreferrer> fill() 每个缩放和转换 Shape ,使用合适的 AlphaComposite 并计算重叠像素基础 Raster



附录1:使用此 calculator ,以查看 AlphaComposite.Xor 显示任何两个不透明颜色的交点是零。



附录2:计算像素可能会有性能问题;抽样可能有帮助。如果每个 Shape 是相当凸的,可以根据 intersect() 面积与 Shape s' getBounds2D() 。例如,

 形状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();
}

您可能需要凭经验验证结果。 b

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.

这篇关于如何计算java.awt.geom.Area的面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆