Comments on: Measuring overlap in graphical objects
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects/
Comments on Ask MetaFilter post Measuring overlap in graphical objectsMon, 18 Feb 2008 17:38:24 -0800Mon, 18 Feb 2008 17:38:24 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Measuring overlap in graphical objects
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects
SVG or Java: Measuring the overlap of graphic objects <br /><br /> If I have two overlapping objects, say two circles, is it possible to measure the overlap between them as a fraction of the enclosed area of either object?<br>
<br>
Can this be done within SVG, and if not, is this kind of measurement possible with the Java AWT Graphics package, or another framework I can plug into Java?post:ask.metafilter.com,2008:site.83976Mon, 18 Feb 2008 17:17:07 -0800Blazecock PileonjavasvggraphicgraphicsobjectimageimagingBy: sanko
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects#1243123
Looks like you should be able to get it done with SVG's <a href="http://www.w3.org/TR/SVG11/script.html">scripting</a> capabilities. See equation 14 <a href="http://mathworld.wolfram.com/Circle-CircleIntersection.html">here</a> for the area of intersection.comment:ask.metafilter.com,2008:site.83976-1243123Mon, 18 Feb 2008 17:38:24 -0800sankoBy: holloway
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects#1243130
Measure the overlap, the area of the overlap?<br>
<br>
If you've got two bitmaps (with alpha) and you're trying to see the overlap then just traverse each pixel in the image and see if there's any equivalent pixel in the image below. SVG doesn't help here but any canvas whose pixels can be queried will work.<br>
<br>
If you're talking generally about intersection between vector shapes (which involves calculating the area accurately, including what would be subpixel if serialized to a bitmap) then SVG or any canvas won't really help. Intersection between shapes such as this is just math (like the kind that sanko links at mathworld) so get someone who knows how to work circle or polygon overlaps (eg, matrices).comment:ask.metafilter.com,2008:site.83976-1243130Mon, 18 Feb 2008 17:46:36 -0800hollowayBy: Netzapper
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects#1243138
It depends on what exactly you're trying to do.<br>
<br>
If you want regular polygons (or circles), you could easily do this with simple geometry.<br>
<br>
If you want arbitrary polygons, which represent real graphical objects (like, UI elements or game sprites), then you need to compute the intersection of the polygons (or their convex hulls, which is easier). Very fast versions of this algorithm exist. You would do this completely outside of the graphics context, merely reusing the data you'll eventually use to render the graphics. <br>
<br>
If you want arbitrary polygons, and do not care about how it looks, you can estimate the area of the overlap with the following algorithm:<br>
<br>
1) Make an image buffer (this is easier than doing it on the main buffer, really).<br>
2) Set your pen color to pure blue, and draw your first shape.<br>
3) Set the raster op to add.<br>
4) Set your pen color to red, and draw your second shape.<br>
5) Retrieve pixels from the buffer, counting how many are red, blue, and purple.<br>
6) Intersecting area as a function of first shape's total area is: purple / (blue + purple).<br>
<br>
This has the drawback that it only estimates by counting pixels. It's also way slower than fast polygon intersection.comment:ask.metafilter.com,2008:site.83976-1243138Mon, 18 Feb 2008 17:53:42 -0800NetzapperBy: Blazecock Pileon
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects#1243195
<i>Very fast versions of this algorithm exist.</i><br>
<br>
Curious, what are those algorithms?comment:ask.metafilter.com,2008:site.83976-1243195Mon, 18 Feb 2008 18:45:13 -0800Blazecock PileonBy: Netzapper
http://ask.metafilter.com/83976/Measuring-overlap-in-graphical-objects#1244628
Google something along the lines of "2d convex hull intersection" or "2d polygon intersection". As I think about it further, you won't actually benefit from the faster algorithms much. You have to check all sides to find all possible intersection points. Then, if you go with concave (or general) polygons, you'll run into problems defining what is "inside" the other.<br>
<br>
Can you be more specific about the problem you're trying to solve? My email is in my profile, if you'd prefer. I can't guarantee much of my time, but I can at least point you in the right direction.comment:ask.metafilter.com,2008:site.83976-1244628Tue, 19 Feb 2008 18:57:52 -0800Netzapper