Question

I am studying the following code.

boolean convex(double x1, double y1, double x2, double y2, 
       double x3, double y3)
{
if (area(x1, y1, x2, y2, x3, y3) < 0)
    return true;
else
    return false;
}


/* area:  determines area of triangle formed by three points
 */
double area(double x1, double y1, double x2, double y2,
    double x3, double y3)
{
double areaSum = 0;

areaSum += x1 * (y3 - y2);
areaSum += x2 * (y1 - y3);
areaSum += x3 * (y2 - y1);

/* for actual area, we need to multiple areaSum * 0.5, but we are
     * only interested in the sign of the area (+/-)
     */

return areaSum;
}

I do not understand the concept that area being negative. Shouldn't area be always positive? maybe I am lacking some understanding of terms here. I tried to contact the original writer but this code is about 8 years old and I have no way to contact the original writer. This method of determining if the given vertex x2y2 is convex seems really mobile. I really want to understand it. Any direction or reference to help me understand this piece of code will be appreciated greatly.

Source code : http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian/applets/BruteForceEarCut.java

Was it helpful?

Solution

The algorithm use a very simple formula with which you can compute twice the area of a triangle.

This formula has two advantages:

  • it doesn't require any division
  • it returns a negative area if the point are in the counterclockwise order.

In the code sample, the actual value of the area doesn't matter, only the sign of the result is needed.

The formula can also be used to check if three points are colinear.

You can find more information about this formula on this site : http://www.mathopenref.com/coordtrianglearea.html

OTHER TIPS

This algorithm is basically using the dot product of two vectors and interpreting the results. This is the core of the Gift Wrapping Algorithm used to find convex hulls.

Since a dot b is also equal to |a|*|b|*cos(theta) then if the result is positive, cos of theta must be positive and thus convex. Per a wiki article on cross products...

Because the magnitude of the cross product goes by the sine of the angle between its arguments, the cross product can be thought of as a measure of ‘perpendicularity’ in the same way that the dot product is a measure of ‘parallelism’. Given two unit vectors, their cross product has a magnitude of 1 if the two are perpendicular and a magnitude of zero if the two are parallel. The opposite is true for the dot product of two unit vectors.

The use of "area" is slightly misleading on part of the original coder in my opinion.

You know about how integrals work, right? One way to think of integrals is in terms of the area under the integrated curve. For functions that are strictly positive, that definition works great, but when the function becomes negative at some point, there is a problem because then you have to take the absolute value, right?

That is not always so, actually, and it can be quite useful in some contexts to leave the curve negative. Think back to what was said earlier: the area under the curve. All that space between negative infinity and our function. Clearly, that is absurd, right? A better way to think of it is as the difference between the area under the curve, and the area under the x axis. That way, when the function is positive, our curve is gaining more area, and when it is negative, it is gaining less than the x axis.

The same thing applies to plane figures that are not strict functions. In order to really determine this, we have to define which direction our edge is going as it travels around the figure. We can define it so that all the area on the right of our curve is inside the region, and all the area to the left is outside (or we can define it the other way around, but I will use the first way).

So our figure includes all the area from there to the edge at infinity of the plane that is directly to our right. Regions enclosed clockwise really include their conventional interior twice. Regions enclosed counterclockwise don't include their conventional interior at all. The area, then, is the difference between our region and the whole plane.

The application of this to concavity is fairly simple, if you understand what it actually means to be concave or convex. The triangle you are given is concave if it is cutting an area out from the plane, and it is convex if you are adding extra area to it. That is the exact same thing we were doing to determine the our area, so positive area corresponds to a convex shape, and negative area corresponds to a concave shape.

You can also do other weird things with this conceptual model. For instance, you can turn a region 'inside out' by reversing the edge direction.

I'm sorry if this has been a little hard to follow, but this is the actual way I understand negative area.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top