An LGPL/GPL-licensed artwork library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

art_svp_point.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Libart_LGPL - library of basic graphic primitives
  2. * Copyright (C) 1999 Raph Levien
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #include "config.h"
  20. #include "art_svp_point.h"
  21. #include <math.h>
  22. #include "art_misc.h"
  23. #include "art_svp.h"
  24. /* Determine whether a point is inside, or near, an svp. */
  25. /* return winding number of point wrt svp */
  26. /**
  27. * art_svp_point_wind: Determine winding number of a point with respect to svp.
  28. * @svp: The svp.
  29. * @x: The X coordinate of the point.
  30. * @y: The Y coordinate of the point.
  31. *
  32. * Determine the winding number of the point @x, @y with respect to @svp.
  33. *
  34. * Return value: the winding number.
  35. **/
  36. int
  37. art_svp_point_wind (ArtSVP *svp, double x, double y)
  38. {
  39. int i, j;
  40. int wind = 0;
  41. for (i = 0; i < svp->n_segs; i++)
  42. {
  43. ArtSVPSeg *seg = &svp->segs[i];
  44. if (seg->bbox.y0 > y)
  45. break;
  46. if (seg->bbox.y1 > y)
  47. {
  48. if (seg->bbox.x1 < x)
  49. wind += seg->dir ? 1 : -1;
  50. else if (seg->bbox.x0 <= x)
  51. {
  52. double x0, y0, x1, y1, dx, dy;
  53. for (j = 0; j < seg->n_points - 1; j++)
  54. {
  55. if (seg->points[j + 1].y > y)
  56. break;
  57. }
  58. x0 = seg->points[j].x;
  59. y0 = seg->points[j].y;
  60. x1 = seg->points[j + 1].x;
  61. y1 = seg->points[j + 1].y;
  62. dx = x1 - x0;
  63. dy = y1 - y0;
  64. if ((x - x0) * dy > (y - y0) * dx)
  65. wind += seg->dir ? 1 : -1;
  66. }
  67. }
  68. }
  69. return wind;
  70. }
  71. /**
  72. * art_svp_point_dist: Determine distance between point and svp.
  73. * @svp: The svp.
  74. * @x: The X coordinate of the point.
  75. * @y: The Y coordinate of the point.
  76. *
  77. * Determines the distance of the point @x, @y to the closest edge in
  78. * @svp. A large number is returned if @svp is empty.
  79. *
  80. * Return value: the distance.
  81. **/
  82. double
  83. art_svp_point_dist (ArtSVP *svp, double x, double y)
  84. {
  85. int i, j;
  86. double dist_sq;
  87. double best_sq = -1;
  88. for (i = 0; i < svp->n_segs; i++)
  89. {
  90. ArtSVPSeg *seg = &svp->segs[i];
  91. for (j = 0; j < seg->n_points - 1; j++)
  92. {
  93. double x0 = seg->points[j].x;
  94. double y0 = seg->points[j].y;
  95. double x1 = seg->points[j + 1].x;
  96. double y1 = seg->points[j + 1].y;
  97. double dx = x1 - x0;
  98. double dy = y1 - y0;
  99. double dxx0 = x - x0;
  100. double dyy0 = y - y0;
  101. double dot = dxx0 * dx + dyy0 * dy;
  102. if (dot < 0)
  103. dist_sq = dxx0 * dxx0 + dyy0 * dyy0;
  104. else
  105. {
  106. double rr = dx * dx + dy * dy;
  107. if (dot > rr)
  108. dist_sq = (x - x1) * (x - x1) + (y - y1) * (y - y1);
  109. else
  110. {
  111. double perp = (y - y0) * dx - (x - x0) * dy;
  112. dist_sq = perp * perp / rr;
  113. }
  114. }
  115. if (best_sq < 0 || dist_sq < best_sq)
  116. best_sq = dist_sq;
  117. }
  118. }
  119. if (best_sq >= 0)
  120. return sqrt (best_sq);
  121. else
  122. return 1e12;
  123. }