common shape: support ellipse

Now, you can pass separate width radius and height radius in circle.

Change-Id: Ie1e2b58fbb399d8ef74d55d83ec48d8f4323f21e
This commit is contained in:
Hermet Park 2020-04-26 18:18:52 +09:00
parent e655471e09
commit 1e96cc2b10
3 changed files with 18 additions and 11 deletions

View file

@ -93,7 +93,7 @@ public:
int clear() noexcept;
int appendRect(float x, float y, float w, float h, float cornerRadius) noexcept;
int appendCircle(float cx, float cy, float radius) noexcept;
int appendCircle(float cx, float cy, float radiusW, float radiusH) noexcept;
int fill(size_t r, size_t g, size_t b, size_t a) noexcept;

View file

@ -135,19 +135,20 @@ int ShapeNode::pathCoords(const Point** pts) const noexcept
}
int ShapeNode::appendCircle(float cx, float cy, float radius) noexcept
int ShapeNode::appendCircle(float cx, float cy, float radiusW, float radiusH) noexcept
{
auto impl = pImpl.get();
assert(impl);
auto halfKappa = radius * PATH_KAPPA;
auto halfKappaW = radiusW * PATH_KAPPA;
auto halfKappaH = radiusH * PATH_KAPPA;
impl->path->reserve(6, 13);
impl->path->moveTo(cx, cy - radius);
impl->path->cubicTo(cx + halfKappa, cy - radius, cx + radius, cy - halfKappa, cx + radius, cy);
impl->path->cubicTo(cx + radius, cy + halfKappa, cx + halfKappa, cy + radius, cx, cy + radius);
impl->path->cubicTo(cx - halfKappa, cy + radius, cx - radius, cy + halfKappa, cx - radius, cy);
impl->path->cubicTo(cx - radius, cy - halfKappa, cx - halfKappa, cy - radius, cx, cy - radius);
impl->path->moveTo(cx, cy - radiusH);
impl->path->cubicTo(cx + halfKappaW, cy - radiusH, cx + radiusW, cy - halfKappaH, cx + radiusW, cy);
impl->path->cubicTo(cx + radiusW, cy + halfKappaH, cx + halfKappaW, cy + radiusH, cx, cy + radiusH);
impl->path->cubicTo(cx - halfKappaW, cy + radiusH, cx - radiusW, cy + halfKappaH, cx - radiusW, cy);
impl->path->cubicTo(cx - radiusW, cy - halfKappaH, cx - halfKappaW, cy - radiusH, cx, cy - radiusH);
impl->path->close();
return 0;
@ -173,7 +174,7 @@ int ShapeNode::appendRect(float x, float y, float w, float h, float cornerRadius
impl->path->close();
//circle
} else if (w == h && cornerRadius * 2 == w) {
return appendCircle(x + (w * 0.5f), y + (h * 0.5f), cornerRadius);
return appendCircle(x + (w * 0.5f), y + (h * 0.5f), cornerRadius, cornerRadius);
} else {
auto halfKappa = cornerRadius * 0.5;
impl->path->reserve(10, 17);

View file

@ -17,7 +17,7 @@ void tvgtest()
auto canvas = tvg::SwCanvas::gen(buffer, WIDTH, HEIGHT);
//canvas->reserve(2); //reserve 2 shape nodes (optional)
//Prepare Rectangle
//Prepare Round Rectangle
auto shape1 = tvg::ShapeNode::gen();
shape1->appendRect(0, 0, 400, 400, 50); //x, y, w, h, cornerRadius
shape1->fill(0, 255, 0, 255); //r, g, b, a
@ -25,10 +25,16 @@ void tvgtest()
//Prepare Circle
auto shape2 = tvg::ShapeNode::gen();
shape2->appendCircle(400, 400, 200); //cx, cy, radius
shape2->appendCircle(400, 400, 200, 200); //cx, cy, radiusW, radiusH
shape2->fill(255, 255, 0, 255); //r, g, b, a
canvas->push(move(shape2));
//Prepare Ellipse
auto shape3 = tvg::ShapeNode::gen();
shape3->appendCircle(600, 600, 150, 100); //cx, cy, radiusW, radiusH
shape3->fill(0, 255, 255, 255); //r, g, b, a
canvas->push(move(shape3));
//Draw the Shapes onto the Canvas
canvas->draw();
canvas->sync();