Skip to content

Commit

Permalink
Add vector functions for drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregersn committed Nov 11, 2024
1 parent 68d060a commit 04ac8e2
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
25 changes: 23 additions & 2 deletions include/pyro/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ namespace Pyro

// Transformation
virtual void translate(float x, float y);
virtual void translate(Vector v);
virtual void rotate(float a);
virtual void scale(float sx, float sy);
virtual void scale(Vector v);
virtual void pushmatrix();
virtual void popmatrix();
void pushstyle();
Expand Down Expand Up @@ -136,18 +138,26 @@ namespace Pyro
{
this->_shape.endcontour();
};
void vertex(Vector v)
void vertex(Vector p)
{
this->_shape.vertex(v.x, v.y);
this->_shape.vertex(p);
};
void vertex(float x, float y)
{
this->_shape.vertex(x, y);
};
void curvevertex(Vector p)
{
this->_shape.curvevertex(p);
};
void curvevertex(float x, float y)
{
this->_shape.curvevertex(x, y);
};
void beziervertex(Vector p2, Vector p3, Vector p4)
{
this->_shape.beziervertex(p2, p3, p4);
}
void beziervertex(float x2, float y2, float x3, float y3, float x4, float y4)
{
this->_shape.beziervertex(x2, y2, x3, y3, x4, y4);
Expand All @@ -160,8 +170,12 @@ namespace Pyro

// Primitive shapes
void point(float x, float y);
void point(Vector p);
virtual void line(Vector /*p0*/, Vector /*p1*/){};
virtual void line(float /*x0*/, float /*y0*/, float /*x1*/, float /*y1*/) {};
void curve(Vector p0, Vector p1, Vector p2, Vector p3);
void curve(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);
void bezier(Vector p0, Vector p1, Vector p2, Vector p3);
void bezier(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);

void triangle(Vector a, Vector b, Vector c);
Expand All @@ -172,10 +186,12 @@ namespace Pyro
this->style.rectmode(mode);
};
void rect(float a, float b, float c, float d);
void rect(Vector p0, Vector p1);

void quad(Vector a, Vector b, Vector c, Vector d);
void quad(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);

void arc(Vector p0, Vector p1, float start, float end, int mode = OPEN);
void arc(float a, float b, float c, float d, float start, float end, int mode = OPEN);

void ellipsemode(int mode)
Expand All @@ -187,6 +203,11 @@ namespace Pyro
{
this->ellipse(x, y, r, r);
};
void ellipse(Vector p0, float w, float h);
void ellipse(Vector p0, float r)
{
this->ellipse(p0, r, r);
};

// Typography

Expand Down
13 changes: 13 additions & 0 deletions include/pyro/pyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,35 @@ namespace Pyro
inline void endshape() { pg->endshape(); };
inline void endshape(int close) { pg->endshape(close); };
inline void vertex(float x, float y) { pg->vertex(x, y); };
inline void vertex(Vector p) { pg->vertex(p); };
inline void curvevertex(float x, float y) { pg->curvevertex(x, y); };
inline void curvevertex(Vector p) { pg->curvevertex(p); };
inline void beziervertex(float x2, float y2, float x3, float y3, float x4, float y4) { pg->beziervertex(x2, y2, x3, y3, x4, y4); };
inline void beziervertex(Vector p2, Vector p3, Vector p4) { pg->beziervertex(p2, p3, p4); };

inline void point(float x, float y) { pg->point(x, y); };
inline void point(Vector p) { pg->point(p); };
inline void line(float x0, float y0, float x1, float y1) { pg->line(x0, y0, x1, y1); };
inline void line(Vector p0, Vector p1) { pg->line(p0, p1); };
inline void triangle(float x0, float y0, float x1, float y1, float x2, float y2) { pg->triangle(x0, y0, x1, y1, x2, y2); };
inline void triangle(Vector p0, Vector p1, Vector p2) { pg->triangle(p0, p1, p2); };
inline void rect(float a, float b, float c, float d) { pg->rect(a, b, c, d); };
inline void rect(Vector p0, Vector p1) { pg->rect(p0, p1); };
inline void quad(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) { pg->quad(x0, y0, x1, y1, x2, y2, x3, y3); };
inline void quad(Vector p0, Vector p1, Vector p2, Vector p3) { pg->quad(p0, p1, p2, p3); };

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 arc(Vector p0, Vector p1, float start, float end, int mode = OPEN) { pg->arc(p0, p1, start, end, mode); };

inline void ellipse(float x, float y, float w, float h) { pg->ellipse(x, y, w, h); };
inline void ellipse(Vector p0, float w, float h) { pg->ellipse(p0, w, h); };
inline void circle(float x, float y, float r) { ellipse(x, y, r, r); };
inline void circle(Vector p0, float r) { ellipse(p0, r, r); };

