Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include<graphics.h>
  3. using namespace std;
  4.  
  5. // Defining region codes
  6. const int INSIDE = 0;
  7. const int LEFT = 1;
  8. const int RIGHT = 2;
  9. const int BOTTOM = 4;
  10. const int TOP = 8;
  11.  
  12. int x_max;
  13. int y_max;
  14. int x_min;
  15. int y_min;
  16. int computeCode(double x, double y)
  17. {
  18. int code = INSIDE;
  19.  
  20. if (x < x_min)
  21. code |= LEFT;
  22. else if (x > x_max)
  23. code |= RIGHT;
  24. if (y < y_min)
  25. code |= BOTTOM;
  26. else if (y > y_max)
  27. code |= TOP;
  28.  
  29. return code;
  30. }
  31. void drawwindow()
  32. {
  33.  
  34. line(x_min,y_min,x_max,y_min);
  35. line(x_min,y_min,x_min,y_max);
  36. line(x_max,y_min,x_max,y_max);
  37. line(x_min,y_max,x_max,y_max);
  38. }
  39. void Clip(double x1, double y1, double x2, double y2)
  40. {
  41.  
  42. int gd=DETECT,v,gm;
  43. initgraph(&gd,&gm,"");
  44. drawwindow();
  45.  
  46. int code1 = computeCode(x1, y1);
  47.  
  48. int code2 = computeCode(x2, y2);
  49. if(code1==0&&code2==0)
  50. {
  51. cout<<"IN The Window"<<endl;
  52. line(x1,y1,x2,y2);
  53. }
  54.  
  55. bool accept = false;
  56.  
  57. while (true)
  58. {
  59. if ((code1 == 0) && (code2 == 0))
  60. {
  61. accept = true;
  62. break;
  63. }
  64. else if (code1 & code2)
  65. {
  66. break;
  67. }
  68. else
  69. {
  70. int code_out;
  71. double x, y;
  72. if (code1 != 0)
  73. code_out = code1;
  74. else
  75. code_out = code2;
  76. if (code_out & TOP)
  77. {
  78. x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
  79. y = y_max;
  80. }
  81. else if (code_out & BOTTOM)
  82. {
  83. x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
  84. y = y_min;
  85. }
  86. else if (code_out & RIGHT)
  87. {
  88. y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
  89. x = x_max;
  90. }
  91. else if (code_out & LEFT)
  92. {
  93. y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
  94. x = x_min;
  95. }
  96. if (code_out == code1)
  97. {
  98. x1 = x;
  99. y1 = y;
  100. code1 = computeCode(x1, y1);
  101. }
  102. else
  103. {
  104. x2 = x;
  105. y2 = y;
  106. code2 = computeCode(x2, y2);
  107. }
  108. }
  109. }
  110. if (accept)
  111. {
  112. cout <<"Clipping Candidate" <<endl;
  113. line(x1,y1,x2,y2);
  114. }
  115. else
  116. {
  117. cout<<"Out of the window"<<endl;
  118. }
  119.  
  120. }
  121.  
  122. // Driver code
  123. int main()
  124. {
  125. cout<<"ENTER XMIN YMIN :"<<endl;
  126. cin>>x_min>>y_min;
  127. cout<<"ENTER XMAX YMAX :"<<endl;
  128. cin>>x_max>>y_max;
  129. cout<<"Enter No of line: "<<endl;
  130. int n;
  131. cin>>n;
  132. for(int i=1;i<=n;i++)
  133. {
  134. int a,b,c,d;
  135. cout<<"Enter first Point X and Y value :";
  136. cin>>a>>b;
  137. cout<<"Enter Second Point X and Y value :";
  138. cin>>c>>d;
  139.  
  140. Clip(a, b, c, d);
  141. delay(5000);
  142. closegraph();
  143.  
  144. }
  145.  
  146.  
  147.  
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement