Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. std::vector<Point> Core::clipedge(vector<Point> polygon, int x0, int y0, int x1, int y1){
  2. std::vector<Point>temp;
  3. temp.push_back(Point(0,0));
  4. temp.push_back(Point(0,0));
  5. temp.push_back(Point(0,0));
  6. std::vector<Point> re;
  7. re=temp;
  8. return re;
  9. }
  10.  
  11. class Point{
  12. public:
  13. int x;
  14. int y;
  15. Uint8 r;
  16. Uint8 g;
  17. Uint8 b;
  18. Point(int x, int y, Uint8 r = 255, Uint8 g =255, Uint8 b =255) : x(x), y(y), r(r), g(g), b(b) {}
  19. };
  20.  
  21. /////////////////////////////////////
  22. // Clips the triangle against the screen boundary.
  23. std::vector<Point> Core::clip(std::vector<Point> polygon)
  24. {
  25. // We can only work with at least a triangle.
  26. if (polygon.size() < 3)
  27. return polygon;
  28.  
  29. // The points p0(x0,y0) and p1(x1,y1) are ordered in clipedge such that any points
  30. // to the right of the line p0-p1 are inside and vice versa
  31. polygon = clipedge(polygon, 0, 0, width-1, 0); // Top edge
  32. polygon = clipedge(polygon, 0, height-1, 0, 0); // Left edge
  33. polygon = clipedge(polygon, width-1, height-1, 0, height-1); // Bottom edge
  34. polygon = clipedge(polygon, width-1, 0, width-1, height-1); // Right edge
  35.  
  36. return polygon;
  37. }
  38.  
  39. ///////////////////////////////////////////////////////////
  40. // Clips for any line defined by P0(x0, y0), P1(x1, y1).
  41.  
  42. vector<Point> Core::clipedge(vector<Point> polygon, int x0, int y0, int x1, int y1)
  43. {
  44. //color random for this stage
  45. int r=rand()%255;
  46. int g=rand()%255;
  47. int b=rand()%255;
  48. // For each side of the polygon, check the 4 cases of sutherland-hodgman.
  49. // Creating a temporary buffer to hold your new set of vertices for the clipped polygon.
  50. vector<Point>temp;
  51. temp.push_back(Point(1,1));
  52.  
  53. int size = (int)polygon.size();
  54. Point p1 (0, 0);
  55. for (int i = 0; i < size; ++i) {
  56. // Points we're testing
  57. Point p0 = polygon[i];
  58. p1 = polygon[(i+1)%size]; // The last point is the same as the first point
  59.  
  60. // Testing sides
  61. // Assumes the right hand side of the line being inside.
  62. int p0_side = testing_side(p0.x, p0.y, x0, y0, x1, y1);
  63. int p1_side = testing_side(p1.x, p1.y, x0, y0, x1, y1);
  64. if (x0-x1==0){
  65. if (p0_side<0){
  66. if (p1_side<0){
  67. //case 1 two point out igone
  68. }
  69. else if(p1_side>=0){
  70. //case 2 out-to-in interception pt and end pt
  71. //line equaltion
  72. //line equaltion
  73. int k=(p1.y-p0.y)/(p1.x-p0.x);
  74. int nb=p0.y-(k*p0.x);
  75. int ny=(int)(k*x0)+nb;
  76.  
  77. temp.push_back(Point(x0,ny,r,g,b));
  78. temp.push_back(p1);
  79. }
  80. }else if(p0_side>=0){
  81. if (p1_side<0){
  82. //case 3 in-to-out interception pt
  83. //line equaltion
  84. int k=(y0-y1)*(x1-x0);
  85. k=k+(y1*x0)-(y0*x0);
  86. int nx=(int)k/(y1-y0);
  87. temp.push_back(Point(nx,y0,r,g,b));
  88. }
  89. else if (p1_side>=0){
  90. //case 4 in-in end pt
  91. temp.push_back(p1);
  92. }
  93. }
  94. }
  95. else if(y0-y1==0){
  96. if (p0_side<0){
  97. if (p1_side<0){
  98. //case 1 two point out igone
  99. }
  100. else if(p1_side>=0){
  101. //case 2 out-to-in interception pt and end pt
  102. //line equaltion
  103. int k=(y0-y1)*(x1-x0);
  104. k=k+(y1*x0)-(y0*x0);
  105. int nx=(int)k/(y1-y0);
  106.  
  107. temp.push_back(Point(nx,y0,r,g,b));
  108. temp.push_back(p1);
  109. }
  110. }else if(p0_side>=0){
  111. if (p1_side<0){
  112. //case 3 in-to-out interception pt
  113. //line equaltion
  114. int k=(p1.y-p0.y)/(p1.x-p0.x);
  115. int nb=p0.y-(k*p0.x);
  116. int nx=(int)(y0-nb)/k;
  117. temp.push_back(Point(nx,y0,r,g,b));
  118. }
  119. else if (p1_side>=0){
  120. //case 4 in-in end pt
  121. temp.push_back(p1);
  122. }
  123. }
  124. }
  125. //get interception pt
  126. else{
  127. //equaltion of line for the intersection point
  128. int k1=(y1 - y0) / (x1 - x0);
  129. int b1=y0 - k1*x0;
  130. int k2=(p1.y-p0.y)/(p1.x-p0.x);
  131. int b2=p0.y - k2*p0.x;
  132. if(k1==k2){
  133. k2=k1+1;
  134. }
  135. int in_x=(int)(b2-b1)/(k2-k1);
  136. int in_y=(int)k1*in_x+b1;
  137. // Consider the 4 cases of SutherlandñHodgman clipping here.
  138.  
  139. if (p0_side<0){
  140. if (p1_side<0){
  141. //case 1 two point out igone
  142. }
  143. else if(p1_side>=0){
  144. //case 2 out-to-in interception pt and end pt
  145. temp.push_back(Point(in_x,in_y,r,g,b));
  146. temp.push_back(p1);
  147. }
  148. }else if(p0_side>=0){
  149. if (p1_side<0){
  150. //case 3 in-to-out interception pt
  151. temp.push_back(Point(in_x,in_y,r,g,b));
  152. }
  153. else if (p1_side>=0){
  154. //case 4 in-in end pt
  155. temp.push_back(p1);
  156. }
  157. }
  158. // Remember to calculate the color for any intersection points
  159. //not done yet
  160.  
  161. }
  162. }
  163. return temp;
  164. }
  165.  
  166. /////////////////////////////////////
  167. // Tests if the point (x, y) lies on the inside or outside of the line P0(x0, y0),
  168. // P1(x1, y1). Positive: inside. Negative: outside. Zero: on the line.
  169. int Core::testing_side(int x, int y, int x0, int y0, int x1, int y1)
  170. {
  171. //// Your code here
  172. //for vertical line
  173. if (x1-x0==0){
  174. if(x<x1){
  175. return -1;
  176. }
  177. else if(x==x1){
  178. return 0;
  179. }
  180. else{
  181. return 1;
  182. }
  183. }
  184. else if (y1-y0==0){
  185. // for horizontal line
  186. if (y<y1){
  187. return -1;
  188. }else if (y==0){
  189. return 0;
  190. }
  191. else{
  192. return 1;
  193. }
  194.  
  195. }
  196. else{
  197. float k=(y1 - y0) / (x1 - x0);
  198. float b=y1 - k*x1;
  199. float spot=(x*k) +b;
  200. if (spot==y){
  201. return 0;
  202. }else if (spot<y) {
  203. return -1;
  204. }else{
  205. return 1;
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement