fork download
  1. // C++ program to implement Cohen Sutherland algorithm
  2. // for line clipping.
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // Defining region codes
  7. const int INSIDE = 0; // 0000
  8. const int LEFT = 1; // 0001
  9. const int RIGHT = 2; // 0010
  10. const int BOTTOM = 4; // 0100
  11. const int TOP = 8; // 1000
  12.  
  13. // Defining x_max, y_max and x_min, y_min for
  14. // clipping rectangle. Since diagonal points are
  15. // enough to define a rectangle
  16. const int x_max = 10;
  17. const int y_max = 8;
  18. const int x_min = 4;
  19. const int y_min = 4;
  20.  
  21. // Function to compute region code for a point(x, y)
  22. int computeCode(double x, double y)
  23. {
  24. // initialized as being inside
  25. int code = INSIDE;
  26.  
  27. if (x < x_min) // to the left of rectangle
  28. code |= LEFT;
  29. else if (x > x_max) // to the right of rectangle
  30. code |= RIGHT;
  31. if (y < y_min) // below the rectangle
  32. code |= BOTTOM;
  33. else if (y > y_max) // above the rectangle
  34. code |= TOP;
  35.  
  36. return code;
  37. }
  38.  
  39. // Implementing Cohen-Sutherland algorithm
  40. // Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)
  41. void cohenSutherlandClip(double x1, double y1,
  42. double x2, double y2)
  43. {
  44. // Compute region codes for P1, P2
  45. int code1 = computeCode(x1, y1);
  46. int code2 = computeCode(x2, y2);
  47.  
  48. // Initialize line as outside the rectangular window
  49. bool accept = false;
  50.  
  51. while (true) {
  52. if ((code1 == 0) && (code2 == 0)) {
  53. // If both endpoints lie within rectangle
  54. accept = true;
  55. break;
  56. }
  57. else if (code1 & code2) {
  58. // If both endpoints are outside rectangle,
  59. // in same region
  60. break;
  61. }
  62. else {
  63. // Some segment of line lies within the
  64. // rectangle
  65. int code_out;
  66. double x, y;
  67.  
  68. // At least one endpoint is outside the
  69. // rectangle, pick it.
  70. if (code1 != 0)
  71. code_out = code1;
  72. else
  73. code_out = code2;
  74.  
  75. // Find intersection point;
  76. // using formulas y = y1 + slope * (x - x1),
  77. // x = x1 + (1 / slope) * (y - y1)
  78. if (code_out & TOP) {
  79. // point is above the clip rectangle
  80. x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
  81. y = y_max;
  82. }
  83. else if (code_out & BOTTOM) {
  84. // point is below the rectangle
  85. x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
  86. y = y_min;
  87. }
  88. else if (code_out & RIGHT) {
  89. // point is to the right of rectangle
  90. y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
  91. x = x_max;
  92. }
  93. else if (code_out & LEFT) {
  94. // point is to the left of rectangle
  95. y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
  96. x = x_min;
  97. }
  98.  
  99. // Now intersection point x, y is found
  100. // We replace point outside rectangle
  101. // by intersection point
  102. if (code_out == code1) {
  103. x1 = x;
  104. y1 = y;
  105. code1 = computeCode(x1, y1);
  106. }
  107. else {
  108. x2 = x;
  109. y2 = y;
  110. code2 = computeCode(x2, y2);
  111. }
  112. }
  113. }
  114. if (accept) {
  115. cout << "Line accepted from " << x1 << ", "
  116. << y1 << " to " << x2 << ", " << y2 << endl;
  117. // Here the user can add code to display the rectangle
  118. // along with the accepted (portion of) lines
  119. }
  120. else
  121. cout << "Line rejected" << endl;
  122. }
  123.  
  124. // Driver code
  125. int main()
  126. {
  127. // First Line segment
  128. // P11 = (5, 5), P12 = (7, 7)
  129. cohenSutherlandClip(5, 5, 7, 7);
  130.  
  131. // Second Line segment
  132. // P21 = (7, 9), P22 = (11, 4)
  133. cohenSutherlandClip(7, 9, 11, 4);
  134.  
  135. // Third Line segment
  136. // P31 = (1, 5), P32 = (4, 1)
  137. cohenSutherlandClip(1, 5, 4, 1);
  138.  
  139. return 0;
  140. }
  141.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Line accepted from 5, 5 to 7, 7
Line accepted from 7.8, 8 to 10, 5.25
Line rejected