CS 345: Spring 2025
Programming Assignment 4
Paths and Gradients
class GPath {
public:
...
GRect bounds() const;
};
class GPathBuilder {
public:
...
void addRect(const GRect& r, GPathDirection dir);
void addPolygon(const GPoint pts[], int count);
};
class GCanvas {
public:
...
virtual void drawPath(const GPath& path, const GPaint&) = 0;
};
std::unique_ptr<GShader> GCreateLinearGradient(GPoint p0, GPoint p1, const GColor[], int count);
Your task is to finish implementing GPath and GPathBuilder, extend your GCanvas subclass, and implement a gradient GShader subclass.
- GPath, GPathBuilder
- GCanvas::drawPath
- GCreateLinearGradient
GPath is a new class to hold multiple contours. Your job is to implement the missing methods
in GPath and GPathBuilder. This is similar to how you implemented the missing methods on GMatrix in PA3.
Override GCanvas::drawPath(). There are two key changes from your drawConvexPolygon() code.
- Paths can have multiple contours. To help finding all of the edges, use GPath::Edger
to return each edge for all contours.
- Path contours are not guaranteed to be convex. To handle this in your scan converter,
Each edge will need to track its winding value (either 1 or -1). Since the path
may not be convex, it means there may more than 2 x-intervals within a scanline. The
winding rule is to only blit intervals where the accumulated winding value is non-zero.
GCreateLinearGradient returns your subclass of GShader for a linear gradient. The gradient is
given N colors, which should appear evenly spaced between the endpoints of the line segment.
Your directory should like something like this:
- apps contains the code for testing your code.
- expected contains sample output from the image tool to compare with your output.
- include contains class headers that your code can use.
- src contains common implementations needed for the tools.
- DO NOT EDIT/ADD/REMOVE anything in these sub-directories. You should only add files to the root of the directory.
- your_files any .cpp or .h files you need for your implementation. All of your work must be here.
To test your code...
> make
> ./image -e expected
> ./tests
> ./bench
> ./dbench be sure this doesn't crash
Bench scores the VM
clear 2.40
rects_blend 6.37
rects_opaque 0.76
rect_big 0.53
rect_tiny 0.53
quads_blend 7.09
quads_opaque 1.15
circles_large 4.18
circles_tiny 1.07
modes_0 1.46
modes_x 25.25
modes_1 14.90
bitmap_opaque 3.43
bitmap_alpha 6.70
gradient_1 0.10
gradient_2 3.88
gradient_3 9.43
path_small 0.61
path_big 3.23
path_bigc 1.05