inline void translate(float x, float y) { pg->translate(x, y); };
inline void translate(Vector v) { pg->translate(v); };
inline void scale(float sx, float sy) { pg->scale(sx, sy); };
inline void scale(Vector v) { pg->scale(v); };
inline void rotate(float a) { pg->rotate(a); };
inline void pushmatrix() { pg->pushmatrix(); };
inline void popmatrix() { pg->popmatrix(); };
Expand Down
10 changes: 6 additions & 4 deletions include/pyro/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ namespace Pyro
void end(int close);
void begincontour();
void endcontour();
void vertex(Vector v) { this->vertex(v.x, v.y); };
void vertex(float x, float y);
void curvevertex(float x, float y);
void beziervertex(float x2, float y2, float x3, float y3, float x4, float y4);
void vertex(Vector p);
void vertex(float x, float y) { this->vertex(Vector(x, y)); };
void curvevertex(Vector p);
void curvevertex(float x, float y) { this->curvevertex(Vector(x, y)); };
void beziervertex(Vector p2, Vector p3, Vector p4);
void beziervertex(float x2, float y2, float x3, float y3, float x4, float y4) { this->beziervertex(Vector(x2, y2), Vector(x3, y3), Vector(x4, y4)); };
std::vector<std::vector<Pyro::Vector>> getpoints() { return this->outpoints; };
};
};
Expand Down
2 changes: 2 additions & 0 deletions include/pyro/transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ namespace Pyro

public:
Transformer2D();
void translate(Vector v);
void translate(float x, float y);
void rotate(float a);
void scale(Vector v);
void scale(float sx, float sy);

void pushmatrix();
Expand Down
21 changes: 21 additions & 0 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,20 @@ namespace Pyro
this->transformer.translate(x, y);
}

void Graphics::translate(Vector v)
{
this->transformer.translate(v);
}

void Graphics::rotate(float a)
{
this->transformer.rotate(a);
}

void Graphics::scale(Vector v)
{
this->transformer.scale(v);
}
void Graphics::scale(float sx, float sy)
{
this->transformer.scale(sx, sy);
Expand Down Expand Up @@ -192,6 +201,18 @@ namespace Pyro
this->shape(s, 0, 0);
}

void Graphics::rect(Vector p0, Vector p1)
{
Shape s{Shape()};
s.begin();
s.vertex(p0);
s.vertex(p1.x, p0.y);
s.vertex(p1);
s.vertex(p0.x, p1.y);
s.end(CLOSE);
this->shape(s, 0, 0);
}

void Graphics::curve(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
{
Shape s{Shape()};
Expand Down
16 changes: 8 additions & 8 deletions src/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,22 @@ namespace Pyro
{
}

void Shape::vertex(float x, float y)
void Shape::vertex(Vector p)
{
this->points.push_back({Pyro::Vector(x, y), PointType::VERTEX});
this->points.push_back({p, PointType::VERTEX});
}

void Shape::curvevertex(float x, float y)
void Shape::curvevertex(Vector p)
{
this->points.push_back({Pyro::Vector(x, y), PointType::CURVEVERTEX});
this->points.push_back({p, PointType::CURVEVERTEX});
}

void Shape::beziervertex(float x2, float y2, float x3, float y3, float x4, float y4)
void Shape::beziervertex(Vector p2, Vector p3, Vector p4)
{
assert(this->points.size() > 0);
this->points.push_back({Pyro::Vector(x2, y2), PointType::BEZIERVERTEX});
this->points.push_back({Pyro::Vector(x3, y3), PointType::BEZIERVERTEX});
this->points.push_back({Pyro::Vector(x4, y4), PointType::BEZIERVERTEX});
this->points.push_back({p2, PointType::BEZIERVERTEX});
this->points.push_back({p3, PointType::BEZIERVERTEX});
this->points.push_back({p4, PointType::BEZIERVERTEX});
}

}
10 changes: 10 additions & 0 deletions src/transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace Pyro
this->current = Eigen::Affine2d::Identity();
this->stack = std::vector<Eigen::Affine2d>();
}
void Transformer2D::translate(Vector v)
{
Eigen::Vector2d t = Eigen::Vector2d(v.x, v.y);
this->current *= Eigen::Translation2d(t);
}

void Transformer2D::translate(float x, float y)
{
Expand All @@ -27,6 +32,11 @@ namespace Pyro
this->current *= Eigen::Scaling<double>(sx, sy);
}

void Transformer2D::scale(Vector v)
{
this->current *= Eigen::Scaling<double>(v.x, v.y);
}

void Transformer2D::pushmatrix()
{
this->stack.push_back(Eigen::Affine2d(this->current));
Expand Down

0 comments on commit 04ac8e2

Please sign in to comment.