CLIP.H 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/2d/rcs/clip.h $
  15. * $Revision: 1.3 $
  16. * $Author: john $
  17. * $Date: 1993/10/15 16:22:28 $
  18. *
  19. * Macros used for clipping
  20. *
  21. * $Log: clip.h $
  22. * Revision 1.3 1993/10/15 16:22:28 john
  23. * *** empty log message ***
  24. *
  25. * Revision 1.2 1993/09/29 16:14:31 john
  26. * made work with fix point
  27. *
  28. * Revision 1.1 1993/09/08 11:40:53 john
  29. * Initial revision
  30. *
  31. *
  32. */
  33. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  34. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  35. #define IABS(x) (((x) < 0) ? -(x) : (x))
  36. #define EXCHG(a,b) do { \
  37. int __temp__ = (a); \
  38. (a) = (b); \
  39. (b) = __temp__; \
  40. } while(0)
  41. // sort two values
  42. #define SORT2(a,b) do { \
  43. if((a) > (b)) EXCHG(a,b); \
  44. } while(0)
  45. # define SCALE(var,arg,num,den) \
  46. ((var) = ((arg) * (num)) / (den))
  47. # define USCALE(var,arg,num,den) \
  48. ((var) = ((unsigned)(arg) * (unsigned)(num)) / (unsigned)(den))
  49. # define FSCALE(var,arg,num,den) ((var) = fixmuldiv((arg),(num),(den)))
  50. #define CLIPDOT(xx,yy,WHEN_OUTSIDE,WHEN_CLIPPED) { \
  51. if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; } \
  52. if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; } \
  53. }
  54. #define CLIPHLINE(x1,x2,yy,WHEN_OUTSIDE,WHEN_CLIPPED) { \
  55. if(x1 > x2) EXCHG(x1,x2); \
  56. if((x1 > XMAX) || (x2 < XMIN)) { WHEN_OUTSIDE; } \
  57. if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; } \
  58. if(x1 < XMIN) { x1 = XMIN; WHEN_CLIPPED; } \
  59. if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; } \
  60. }
  61. #define CLIPVLINE(xx,y1,y2,WHEN_OUTSIDE,WHEN_CLIPPED) { \
  62. if(y1 > y2) EXCHG(y1,y2); \
  63. if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; } \
  64. if((y1 > YMAX) || (y2 < YMIN)) { WHEN_OUTSIDE; } \
  65. if(y1 < YMIN) { y1 = YMIN; WHEN_CLIPPED; } \
  66. if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; } \
  67. }
  68. #define CLIPBOX(x1,y1,x2,y2,WHEN_OUTSIDE,WHEN_CLIPPED) { \
  69. if(x1 > x2) EXCHG(x1,x2); \
  70. if(y1 > y2) EXCHG(y1,y2); \
  71. if((x1 > XMAX) || (x2 < 0)) { WHEN_OUTSIDE; } \
  72. if((y1 > YMAX) || (y2 < 0)) { WHEN_OUTSIDE; } \
  73. if(x1 < 0) { x1 = 0; WHEN_CLIPPED; } \
  74. if(y1 < 0) { y1 = 0; WHEN_CLIPPED; } \
  75. if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; } \
  76. if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; } \
  77. }
  78. #define CLIPLINE(x1,y1,x2,y2,XMIN,YMIN,XMAX,YMAX,WHEN_OUTSIDE,WHEN_CLIPPED,MY_SCALE) do { \
  79. register int temp; \
  80. \
  81. if(y1 > y2) \
  82. { EXCHG(y1,y2); EXCHG(x1,x2); } \
  83. if((y2 < YMIN) || (y1 > YMAX)) \
  84. { WHEN_OUTSIDE; } \
  85. if(x1 < x2) { \
  86. if((x2 < XMIN) || (x1 > XMAX)) { \
  87. WHEN_OUTSIDE; \
  88. } \
  89. if(x1 < XMIN) { \
  90. MY_SCALE(temp,(y2 - y1),(XMIN - x1),(x2 - x1)); \
  91. if((y1 += temp) > YMAX) { WHEN_OUTSIDE; } \
  92. x1 = XMIN; \
  93. WHEN_CLIPPED; \
  94. } \
  95. if(x2 > XMAX) { \
  96. MY_SCALE(temp,(y2 - y1),(x2 - XMAX),(x2 - x1)); \
  97. if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; } \
  98. x2 = XMAX; \
  99. WHEN_CLIPPED; \
  100. } \
  101. if(y1 < YMIN) { \
  102. MY_SCALE(temp,(x2 - x1),(YMIN - y1),(y2 - y1)); \
  103. x1 += temp; \
  104. y1 = YMIN; \
  105. WHEN_CLIPPED; \
  106. } \
  107. if(y2 > YMAX) { \
  108. MY_SCALE(temp,(x2 - x1),(y2 - YMAX),(y2 - y1)); \
  109. x2 -= temp; \
  110. y2 = YMAX; \
  111. WHEN_CLIPPED; \
  112. } \
  113. } \
  114. else { \
  115. if((x1 < XMIN) || (x2 > XMAX)) { \
  116. WHEN_OUTSIDE; \
  117. } \
  118. if(x1 > XMAX) { \
  119. MY_SCALE(temp,(y2 - y1),(x1 - XMAX),(x1 - x2)); \
  120. if((y1 += temp) > YMAX) { WHEN_OUTSIDE; } \
  121. x1 = XMAX; \
  122. WHEN_CLIPPED; \
  123. } \
  124. if(x2 < XMIN) { \
  125. MY_SCALE(temp,(y2 - y1),(XMIN - x2),(x1 - x2)); \
  126. if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; } \
  127. x2 = XMIN; \
  128. WHEN_CLIPPED; \
  129. } \
  130. if(y1 < YMIN) { \
  131. MY_SCALE(temp,(x1 - x2),(YMIN - y1),(y2 - y1)); \
  132. x1 -= temp; \
  133. y1 = YMIN; \
  134. WHEN_CLIPPED; \
  135. } \
  136. if(y2 > YMAX) { \
  137. MY_SCALE(temp,(x1 - x2),(y2 - YMAX),(y2 - y1)); \
  138. x2 += temp; \
  139. y2 = YMAX; \
  140. WHEN_CLIPPED; \
  141. } \
  142. } \
  143. } while(0)