Skip to content

Commit

Permalink
Move segments out of ellipse function.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregersn committed Nov 6, 2024
1 parent 65e8d36 commit 79b7146
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions include/pyro/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Pyro
std::filesystem::path filename{""};

bool _smooth{true};
int _curvedetail{32};

Shape _shape{Shape()};
Transformer2D transformer;
Expand Down Expand Up @@ -76,6 +77,8 @@ namespace Pyro
Color stroke(int c, int a = 255);
Color stroke(int r, int g, int b, int a = 255);

void curvedetail(int segments);

float strokeweight();
float strokeweight(float w);
virtual void strokecap(int cap);
Expand Down Expand Up @@ -178,10 +181,10 @@ namespace Pyro
{
this->style.ellipsemode(mode);
};
void ellipse(float x, float y, float w, float h, unsigned int segments);
void ellipse(float x, float y, float r, unsigned int segments)
void ellipse(float x, float y, float w, float h);
void ellipse(float x, float y, float r)
{
this->ellipse(x, y, r, r, segments);
this->ellipse(x, y, r, r);
};

// Typography
Expand All @@ -205,7 +208,6 @@ namespace Pyro
std::filesystem::path filename = "");

Graphics *creategraphics(unsigned int width, unsigned int height, int dpi, Unit unit, GraphicsMode mode = GraphicsMode::CAIRO, std::filesystem::path filename = "");

};

#endif
4 changes: 2 additions & 2 deletions include/pyro/pyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ namespace Pyro

inline void arc(float a, float b, float c, float d, float start, float end, int mode = OPEN) { pg->arc(a, b, c, d, start, end, mode); };

inline void ellipse(float x, float y, float w, float h, unsigned int segments = 32) { pg->ellipse(x, y, w, h, segments); };
inline void circle(float x, float y, float r, unsigned int segments = 32) { ellipse(x, y, r, r, segments); };
inline void ellipse(float x, float y, float w, float h) { pg->ellipse(x, y, w, h); };
inline void circle(float x, float y, float r) { ellipse(x, y, r, r); };

inline void translate(float x, float y) { pg->translate(x, y); };
inline void scale(float sx, float sy) { pg->scale(sx, sy); };
Expand Down
9 changes: 6 additions & 3 deletions screenshot-tests/test-shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ TEST_CASE("Draw shapes", "[shapes]")
p->background(192);
p->nostroke();
p->fill(64, 64, 255);
p->ellipse(p->width() / 2, p->height() / 2, 60.f, 64);
p->curvedetail(64);
p->ellipse(p->width() / 2, p->height() / 2, 60.f);
p->save(current_folder / filename);
CHECK_THAT(current_folder / filename, LooksLike(actual_folder / filename));
}
Expand All @@ -89,7 +90,8 @@ TEST_CASE("Draw shapes", "[shapes]")
p->background(192);
p->nostroke();
p->fill(255, 64, 128);
p->ellipse(p->width() / 2, p->height() / 2, 60, 120, 64);
p->curvedetail(64);
p->ellipse(p->width() / 2, p->height() / 2, 60, 120);
p->save(current_folder / filename);
CHECK_THAT(current_folder / filename, LooksLike(actual_folder / filename));
}
Expand All @@ -112,7 +114,8 @@ TEST_CASE("Variable side count ellipses", "[shapes]")
p->pushmatrix();
for (uint32_t x = 0; x < 4; x++)
{
p->ellipse(512 / 8, 512 / 8, 500 / 4, 500 / 4, (y * 4) + x + 3);
p->curvedetail((y * 4) + x + 3);
p->ellipse(512 / 8, 512 / 8, 500 / 4, 500 / 4);
p->translate(512 / 4, 0);
}
p->popmatrix();
Expand Down
3 changes: 3 additions & 0 deletions screenshot-tests/test-style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ TEST_CASE("Push and pop style")

Pyro::Graphics *p = Pyro::creategraphics(400, 400);

p->curvedetail(132);

p->ellipse(0, 200, 132, 132); // Left circle

p->pushstyle(); // Start a new style
p->strokeweight(40);
p->fill(204, 153, 0);

p->ellipse(200, 200, 132, 132); // Middle circle
p->popstyle(); // Restore original style

Expand Down
3 changes: 2 additions & 1 deletion screenshot-tests/test-units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ SCENARIO("Check that different units can be used.")
p->nostroke();
p->background(192);
p->fill(0, 0, 0, 255);
p->ellipse(2, 2, 2, 8);
p->curvedetail(8);
p->ellipse(2, 2, 2);
p->save(current_folder / filename);
delete p;
CHECK_THAT(current_folder / filename, LooksLike(actual_folder / filename));
Expand Down
11 changes: 8 additions & 3 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ namespace Pyro
return g;
}

void Graphics::curvedetail(int segments)
{
this->_curvedetail = segments;
}

void Graphics::point(float x, float y)
{
this->line(x, y, x + 1, y + 1);
Expand Down Expand Up @@ -261,13 +266,13 @@ namespace Pyro
this->shape(s, x, y);
}

void Graphics::ellipse(float x, float y, float w, float h, unsigned int segments)
void Graphics::ellipse(float x, float y, float w, float h)
{
Shape s = Shape();
s.begin();
float da = M_PI / (segments / 2.0f);
float da = M_PI / (this->_curvedetail / 2.0f);

for (unsigned int i = 0; i < segments; i++)
for (unsigned int i = 0; i < this->_curvedetail; i++)
{
s.vertex(cos(i * da) * w / 2.0f,
sin(i * da) * h / 2.0f);
Expand Down

0 comments on commit 79b7146

Please sign in to comment.