geometry.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include <neatogen/geometry.h>
  11. #include <math.h>
  12. #include <stddef.h>
  13. double xmin, xmax, ymin, ymax; /* min and max x and y values of sites */
  14. double deltax; // xmax - xmin
  15. size_t nsites;
  16. int sqrt_nsites;
  17. void geominit(void)
  18. {
  19. double sn;
  20. sn = nsites + 4;
  21. sqrt_nsites = (int) sqrt(sn);
  22. }
  23. double dist_2(Point pp, Point qp) {
  24. const double dx = pp.x - qp.x;
  25. const double dy = pp.y - qp.y;
  26. return (dx * dx + dy * dy);
  27. }
  28. void subpt(Point * a, Point b, Point c)
  29. {
  30. a->x = b.x - c.x;
  31. a->y = b.y - c.y;
  32. }
  33. void addpt(Point * c, Point a, Point b)
  34. {
  35. c->x = a.x + b.x;
  36. c->y = a.y + b.y;
  37. }
  38. double area_2(Point a, Point b, Point c)
  39. {
  40. return ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x));
  41. }
  42. int leftOf(Point a, Point b, Point c)
  43. {
  44. return (area_2(a, b, c) > 0);
  45. }
  46. int intersection(Point a, Point b, Point c, Point d, Point * p)
  47. {
  48. double s, t; /* The two parameters of the parametric eqns. */
  49. double denom; /* Denominator of solutions. */
  50. denom =
  51. a.x * (d.y - c.y) +
  52. b.x * (c.y - d.y) + d.x * (b.y - a.y) + c.x * (a.y - b.y);
  53. /* If denom is zero, then the line segments are parallel. */
  54. /* In this case, return false even though the segments might overlap. */
  55. if (denom == 0.0)
  56. return 0;
  57. s = (a.x * (d.y - c.y) + c.x * (a.y - d.y) + d.x * (c.y - a.y)
  58. ) / denom;
  59. t = -(a.x * (c.y - b.y) + b.x * (a.y - c.y) + c.x * (b.y - a.y)
  60. ) / denom;
  61. p->x = a.x + s * (b.x - a.x);
  62. p->y = a.y + s * (b.y - a.y);
  63. if ((0.0 <= s) && (s <= 1.0) && (0.0 <= t) && (t <= 1.0))
  64. return 1;
  65. else
  66. return 0;
  67. }