-
Notifications
You must be signed in to change notification settings - Fork 1
Draw polygon #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Draw polygon #10
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| #include <memory> | ||
| #include <set> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "image_event.h" | ||
|
|
||
|
|
@@ -277,6 +278,28 @@ class Image { | |
| bool DrawRectangle(int x, int y, int width, int height, int red, int green, | ||
| int blue); | ||
|
|
||
| /** | ||
| * Draws a polygon whose vertices are listed in |points| and colored with | ||
| * |color|. Each vertex is represented by it's x and y coordinate, and are | ||
| * listed sequentially in the vector. For example, a polygon with three | ||
| * vertices (0, 0), (0, 2), (2,1) is represented as a vector of integers | ||
| * {0, 0, 0, 2, 2, 1}. The last vertex will connect with the first vertex in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little hard to read this polygon vector, especially for large numbers of vertices. What do you think of making a simple graphics::Point class and having a std::vectorgraphics::Point instead? You could also add a DrawTriangle that takes int x1, int y1, int x2, int y2, int x3, int y3 as that's probably the most common polygon that would be used. |
||
| * the list. Returns false if params are out of bounds. | ||
| */ | ||
| bool DrawPolygon(std::vector<int>& points, const Color& color) { | ||
|
pinventado marked this conversation as resolved.
Outdated
|
||
| return DrawPolygon(points, color.Red(), color.Green(), color.Blue()); | ||
| } | ||
|
|
||
| /** | ||
| * Draws a polygon whose vertices are listed in |points| and colored with | ||
| * |red|, |green|, |blue|. Each vertex is represented by it's x and y | ||
| * coordinate, and are listed sequentially in the vector. For example, a | ||
| * polygon with three vertices (0, 0), (0, 2), (2,1) is represented as a | ||
| * vector of integers {0, 0, 0, 2, 2, 1}. The last vertex will connect with | ||
| * the first vertex in* the list. Returns false if params are out of bounds. | ||
| */ | ||
| bool DrawPolygon(std::vector<int>& points, int red, int green, int blue); | ||
|
pinventado marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Draws the string |text| with position (x,y) at the top left corner, | ||
| * with |font_size| in pixels, colored by |color|. Returns false if the | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ TEST(ImageTest, Drawing) { | |
| graphics::Image image(50, 50); | ||
| graphics::Color white(255, 255, 255); | ||
| graphics::Color blue(0, 0, 255); | ||
| graphics::Color red(255, 0, 0); | ||
| image.DrawCircle(20, 20, 5, blue); | ||
| // Spot check some pixels. | ||
| EXPECT_EQ(image.GetColor(20, 20), blue); | ||
|
|
@@ -123,6 +124,10 @@ TEST(ImageTest, Drawing) { | |
| EXPECT_EQ(image.GetBlue(i, j), 0); | ||
| } | ||
| } | ||
| std::vector<int> points = {20, 20, 20, 22, 22, 21}; | ||
| image.DrawPolygon(points, red); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add some polygon-speicifc tests: first, that drawing a polygon clockwise is the same as drawing the same thing counterclockwise? second, if drawing a rect is the same as drawing a polygon with four points? |
||
| EXPECT_EQ(image.GetColor(20, 21), red); | ||
| EXPECT_EQ(image.GetColor(21, 21), red); | ||
|
|
||
| // Drawing something out of bounds doesn't work. | ||
| image.DrawRectangle(-1, -1, 50, 50, 0, 255, 0); | ||
|
|
@@ -131,6 +136,10 @@ TEST(ImageTest, Drawing) { | |
| image.DrawCircle(40, 50, 100, 0, 255, 0); | ||
| EXPECT_EQ(image.GetColor(0, 0), white); | ||
|
|
||
| std::vector<int> out_points = {-1, 0, 0, 0, -2, 0}; | ||
| image.DrawPolygon(out_points, red); | ||
| EXPECT_EQ(image.GetColor(0, 0), white); | ||
|
|
||
| // Hard to test the line because of anti-aliasing. | ||
| image.DrawLine(0, 0, 40, 40, 255, 0, 0); | ||
| EXPECT_EQ(image.GetGreen(0, 0), 0); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.