Re: mathematical area of closed vector path
Re: mathematical area of closed vector path
- Subject: Re: mathematical area of closed vector path
- From: Nicko van Someren <email@hidden>
- Date: Tue, 25 Jan 2005 16:13:35 +0000
On 25 Jan 2005, at 13:37, Tim Ramsey wrote:
I saw a number of responses to this that involved complex geometry
calculations that may or may not work depending on the nature of the
path. A completely different approach is to compute an off-screen
bitmap representation of the content of the path. Color or flag each
pixel inside the area, then count the colored pixels. Scale the
logical size of the map to be big enough so that resolution gives you
the accuracy you need. You can determine if you are inside the region
to color a pixel by rasterizing and counting line crossings (if the
count is odd you are inside) or by keeping track of intersecting areas
as you erect the path. Of course, you can also skip building a special
bitmap if you have actually drawn the path on screen and the
resolution is good enough for your accuracy needs.
For most paths I think that this method will be both slower and consume
more memory. Rendering the path to a bit map at any scale will involve
flattening it, which is a pre-requisite of the geometric method, but
once flattened the calculations are very simple; in essence you compute
the area of each quadrilateral formed by one segment of the path, the
vertical lines from the end points to one axis (usually X) parallel to
the other (Y) axis and the line along the first axis between those two
lines. The area is computed as negative if the end point of the line
segment is left of the start point. The sum of these areas along all
line segments is the enclosed area if the path runs clockwise.
It should be noted that the geometric method fails in most cases where
the winding rule for rendering would be significant. For instance if
you have a counter-clockwise loop wholly inside a clockwise loop it
works fine (and non-zero and odd-even winding rules for rendering look
the same) but if both loops are clockwise then the inner loop gets
counted twice. Loops in a figure of eight or other types of loop that
are not inherently either clockwise or counter-clockwise also fail.
Cheers,
Nicko
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden