Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void DrawPolygon(POLYGON *p )
- {
- // Your code here
- int i;
- int top_vertex = 0;
- int current_left;
- int current_right;
- int next_left;
- int next_right;
- float x_start;
- float x_end;
- float y_start;
- float y_end;
- float slope_right;
- float slope_left;
- int y_top;
- int y_bottom;
- float x_slope; //slope for x
- float y_slope; //slope for y
- bool change_left;
- top_vertex = 0;
- for(i = 1; i < p->nv; i++)
- {
- if(p->vert[i].y < p->vert[top_vertex].y)
- {
- top_vertex = i;
- }
- }
- current_left = top_vertex;
- current_right = top_vertex;
- next_left = (current_left - 1 + p->nv) % p->nv;
- next_right = (current_right + 1) % p->nv;
- x_slope = p->vert[current_left].x - p->vert[next_left].x;
- y_slope = p->vert[current_left].y - p->vert[next_left].y;
- if(x_slope != y_slope)
- {
- slope_left = x_slope/y_slope;
- }
- x_slope = p->vert[current_right].x - p->vert[next_right].x;
- y_slope = p->vert[current_right].y - p->vert[next_right].y;
- if(x_slope != y_slope)
- {
- slope_right = x_slope/y_slope;
- }
- if(p->vert[next_left].y < p->vert[next_right].y)
- {
- y_bottom = p->vert[next_right].y;
- change_left = false;
- }
- else
- {
- y_bottom = p->vert[next_left].y;
- change_left = true;
- }
- y_top = p->vert[top_vertex].y;
- x_start = p->vert[current_left].x;
- x_end = p->vert[current_right].x;
- DrawTrapezium(p->colour, &x_end, slope_right, slope_left, &x_start, y_bottom, y_top);
- do
- {
- if(change_left == true)
- {
- current_left = next_left;
- next_left = (current_left - 1 + p->nv) % p->nv;
- x_slope = p->vert[next_left].x - p->vert[current_left].x;
- y_slope = p->vert[next_left].y - p->vert[current_left].y;
- if(y_slope != 0)
- {
- slope_left = x_slope/y_slope;
- }
- x_start = p->vert[current_left].x;
- }
- else
- {
- current_right = next_right;
- next_right = (current_right + 1) % p->nv;
- x_slope = p->vert[next_right].x - p->vert[current_right].x;
- y_slope = p->vert[next_right].y - p->vert[current_right].y;
- if(y_slope != 0)
- {
- slope_right = x_slope/y_slope;
- }
- x_end = p->vert[current_right].x;
- }
- y_top = y_bottom;
- if(p->vert[next_right].y < p->vert[next_left].y)
- {
- y_bottom = p->vert[next_left].y;
- change_left = true;
- }
- else
- {
- y_bottom = p->vert[next_right].y;
- change_left = false;
- }
- DrawTrapezium(p->colour, &x_end, slope_right, slope_left, &x_start, y_bottom, y_top);
- }while(next_left != next_right);
- }
Add Comment
Please, Sign In to add comment