home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.graphics
- Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!curt
- From: curt@watson.ibm.com (Curt McDowell)
- Subject: Re: Area of polygons
- Sender: news@watson.ibm.com (NNTP News Poster)
- Message-ID: <1992Aug27.180851.5035@watson.ibm.com>
- Date: Thu, 27 Aug 1992 18:08:51 GMT
- Lines: 109
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <1464@igd.fhg.de>
- Nntp-Posting-Host: gorby.watson.ibm.com
- Organization: IBM T.J. Watson Research Center
- Keywords: polygon,triangle,constraints
-
- In article <1464@igd.fhg.de>, willm@igd.fhg.de (Matt Will) writes:
- > I don't know if this is the right place to ask, but I'll give it a try.
- > I am currently trying to find constraints for planar (2D) polygons, and in
- > particular, I have troubles finding a constraint (=a formula) to calculate
- > the area of a polygon.
- >
- > I know that it is possible to subdivide a polygon into triangles, but I haven't
- > found a general formula to find out which diagonals to use for this. With
- > convex polygons, things are easy, but I got stuck as far as concave polygons
- > are concerned.
- >
- > Please e-mail me, since I am not reading this list. You can reach me at the
- > address listed below.
- >
- > Thank you for your help,
- > Matt Will <willm@igd.fhg.de>
-
- Here's an area formula that so neat I thought I'd post it in addition
- to sending it to Matt. It probably extends to 3D as well, but right now
- I don't have time to test that theory.
-
- /*
- * polyarea
- *
- * Computes the area of an arbitrary n-point polygon using the following
- * amazingly beautiful formula from high school geometry:
- *
- * | x(0) y(0) | | x(1) y(1) | | x(n-1) y(n-1) |
- * 2A = det | | + det | | + ... + det | |
- * | x(1) y(1) | | x(2) y(2) | | x(0) y(0) |
- *
- * The area is positive if the points are in counter-clockwise order,
- * negative if they are clockwise.
- *
- * Curt McDowell, IBM 08-27-92
- */
-
- double polyarea(x, y, n)
- double *x, *y;
- int n;
- {
- double a;
- int i;
-
- a = x[n - 1] * y[0] - x[0] * y[n - 1];
-
- for (i = 0; i < n - 1; i++)
- a += x[i] * y[i + 1] - x[i + 1] * y[i];
-
- return (a / 2.0);
- }
-
- /*
- * Test main program for polyarea
- */
-
- #include <stdio.h>
- #include <math.h>
-
- extern void *malloc();
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int n, i;
- double *x, *y;
-
- if (argc != 2) {
- fprintf(stderr,
- "Usage: polyarea npoints\n");
- exit(1);
- }
-
- n = atoi(argv[1]);
-
- /*
- * Input array of n points
- */
-
- printf("Please enter %d points.\n", n);
-
- x = malloc(n * sizeof (*x));
- y = malloc(n * sizeof (*y));
-
- for (i = 0; i < n; i++) {
- char buf[256];
-
- printf("p%d.x? ", i);
- fflush(stdout);
- if (gets(buf) == 0)
- exit(0);
- x[i] = atof(buf);
-
- printf("p%d.y? ", i);
- fflush(stdout);
- if (gets(buf) == 0)
- exit(0);
- y[i] = atof(buf);
- }
-
- printf("The area of the polygon is %f units.\n", polyarea(x, y, n));
-
- exit(0);
- }
-
- --
- Curt McDowell
- IBM T. J. Watson Research Center
-