g_clipper.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. #include "sys-defines.h"
  16. #include "extern.h"
  17. /* bit fields for return value from Cohen-Sutherland clipper */
  18. enum { ACCEPTED = 0x1, CLIPPED_FIRST = 0x2, CLIPPED_SECOND = 0x4 };
  19. /* for internal clipper use */
  20. enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
  21. /* forward references */
  22. static int compute_outcode (double x, double y, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip);
  23. /* _clip_line() takes two points, the endpoints of a line segment in the
  24. * device frame (expressed in terms of floating-point device coordinates),
  25. * and destructively passes back two points: the endpoints of the line
  26. * segment clipped by Cohen-Sutherland to the rectangular clipping area.
  27. * The return value contains bitfields ACCEPTED, CLIPPED_FIRST, and
  28. * CLIPPED_SECOND.
  29. */
  30. int
  31. _clip_line (double *x0_p, double *y0_p, double *x1_p, double *y1_p, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip)
  32. {
  33. double x0 = *x0_p;
  34. double y0 = *y0_p;
  35. double x1 = *x1_p;
  36. double y1 = *y1_p;
  37. int outcode0, outcode1;
  38. bool accepted;
  39. int clipval = 0;
  40. outcode0 = compute_outcode (x0, y0, x_min_clip, x_max_clip, y_min_clip, y_max_clip);
  41. outcode1 = compute_outcode (x1, y1, x_min_clip, x_max_clip, y_min_clip, y_max_clip);
  42. for ( ; ; )
  43. {
  44. if (!(outcode0 | outcode1)) /* accept */
  45. {
  46. accepted = true;
  47. break;
  48. }
  49. else if (outcode0 & outcode1) /* reject */
  50. {
  51. accepted = false;
  52. break;
  53. }
  54. else
  55. {
  56. /* at least one endpoint is outside; choose one that is */
  57. int outcode_out = (outcode0 ? outcode0 : outcode1);
  58. double x, y; /* intersection with clip edge */
  59. if (outcode_out & RIGHT)
  60. {
  61. x = x_max_clip;
  62. y = y0 + (y1 - y0) * (x_max_clip - x0) / (x1 - x0);
  63. }
  64. else if (outcode_out & LEFT)
  65. {
  66. x = x_min_clip;
  67. y = y0 + (y1 - y0) * (x_min_clip - x0) / (x1 - x0);
  68. }
  69. else if (outcode_out & TOP)
  70. {
  71. x = x0 + (x1 - x0) * (y_max_clip - y0) / (y1 - y0);
  72. y = y_max_clip;
  73. }
  74. else
  75. {
  76. x = x0 + (x1 - x0) * (y_min_clip - y0) / (y1 - y0);
  77. y = y_min_clip;
  78. }
  79. if (outcode_out == outcode0)
  80. {
  81. x0 = x;
  82. y0 = y;
  83. outcode0 = compute_outcode (x0, y0, x_min_clip, x_max_clip, y_min_clip, y_max_clip);
  84. }
  85. else
  86. {
  87. x1 = x;
  88. y1 = y;
  89. outcode1 = compute_outcode (x1, y1, x_min_clip, x_max_clip, y_min_clip, y_max_clip);
  90. }
  91. }
  92. }
  93. if (accepted)
  94. {
  95. clipval |= ACCEPTED;
  96. if ((x0 != *x0_p) || (y0 != *y0_p))
  97. clipval |= CLIPPED_FIRST;
  98. if ((x1 != *x1_p) || (y1 != *y1_p))
  99. clipval |= CLIPPED_SECOND;
  100. *x0_p = x0;
  101. *y0_p = y0;
  102. *x1_p = x1;
  103. *y1_p = y1;
  104. }
  105. return clipval;
  106. }
  107. static int
  108. compute_outcode (double x, double y, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip)
  109. {
  110. int code = 0;
  111. if (x > x_max_clip)
  112. code |= RIGHT;
  113. else if (x < x_min_clip)
  114. code |= LEFT;
  115. if (y > y_max_clip)
  116. code |= TOP;
  117. else if (y < y_min_clip)
  118. code |= BOTTOM;
  119. return code;
  120. }