DEMO.DESIGN
Frequently Asked Questions
 
оглавление | demo party в ex-СССР | infused bytes e-mag | новости от ib/news | другие проекты | письмо | win koi lat

следующий фpагмент (2)
- [24] Usenet echoes (21:200/1) --------------------- COMP.GRAPHICS.ALGORITHMS - Msg : 87 of 94 From : orourke@sophia.smith.edu 2:5030/144.99 30 Mar 94 02:29:00 To : All Subj : Re: Centroid fun -------------------------------------------------------------------------------- In article <1994Mar30.013606.17818@wixer.bga.com>, Jason Cohen <jcohen@wixer.bga.com> wrote: > >Does anyone know of an algorithm/formula that, given points of a 2D polygon >defined counter-clockwise and either concave or convex, returns the >coordinates of the centroid? It is just the average of the defining points? No, you have to use area to weight properly. I gave this as an assignment in a class here. Here is one solution. Why this works is another story. /* Written by Joseph O'Rourke orourke@sophia.smith.edu Adapted by Junheng Luo to be center of gravity code. */ #include <stdio.h> #include <math.h> #define ERROR 1 #define X 0 #define Y 1 typedef enum { FALSE, TRUE } bool; #define DIM 2 /* Dimension of points */ typedef int tPointi[DIM]; /* type integer point */ typedef double tPointd[DIM]; /* type double point */ #define PMAX 1000 /* Max # of pts in polygon */ typedef tPointi tPolygoni[PMAX];/* type integer polygon */ int Area2( tPointi a, tPointi b, tPointi c ); void FindCG( int n, tPolygoni P, tPointd CG ); int ReadPoints( tPolygoni P ); void Centroid( tPointi p1, tPointi p2, tPointi p3, tPointd c ); void PrintPoint( tPointd p ); main() { int n; tPolygoni P; tPointd CG; n = ReadPoints( P ); FindCG( n, P ,CG); printf("The cg is "); PrintPoint( CG ); putchar('\n'); } /* Returns twice the signed area of the triangle determined by a,b,c, positive if a,b,c are oriented ccw, and negative if cw. */ int Area2( tPointi a, tPointi b, tPointi c ) { return a[0] * b[1] - a[1] * b[0] + a[1] * c[0] - a[0] * c[1] + b[0] * c[1] - c[0] * b[1]; } /* Returns twice the area of polygon P. */ void FindCG( int n, tPolygoni P, tPointd CG) { int i; double A,Areasum = 0; /* Partial area sum */ tPointd Centr; CG[0] = 0; CG[1] = 0; for (i = 1; i < n-1; i++) { Centroid( P[0], P[i], P[i+1], Centr ); A = (double) Area2( P[0], P[i], P[i+1]) /2; CG[0] += A * Centr[0]; CG[1] += A * Centr[1]; Areasum += A; } CG[0] /= Areasum; CG[1] /= Areasum; return; } void Centroid( tPointi p1, tPointi p2, tPointi p3, tPointd c ) { c[0] = (double)(p1[0] + p2[0] + p3[0])/3; c[1] = (double)(p1[1] + p2[1] + p3[1])/3; return; } void PrintPoint( tPointd p ) {...} int ReadPoints( tPolygoni P ) {...}

Всего 1 фpагмент(а/ов) |пpедыдущий фpагмент (1)

Если вы хотите дополнить FAQ - пожалуйста пишите.

design/collection/some content by Frog,
DEMO DESIGN FAQ (C) Realm Of Illusion 1994-2000,
При перепечатке материалов этой страницы пожалуйста ссылайтесь на источник: "DEMO.DESIGN FAQ, http://www.enlight.ru/demo/faq".