On 02/07/2014 at 12:39, xxxxxxxx wrote:
Cinema 4D Version: 13
Platform: Windows ;
Language(s) : C++ ;
I'm trying to find the area of a polygon. And I was wondering if there's a better way than how I'm doing it?
Right now I'm splitting it up into two triangles. And getting the first triangle's area like this:
PolygonObject *obj = (PolygonObject* ) doc->GetActiveObject(); if(!obj || obj->GetType() != Opolygon) return FALSE; CPolygon poly = obj->GetPolygonW(); //Get the first polygon in the object to find it's area Vector p1 = obj->GetPointW()[poly.a]; Vector p2 = obj->GetPointW()[poly.b]; Vector p3 = obj->GetPointW()[poly.c]; Vector p4 = obj->GetPointW()[poly.d]; Real side1 = sqrt( (p2.x-p1.x) + (p2.y-p1.y) + (p2.z-p1.z) ); //Distance a-b Real side2 = sqrt( (p3.x-p2.x) + (p3.y-p2.y) + (p3.z-p2.z) ); //Distance b-c Real side3 = sqrt( (p3.x-p1.x) + (p3.y-p1.y) + (p3.z-p1.z) ); //Distance c-a The hypotenuse Real s = (side1+side2+side3)/2; Real area= sqrt(s*(s-side1)*(s-side2)*(s-side3)); //The area of only this side (triangle) of the polygon GePrint(RealToString(area));
This only gives me the area for the left hand triangle area of the polygon.
So now do I have to run the same damned code all over again. Using the right side edges to get the other polygon's area. Then add them together?
Repeating the same code twice just to get both triangle's areas seems a bit silly to me.
Is there a better(shorter) way to get the area of a polygon?