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 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. 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:
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