views:

631

answers:

4

I see there's a good question already for general polygons here. Are there any simpler or more efficient algorithms specific to quadrilaterals?

+6  A: 

Nope. I'd use the formula in the post you mention.

Edits:

To elaborate on that a lot, the method presented in the post you mention (called the Closed Polygon Approach in Reed Copsey's answer) DOES end up breaking the list of points into triangles and calculating their areas using cross products. It gets away with not triangulating anything by taking advantage of both positive and negative areas, according to the ordering (winding) of the points that describe the polygon. Because it takes advantage of both positive and negative areas, this approach does not require any calculations for the lines that make up each triangle in the quadrilateral, and it does not matter if the quadrilateral is convex or not.

That said, it is easier to conceptually understand breaking up the quadrilateral into two non-overlapping triangles, and independently calculating the area of each triangle. This approach will always yield the correct result as well. The complication for this approach comes in deciding which pair of opposing vertices should specify the break between the two triangles. If you have a non-convex quadrilateral and choose the wrong triangulation, then you end up with overlapping triangles that (unless accounted for) will skew the area result. If some care is taken in calculating these triangles' areas, you will find that (in the case specifically of a quadrilateral) one triangle will always be contained in the other. With some cleverness, you can get the contained triangle's area to have the opposite sign of the containing triangle's area, which will then again yield the correct result.

In essence, these two algorithms are the same. There is no performance difference; suppose the quad is specified by x0, y0, x1, y1, x2, y2, x3, and y3. Then the closed polygon approach has the following operations:

area = 0.5 * abs( x0 * y1 - x1 * y0 + x1 * y2 - x2 * y1 + 
  x2 * y3 - x3 * y2 + x3 * y0 - x0 * y3 )

Which may be simplified as:

area = 0.5 * abs( x0 * (y1 - y3) + x1 * (y2 - y0) + x2 * (y3 + y1) + 
  x3 * (y0 - y2) )

Which works out to (count the *'s and +'s) 12 operations altogether. The other approach, finding each individual triangle and taking the cross product works as follows:

x2_line = x2 - x0
y2_line = y2 - y0
area = 0.5 * abs( (x1 - x0) * y2_line + (y1 - y0) * x2_line + 
  x2_line * (y3 - y0) + y2_line * (x3 - x0) )

Which may again be simplified to:

x2_line = x2 - x0
y2_line = y2 - y0
area = 0.5 * abs( y2_line * (x1 - x0 + x3 - x0) + x2_line * (y1 - y0 + y3 - y0) )

Which also works out to 12 operations. The exact same number of operations.

So, the biggest difference is that the triangulation followed by cross-product area calculation is easier to understand, in that it is very straightforward, whereas the Closed-Polygon approach is really just the same algorithm, but optimized and thus presented in a different way.

In conclusion, yes, the formula in the post you mention is the most efficient one you've got, and is at the same time the simplest algorithm, when presented differently.

Walt W
Why the downvote . . . the general formula requires 3 operations per vertex pair + 1 multiply by 0.5, which equates to 3*4+1 = 13 operations for a quad . . .
Walt W
Plus, the closed polygon approach mentioned in the top post IS the original formula mentioned in the question...
Walt W
Originally, it seemed like a snotty answer that I see all too often. But your edit and comments earned it back. Consider putting those comments in another edit.
geowa4
@geowa4: Better?
Walt W
+1 for the very good explanation
Reed Copsey
+10  A: 

For a (convex) quadrilateral, it's often faster to just split the quad into two triangles, and compute the area of the two triangles.

If the quadralateral is not guaranteed convex, the closed polygon approach is still my preference, since it's usually faster than the checks to figure out how to correctly split the quad.


Edit from Comments:

As Walt W points out, the two approaches are theoretically identical in terms of performance. The second is more flexible, due to not requiring convex quads, but the first (splitting triangles) is easier to implement as well as understand, so potentially more maintainable.

Reed Copsey
Which formula would you use for calculating the area of each triangle?
Walt W
You can use any approach. I usually use 1/2 the cross product of the two vectors, but that's because I'm almost always working in 3D space, and that works for non-planar quads (although, technically, you should do a ruled surface in that case, which is not fast).
Reed Copsey
Well, my question is how many operations are involved? Why is that faster than always using the closed polygon approach (as you call it, which is the same as the formula the OP provided a link to)?
Walt W
Walt: The closed polygon approach, when you drop it to this point, basically is just doing cross products. The number of computations ~should~ be identical or near identical - but it's easier to understand IMO. However, as I said, if there's not guarantee on correctness, I'd just use that. (Realize, too, that the closed polygon approach linked only works for computing 2D areas, not 3D. If you need to extend it into 3D, it's quite a few more computations than what you posted...)
Reed Copsey
The thing about closed polygon approach though is that it does not care where in space the triangles are located. To do cross products, you must find the length and direction of the triangle's edges, which require subtracting them from one another. The closed polygon approach gets away with not moving the lines to the origin because the area between the origin and the polygon's points is added and subtracted an equal number of times, zeroing it out. If efficiency is key, I think that the closed polygon approach is more appropriate. Please correct me if I'm wrong about the subtractions..
Walt W
For the record, I really am just trying to figure this out objectively . . . but when I do cross products, I get one triangle as: (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0) = area * 2 = 7 operations. For two triangles, plus multiplying by 0.5 again, this works out to 15 operations. Whereas the general approach (by my figuring in my answer below) works out to 13 operations. Am I wrong? I really may be. But am I?
Walt W
After a little more thought, I guess you could cache one of the lines to remove two subtractions from the 2nd triangle calculation. Even so, this is equivalent to the closed polygon approach but works under fewer conditions. I agree it's easier to understand . . . But not necessarily the better solution. I'll stop commenting now and wait, sorry about the comment storm :X Just trying to work this out.
Walt W
I believe, if implemented correctly, the two approaches wind up being almost exactly equivalent in terms of operations. Stoke's theorem (which is what the 2nd approach is based on), I believe, started with a triangulation, and derived the second approach. I don't think one is "better" than the other - I just go for easy to understand first, unless there is a reason to go otherwise.
Reed Copsey
Gotcha. Why not re-word your post then to reflect that the former is easier to understand and a better beginner approach, but they are equivalent (excepting that the latter works for non-convex as well)?
Walt W
@Walt W: How's that?
Reed Copsey
Well, I'd argue about the ease of implementation, but it'll do ;) Thanks.
Walt W
A: 

The mathworld page lists several formulae.

moonshadow
A: 

Break the quadrialateral into two triangles and calculate the area of both.

Once you have two triangles, Heron's Formula works well within a computer program.

For a triangle with sides a, b, and c, the area is

double area = Math.Sqrt((a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c)/16);

This method works with any quadrilateral whether it is a rectangle, square, rhombus, or trapezoid.

Rap
There are quadrilaterals that are not rectangles, squares, rhombuses, or trapezoids! If they're convex, "break into two triangles" is a nontrivial task, as Reed noted.
Ken
@Ken: That should be "If they're NOT convex"... convex quads are trivial, since either break is valid. Non-convex quads can cause severe over-calculation of area.
Reed Copsey
Is the simplest solution then to calculate the area of both sets of triangles and choose the minimum value?
Kirk Broadhurst