1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3
4 /*************************************************************************
5 * Copyright (c) 2011 AT&T Intellectual Property
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors: See CVS logs. Details at http://www.graphviz.org/
12 *************************************************************************/
13
14 /*
15 * Force the vertices of a polygon to be in CW order.
16 *
17 * Works for polygons with concavities.
18 * Does not work for twisted polygons.
19 *
20 * ellson@graphviz.org October 2nd, 1996
21 */
22
23 #include <pathutil.h>
24
make_CW(Ppoly_t * poly)25 void make_CW(Ppoly_t * poly)
26 {
27 int i, j, n;
28 Ppoint_t *P;
29 Ppoint_t tP;
30 double area = 0.0;
31
32 P = poly->ps;
33 n = poly->pn;
34 /* points or lines don't have a rotation */
35 if (n > 2) {
36 /* check CW or CCW by computing (twice the) area of poly */
37 for (i = 1; i < n - 1; i++) {
38 area += area2(P[0], P[i + 1], P[i]);
39 }
40 /* if the area is -ve then the rotation needs to be reversed */
41 /* the starting point is left unchanged */
42 if (area < 0.0) {
43 for (i = 1, j = n - 1; i < 1 + n / 2; i++, j--) {
44 tP = P[i];
45 P[i] = P[j];
46 P[j] = tP;
47 }
48 }
49 }
50 }
51