Organization of the Course

Seminar 2 - Triangle Rasterization + Line Clipping

Your task will be to implement 3 methods again. This time it will be one focused on triangle rasterization and two focused on line clipping.

You can find the details in the study materials for the corresponding lecture.

The deadline is again 2 weeks from the assignment.

1) Pineda

This algorithm is described in Lecture 3.

You can use the edge function to sort the triangle points clockwise or counter-clockwise.

A triangle vertex coordinates can exceed the raster size, meaning that you must clamp the input values for all tests to pass.

You must compute a bounding box of the given triangle, not only naively go through all the pixels in raster.

You must transform the coordinates between the window space and raster space correctly. [0, 0] is the bottom left corner and [raster_width, raster_height] is the top right corner of the window. The pixel must be colored iff the center of the raster pixel is inside or on the edge of the triangle in window space. The pixel coordinates correspond to the lower left corner of the pixel. So if you want to know if the pixel is inside of a triangle, you will need to check if its center is inside the triangle. This means that you need to add 0.5 in y and 0.5 in x axis to get the center of the pixel in window space.

2) Cohen-Sutherland

This algorithm is described in Lecture 4.

The basic idea is to clip a line to an axis-oriented rectangle (e.g., a window).

The binary numbers can be created by bit shifting operators or alternatively you can define them simple as  numbers (e.g., 1000 corresponds to number 8, 0100 to 4,  0010 to 2 etc.).

If the line is inside the rectangle or intersects with any of its edges or points, you must set the visible flag to true.

3) Cyrus-Beck

This algorithm is also described in Lecture 4.

The basic idea is to clip a line to fit a polygon. Here we use a triangle for simplicity, but the approach would be similar for any convex generic polygon.

You can again use the edge function to determine the order of the triangle points and thus the orientation of triangle edges (CW vs. CCW).

Be careful about the orientation of a normal.

If the line is inside of the polygon or intersects with any of its edges or points , you must set the visible flag to true.