makecw.c 1.3 KB

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