- //-----------------------------------------------------------------------------
- // Name: ClipPolyYLow
- // Desc: Clipping Routine for lower y boundary
- //-----------------------------------------------------------------------------
- int ClipPolyYLow(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
- {
- // Tell calling routine how many vertices in pOutput array
- return pOutput->nv;
- }
- //-----------------------------------------------------------------------------
- // Name: ClipPolyXHi
- // Desc: Clipping Routine for upper x boundary
- //-----------------------------------------------------------------------------
- int ClipPolyXHigh(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
- {
- // Practical 4 - Define two local variables (2)
- int current_vertex;
- int previous_vertex;
- // Practical 4 - copy the non-vertex-related parts of the polygon from input to output (3)
- pOutput->colour = pIinput->colour;
- // Practical 4 - Initialise the output number of vertices to zero (4)
- pOutput->nv = 0;
- // Practical 4 - Implement the structured English algorithm given in the notes (5)
- for (int current_vertex = 0; current_vertex < pIinput->nv; current_vertex++){
- // Testing for wrap around (Practical 4 (5))
- if (current_vertex - 1 < 0){
- previous_vertex = (pIinput->nv - 1);
- }
- else if (current_vertex - 1 > 0){
- previous_vertex = current_vertex - 1;
- }
- // Pracitcal 4 (6)
- // Check to see if current and previous are on screen
- if(pIinput->vert[previous_vertex].x <= iXBound){
- if(pIinput->vert[current_vertex].x <= iXBound){
- // If this vertex on screen - copy this vertex to output
- pOutput->vert[pOutput->nv] = pIinput->vert[current_vertex];
- pOutput->nv++;
- }
- else if(pIinput->vert[current_vertex].x > iXBound){
- // If this vertex off screen - generate clipped vertex to output
- // Output.x = xboundary
- pOutput->vert[pOutput->nv].x = iXBound;
- // output.y = current y + (x boundary - current x).(previous y - current y ) / (previous x - current x) (taken from notes)
- pOutput->vert[pOutput->nv].y = pIinput->vert[current_vertex].y + (iXBound - pIinput->vert[current_vertex].x) * (pIinput->vert[previous_vertex].y - pIinput->vert[current_vertex].y)/ (pIinput->vert[previous_vertex].x - pIinput->vert[current_vertex].x);
- pOutput->nv++;
- }
- }
- else if (pIinput->vert[previous_vertex].x > iXBound){
- if (pIinput->vert[current_vertex].x <= iXBound){
- // If this vertex off screen - generate clipped vertex to output
- // Output.x = xboundary
- pOutput->vert[pOutput->nv].x = iXBound;
- // output.y = previous y + (x boundary - previous x).(current y - previous y ) / (cqurrent x - previous x) (taken from notes)
- pOutput->vert[pOutput->nv].y = pIinput->vert[previous_vertex].y + (iXBound - pIinput->vert[previous_vertex].x) * (pIinput->vert[current_vertex].y - pIinput->vert[previous_vertex].y) / (pIinput->vert[current_vertex].x - pIinput->vert[previous_vertex].x);
- pOutput->nv++;
- // Copy vertex to output
- pOutput->vert[pOutput->nv] = pIinput->vert[current_vertex];
- pOutput->nv++;
- }
- }
- }
- // Tell calling routine how many vertices in pOutput array
- return pOutput->nv;
- }
