What about lines?
Lines can (and must later on) also be clipped like triangles.
As you can probably expect, this works nearly the same way as with the polygons, its just simpler.
Basically, we do only one step of the Sutherland-Hodgman algorithm: The one line against the plane. Since the polygons loop over, we can't use exactly the same algorithm, as that would reverse our lines or miss a vertex.
So we basically just do the 4 cases described in the Sutherland-Hodgman section, just adapted for only having two points as input:
- Both points outside? -> return empty array
- Both points inside? -> return original line
- Startpoint inside, endpoint outside? -> return startpoint and intersection
- Startpoint outside, endpoint inside? -> return intersection and endpoint
we also need to enhance the process_line method the same way as the process_triangle method, but it is even easier, since the clipping produces either an empty array or a new line, so we don't have to decompose the result into anything.
We just need to check, if we have two points and if so, call the rasterize_line method as before.
As this is more or less copy and paste from the more complicated triangle code, we will just show you the result here. We added some additional lines to the geometry that is clipped.
The next section will be the beginning of some incredible flexibility! Even in the first step, we will be able to freely move objects on the screen and for example change their color!