From 79b714601e567a21e097fcbc1454861b4c5c454e Mon Sep 17 00:00:00 2001 From: Greger Stolt Nilsen Date: Wed, 6 Nov 2024 23:48:02 +0100 Subject: [PATCH] Move segments out of ellipse function. --- include/pyro/graphics.h | 10 ++++++---- include/pyro/pyro.h | 4 ++-- screenshot-tests/test-shapes.cpp | 9 ++++++--- screenshot-tests/test-style.cpp | 3 +++ screenshot-tests/test-units.cpp | 3 ++- src/graphics.cpp | 11 ++++++++--- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/include/pyro/graphics.h b/include/pyro/graphics.h index 20da779..dcc9184 100644 --- a/include/pyro/graphics.h +++ b/include/pyro/graphics.h @@ -27,6 +27,7 @@ namespace Pyro std::filesystem::path filename{""}; bool _smooth{true}; + int _curvedetail{32}; Shape _shape{Shape()}; Transformer2D transformer; @@ -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); @@ -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 @@ -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 diff --git a/include/pyro/pyro.h b/include/pyro/pyro.h index c3be146..669a485 100644 --- a/include/pyro/pyro.h +++ b/include/pyro/pyro.h @@ -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); }; diff --git a/screenshot-tests/test-shapes.cpp b/screenshot-tests/test-shapes.cpp index 6eee5a4..d018bbb 100644 --- a/screenshot-tests/test-shapes.cpp +++ b/screenshot-tests/test-shapes.cpp @@ -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)); } @@ -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)); } @@ -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(); diff --git a/screenshot-tests/test-style.cpp b/screenshot-tests/test-style.cpp index 6047272..4f179c6 100644 --- a/screenshot-tests/test-style.cpp +++ b/screenshot-tests/test-style.cpp @@ -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 diff --git a/screenshot-tests/test-units.cpp b/screenshot-tests/test-units.cpp index 62fdc65..6944fdd 100644 --- a/screenshot-tests/test-units.cpp +++ b/screenshot-tests/test-units.cpp @@ -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)); diff --git a/src/graphics.cpp b/src/graphics.cpp index fcec0fe..ae06495 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -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); @@ -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);