Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 3.18 KB | Hits: 14 | Expires: Never
Copy text to clipboard
  1. //-----------------------------------------------------------------------------
  2. // Name: ClipPolyYLow
  3. // Desc: Clipping Routine for lower y boundary
  4. //-----------------------------------------------------------------------------
  5. int ClipPolyYLow(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
  6. {
  7.         // Tell calling routine how many vertices in pOutput array
  8.         return pOutput->nv;
  9. }
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Name: ClipPolyXHi
  13. // Desc: Clipping Routine for upper x boundary
  14. //-----------------------------------------------------------------------------
  15. int ClipPolyXHigh(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
  16. {
  17.         // Practical 4 - Define two local variables (2)
  18.         int current_vertex;
  19.         int previous_vertex;
  20.  
  21.         // Practical 4 - copy the non-vertex-related parts of the polygon from input to output (3)
  22.         pOutput->colour = pIinput->colour;
  23.  
  24.         // Practical 4 - Initialise the output number of vertices to zero (4)
  25.         pOutput->nv = 0;
  26.  
  27.         // Practical 4 - Implement the structured English algorithm given in the notes (5)
  28.         for (int current_vertex = 0; current_vertex < pIinput->nv; current_vertex++){
  29.                
  30.                 // Testing for wrap around (Practical 4 (5))
  31.                 if (current_vertex - 1 < 0){
  32.                         previous_vertex = (pIinput->nv - 1);
  33.                 }
  34.                 else if (current_vertex - 1 > 0){
  35.                         previous_vertex = current_vertex - 1;
  36.                 }
  37.  
  38.                 // Pracitcal 4 (6)
  39.                 // Check to see if current and previous are on screen
  40.                 if(pIinput->vert[previous_vertex].x <= iXBound){
  41.                         if(pIinput->vert[current_vertex].x <= iXBound){
  42.                                
  43.                                 // If this vertex on screen - copy this vertex to output
  44.                                 pOutput->vert[pOutput->nv] = pIinput->vert[current_vertex];
  45.                                 pOutput->nv++;
  46.                         }
  47.  
  48.                         else if(pIinput->vert[current_vertex].x > iXBound){
  49.                                
  50.                                 // If this vertex off screen - generate clipped vertex to output
  51.                                 // Output.x = xboundary
  52.                                 pOutput->vert[pOutput->nv].x = iXBound;
  53.                                 // output.y = current y + (x boundary - current x).(previous y - current y ) / (previous x - current x) (taken from notes)
  54.                                 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);
  55.                                 pOutput->nv++;
  56.                         }
  57.                 }
  58.  
  59.                 else if (pIinput->vert[previous_vertex].x > iXBound){
  60.                         if (pIinput->vert[current_vertex].x <= iXBound){
  61.                                
  62.                                 // If this vertex off screen - generate clipped vertex to output
  63.                                 // Output.x = xboundary
  64.                                 pOutput->vert[pOutput->nv].x = iXBound;
  65.                                 // output.y = previous y + (x boundary - previous x).(current y - previous y ) / (cqurrent x - previous x) (taken from notes)
  66.                                 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);
  67.                                 pOutput->nv++;
  68.  
  69.                                 // Copy vertex to output
  70.                                 pOutput->vert[pOutput->nv] = pIinput->vert[current_vertex];
  71.                                 pOutput->nv++;
  72.                         }
  73.                 }
  74.  
  75.         }
  76.        
  77.         // Tell calling routine how many vertices in pOutput array
  78.         return pOutput->nv;
  79. }