Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package galpr;
  2.  
  3. public class CohenSutherland {
  4. final int INSIDE = 0; // 0000
  5. final int LEFT = 1; // 0001
  6. final int RIGHT = 2; // 0010
  7. final int BOTTOM = 4; // 0100
  8. final int TOP = 8; // 1000
  9.  
  10. private int xmin;
  11. private int ymin;
  12. private int xmax;
  13. private int ymax;
  14.  
  15. public CohenSutherland(int xmin, int ymin, int xmax, int ymax) {
  16. this.xmin = xmin;
  17. this.ymin = ymin;
  18. this.xmax = xmax;
  19. this.ymax = ymax;
  20. }
  21.  
  22.  
  23.  
  24. // Compute the bit code for a point (x, y) using the clip rectangle
  25. // bounded diagonally by (xmin, ymin), and (xmax, ymax)
  26.  
  27. // ASSUME THAT xmax, xmin, ymax and ymin are global constants.
  28.  
  29. int ComputeOutCode(double x, double y)
  30. {
  31. int code;
  32.  
  33. code = INSIDE; // initialised as being inside of [[clip window]]
  34.  
  35. if (x < xmin) // to the left of clip window
  36. code |= LEFT;
  37. else if (x > xmax) // to the right of clip window
  38. code |= RIGHT;
  39. if (y < ymin) // below the clip window
  40. code |= BOTTOM;
  41. else if (y > ymax) // above the clip window
  42. code |= TOP;
  43.  
  44. return code;
  45. }
  46.  
  47. // Cohen–Sutherland clipping algorithm clips a line from
  48. // P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with
  49. // diagonal from (xmin, ymin) to (xmax, ymax).
  50. public void CohenSutherlandLineClipAndDraw(double x0, double y0, double x1, double y1, G_Graphics g)
  51. {
  52. // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle
  53. int outcode0 = ComputeOutCode(x0, y0);
  54. int outcode1 = ComputeOutCode(x1, y1);
  55. boolean accept = false;
  56.  
  57. while (true) {
  58. if (!((outcode0 | outcode1) > 0)) {
  59. // bitwise OR is 0: both points inside window; trivially accept and exit loop
  60. accept = true;
  61. break;
  62. } else if ((outcode0 & outcode1) > 0) {
  63. // bitwise AND is not 0: both points share an outside zone (LEFT, RIGHT, TOP,
  64. // or BOTTOM), so both must be outside window; exit loop (accept is false)
  65. break;
  66. } else {
  67. // failed both tests, so calculate the line segment to clip
  68. // from an outside point to an intersection with clip edge
  69. double x, y;
  70.  
  71. // At least one endpoint is outside the clip rectangle; pick it.
  72. int outcodeOut = (outcode0 > 0) ? outcode0 : outcode1;
  73.  
  74. // Now find the intersection point;
  75. // use formulas:
  76. // slope = (y1 - y0) / (x1 - x0)
  77. // x = x0 + (1 / slope) * (ym - y0), where ym is ymin or ymax
  78. // y = y0 + slope * (xm - x0), where xm is xmin or xmax
  79. // No need to worry about divide-by-zero because, in each case, the
  80. // outcode bit being tested guarantees the denominator is non-zero
  81. if ((outcodeOut & TOP) > 0) { // point is above the clip window
  82. x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
  83. y = ymax;
  84. } else if ((outcodeOut & BOTTOM) > 0) { // point is below the clip window
  85. x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
  86. y = ymin;
  87. } else if ((outcodeOut & RIGHT) > 0) { // point is to the right of clip window
  88. y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
  89. x = xmax;
  90. } else if ((outcodeOut & LEFT) > 0) { // point is to the left of clip window
  91. y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
  92. x = xmin;
  93. }else{
  94. x = 0;//wtf java logic
  95. y = 0;
  96. }
  97.  
  98. // Now we move outside point to intersection point to clip
  99. // and get ready for next pass.
  100. if (outcodeOut == outcode0) {
  101. x0 = x;
  102. y0 = y;
  103. outcode0 = ComputeOutCode(x0, y0);
  104. } else {
  105. x1 = x;
  106. y1 = y;
  107. outcode1 = ComputeOutCode(x1, y1);
  108. }
  109. }
  110. }
  111. if (accept) {
  112. // Following functions are left for implementation by user based on
  113. // their platform (OpenGL/graphics.h etc.)
  114. //DrawRectangle(xmin, ymin, xmax, ymax);
  115. //LineSegment(x0, y0, x1, y1);
  116. g.StrokeRect(xmin, ymin, xmax-xmin, ymax-ymin);
  117. g.DDA((int)x0, (int)y0, (int)x1, (int)y1);
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement