Guest User

Untitled

a guest
Jun 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. void DrawPolygon(POLYGON *p )
  2. {
  3.     // Your code here
  4. int i;
  5. int top_vertex = 0;
  6. int current_left;
  7. int current_right;
  8. int next_left;
  9. int next_right;
  10. float x_start;
  11. float x_end;
  12. float y_start;
  13. float y_end;
  14. float slope_right;
  15. float slope_left;
  16. int y_top;
  17. int y_bottom;
  18.  
  19. float x_slope; //slope for x
  20. float y_slope; //slope for y
  21.  
  22. bool change_left;
  23.  
  24. top_vertex = 0;
  25.  
  26. for(i = 1; i < p->nv; i++)
  27. {
  28.     if(p->vert[i].y < p->vert[top_vertex].y)
  29.     {
  30.         top_vertex = i;
  31.     }
  32. }
  33.  
  34. current_left = top_vertex;
  35. current_right = top_vertex;
  36.  
  37. next_left = (current_left - 1 + p->nv) % p->nv;
  38. next_right = (current_right + 1) % p->nv;
  39.  
  40. x_slope = p->vert[current_left].x - p->vert[next_left].x;
  41. y_slope = p->vert[current_left].y - p->vert[next_left].y;
  42.  
  43. if(x_slope != y_slope)
  44. {
  45.     slope_left = x_slope/y_slope;
  46. }
  47.  
  48. x_slope = p->vert[current_right].x - p->vert[next_right].x;
  49. y_slope = p->vert[current_right].y - p->vert[next_right].y;
  50.  
  51. if(x_slope != y_slope)
  52. {
  53.     slope_right = x_slope/y_slope;
  54. }
  55.  
  56. if(p->vert[next_left].y < p->vert[next_right].y)
  57. {
  58.     y_bottom = p->vert[next_right].y;
  59.     change_left = false;
  60. }
  61. else
  62. {
  63.     y_bottom = p->vert[next_left].y;
  64.     change_left = true;
  65. }
  66.  
  67. y_top = p->vert[top_vertex].y;
  68. x_start = p->vert[current_left].x;
  69. x_end = p->vert[current_right].x;
  70.  
  71. DrawTrapezium(p->colour, &x_end, slope_right, slope_left, &x_start, y_bottom, y_top);
  72.  
  73. do
  74.     {
  75.         if(change_left == true)
  76.         {
  77.             current_left = next_left;
  78.             next_left = (current_left - 1 + p->nv) % p->nv;
  79.  
  80.            
  81.             x_slope = p->vert[next_left].x - p->vert[current_left].x;
  82.             y_slope = p->vert[next_left].y - p->vert[current_left].y;
  83.  
  84.             if(y_slope != 0)
  85.             {
  86.                 slope_left = x_slope/y_slope;
  87.             }
  88.  
  89.             x_start = p->vert[current_left].x;
  90.  
  91.         }
  92.         else
  93.         {
  94.             current_right = next_right;
  95.             next_right = (current_right + 1) % p->nv;
  96.  
  97.                 x_slope = p->vert[next_right].x - p->vert[current_right].x;
  98.                 y_slope = p->vert[next_right].y - p->vert[current_right].y;
  99.  
  100.                 if(y_slope != 0)
  101.                 {
  102.                     slope_right = x_slope/y_slope;
  103.                 }
  104.  
  105.                 x_end = p->vert[current_right].x;
  106.  
  107.         }
  108.  
  109.         y_top = y_bottom;
  110.        
  111.         if(p->vert[next_right].y < p->vert[next_left].y)
  112.         {
  113.             y_bottom = p->vert[next_left].y;
  114.             change_left = true;
  115.         }
  116.         else
  117.         {
  118.             y_bottom = p->vert[next_right].y;
  119.             change_left = false;
  120.         }
  121.        
  122.         DrawTrapezium(p->colour, &x_end, slope_right, slope_left, &x_start, y_bottom, y_top);
  123.  
  124.     }while(next_left != next_right);
  125. }
Add Comment
Please, Sign In to add comment