Tessellating and Anti-Aliasing the Clipped Polygon

The last step in the process before an anti-aliased intensity value can be assigned for this HI pixel involves finding the area of the polygon which is now contained within the boundaries of the current pixel being processed. Since the area of the unit pixel is 1.0, the area of the clipped polygon can range between 0.0 to 1.0. The way I find the area of the polygon is by breaking the polygon down into a series of non-over lapping triangles and then summing up the areas of all the triangles. I do this because finding the area of a series of general triangles is much easier than trying to find the area of one general polygon. Below is a diagram of the procedure and the C computer code to implement it.

Image

void determine_clipped_polygon_area(int row, int col)
   {
   double total_polygon_area, triangle_area;
   double ax, ay, bx, by, cx, cy;
   int i,j, number_vertices_found, vertex_found[3];
   int pixel_intensity;

   total_polygon_area = 0.0; /*total area of all triangles in poly*/

   /*lets go through however many triangles there are in
     this polygon*/

   for (i=0; i < (number_out_vertices-2); i++)
      {
      number_vertices_found = 0; /*number of vertices found in tri*/

      /*lets go through all vertices in the polygon*/

      for (j=0; j < number_out_vertices; j++)
         {
         /*have we found a vertex that has not already been used?*/

         if (poly_clip.vertex_used[j] == 0)
            {
            /*lets add this vertex to our list for this triangle*/

            vertex_found[number_vertices_found] = j;

            number_vertices_found++;  /*note we have another vertex*/

            /*if this is the second vertex added for this triangle
              then we need to mark it so that it is not used again.
              We do this because we need to eliminate triangles as
              we use them so we don't keep refinding them again.*/

            if (number_vertices_found == 2)
               poly_clip.vertex_used[j] = 1;

            /*if we have now found 3 vertices, then we have a
              triangle so we don't need to look any more*/

            if (number_vertices_found == 3)
               break;
            }
         }

      /*figure out this triangle's area and add it to the total.  
        The total area of the polygon cannot exceed 1.0 since 
        the entire area of the clipping window is also 1.0 .*/

      /*lets load our triangle coords into more convienent
        storage*/

      ax = poly_clip.vertex_out[vertex_found[0]].x;
      ay = poly_clip.vertex_out[vertex_found[0]].y;
      bx = poly_clip.vertex_out[vertex_found[1]].x;
      by = poly_clip.vertex_out[vertex_found[1]].y;
      cx = poly_clip.vertex_out[vertex_found[2]].x;
      cy = poly_clip.vertex_out[vertex_found[2]].y;

      triangle_area = (((bx - ax) * (cy - ay)) -
                       ((by - ay) * (cx - ax))) / 2.0;

      /*make sure area is positive*/

      if (triangle_area < 0.0) triangle_area *= -1.0;

      total_polygon_area += triangle_area;
      }

   /*figure out what the intensity of this pixel is*/

   pixel_intensity = total_polygon_area * 255;

   if (pixel_intensity > 255) pixel_intensity=255;
   if (pixel_intensity < 0) pixel_intensity = 0;

   /*finally, lets assign anti-aliased pixel intensity 
     value to the output image array*/

   if ((output_image[row][col] + pixel_intensity) > 255)
      output_image[row][col] = 255;
   else
   output_image[row][col] += pixel_intensity;
   }

Once all the areas of all the triangles forming the polygon have been summed up, the last step is to take this polygon's area and just multiply it by 255. An 8 bit image can have its pixels at 256 different gradations between a pure black value of 0 and a pure white value of 255. Once the pixel value is determined, I simply add this value into the HI. If a HI pixel value exceeds 255 I simply lope off the excess value beyond 255. And that's all there is to it!!! Hehehehe!


Last Updated: April 25, 2000
HTML URL: http://geocities.datacellar.net/~special_effect/hyperspace_ta.html
E-Mail: special_effect@geocities.com or click here
1