inpoly.c 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. * in_poly
  12. *
  13. * Test if a point is inside a polygon.
  14. * The polygon must be convex with vertices in CW order.
  15. */
  16. #include <pathplan/pathutil.h>
  17. #include <pathplan/vispath.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. bool in_poly(const Ppoly_t poly, Ppoint_t q) {
  22. const Ppoint_t *P = poly.ps;
  23. const size_t n = poly.pn;
  24. for (size_t i = 0; i < n; i++) {
  25. const size_t i1 = (i + n - 1) % n; // point index; i1 = i-1 mod n
  26. if (wind(P[i1], P[i], q) == 1)
  27. return false;
  28. }
  29. return true;
  30. }