Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Final commit, end of project
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroafmonteiro committed May 17, 2024
1 parent c618523 commit a420513
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 85 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ All types of transformations (translate, rotate and scale) are well implemented

### Groups

### Element duplication
The groups have been implemented using a derived subclass from SVGElement and uses a recursive function in [readSVG.cpp](readSVG.cpp) with all transformations working.

### Element duplication

For elements that have an "id" attribute, they can be easily duplicated using <use> and their respective transformations.
135 changes: 111 additions & 24 deletions SVGElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,33 @@ namespace svg
img.draw_ellipse(center, radius, fill);
}

/**
* @brief Translates the center of the ellipse by the given translation vector.
*
* @param t The translation vector.
*/
void Ellipse::translate(const Point &t)
{
center = center.translate(t);
}

/**
* @brief Rotates the ellipse around a specified origin by a given number of degrees.
*
* @param origin The origin point around which the ellipse will be rotated.
* @param degrees The number of degrees by which the ellipse will be rotated.
*/
void Ellipse::rotate(const Point &origin, int degrees)
{
center = center.rotate(origin, degrees);
}

/**
* @brief Scales the ellipse by a given factor around a specified origin point.
*
* @param origin The origin point around which the ellipse will be scaled.
* @param v The scaling factor.
*/
void Ellipse::scale(const Point &origin, int v)
{
center = center.scale(origin, v);
Expand All @@ -66,23 +83,6 @@ namespace svg
img.draw_ellipse(center, radius, fill);
}

/* void Circle::translate(const Point &t)
{
center = center.translate(t);
}
void Circle::rotate(const Point &origin, int degrees)
{
center = center.rotate(origin, degrees);
}
void Circle::scale(const Point &origin, int v)
{
center = center.scale(origin, v);
radius.x = radius.x * v;
radius.y = radius.y * v;
} */

/**
* @brief Creates a copy of the Circle object.
*
Expand Down Expand Up @@ -117,13 +117,24 @@ namespace svg
}
}

/**
* @brief Translates the points of the polyline by the given translation vector.
*
* @param t The translation vector.
*/
void Polyline::translate(const Point &t)
{
for (size_t i = 0; i < points.size(); i++) {
points[i] = points[i].translate(t);
}
}

/**
* @brief Rotates the polyline around a specified origin by a given number of degrees.
*
* @param origin The origin point around which the polyline will be rotated.
* @param degrees The number of degrees by which the polyline will be rotated.
*/
void Polyline::rotate(const Point &origin, int degrees)
{
for (size_t i = 0; i < points.size(); i++)
Expand All @@ -132,6 +143,12 @@ namespace svg
}
}

/**
* @brief Scales the polyline by a given factor around a specified origin point.
*
* @param origin The origin point around which the polyline will be scaled.
* @param v The scaling factor.
*/
void Polyline::scale(const Point &origin, int v)
{
for (size_t i = 0; i < points.size(); i++)
Expand Down Expand Up @@ -159,18 +176,35 @@ namespace svg
img.draw_line(start, end, stroke);
}

/**
* @brief Translates the points of the line by the given translation vector.
*
* @param t The translation vector.
*/
void Line::translate(const Point &t)
{
start = start.translate(t);
end = end.translate(t);
}

/**
* @brief Rotates the line around a specified origin by a given number of degrees.
*
* @param origin The origin point around which the line will be rotated.
* @param degrees The number of degrees by which the line will be rotated.
*/
void Line::rotate(const Point &origin, int degrees)
{
start = start.rotate(origin, degrees);
end = end.rotate(origin, degrees);
}

/**
* @brief Scales the line by a given factor around a specified origin point.
*
* @param origin The origin point around which the line will be scaled.
* @param v The scaling factor.
*/
void Line::scale(const Point &origin, int v)
{
start = start.scale(origin, v);
Expand Down Expand Up @@ -209,6 +243,11 @@ namespace svg
img.draw_polygon(points, fill);
}

/**
* @brief Translates the points of the polygon by the given translation vector.
*
* @param t The translation vector.
*/
void Polygon::translate(const Point &t)
{
for (size_t i = 0; i < points.size(); i++)
Expand All @@ -217,6 +256,12 @@ namespace svg
}
}

/**
* @brief Rotates the polygon around a specified origin by a given number of degrees.
*
* @param origin The origin point around which the polygon will be rotated.
* @param degrees The number of degrees by which the polygon will be rotated.
*/
void Polygon::rotate(const Point &origin, int degrees)
{
for (size_t i = 0; i < points.size(); i++)
Expand All @@ -225,6 +270,12 @@ namespace svg
}
}

/**
* @brief Scales the polygon by a given factor around a specified origin point.
*
* @param origin The origin point around which the polygon will be scaled.
* @param v The scaling factor.
*/
void Polygon::scale(const Point &origin, int v)
{
for (size_t i = 0; i < points.size(); i++)
Expand Down Expand Up @@ -261,44 +312,80 @@ namespace svg
return new Rect(points, fill, id);
}



/**
* @brief Draws a group on the given PNGImage.
*
* @param img The PNGImage to draw on.
*/
void Group::draw(PNGImage &img) const
{
for (auto y : V ){
y.draw(img);
y->draw(img);
}
}

/**
* @brief Translates the elements of the group by the given translation vector.
*
* @param t The translation vector.
*/
void Group::translate(const Point &t)
{
for (auto y : V ){
y.translate(t);
y->translate(t);
}
}

/**
* @brief Rotates the elements of the group around a specified origin by a given number of degrees.
*
* @param origin The origin point around which the elements will be rotated.
* @param degrees The number of degrees by which the elements will be rotated.
*/
void Group::rotate(const Point &origin, int degrees)
{
for (auto y : V ){
y.rotate(origin,degrees);
y->rotate(origin,degrees);
}
}

/**
* @brief Scales the elements of the group by a given factor around a specified origin point.
*
* @param origin The origin point around which the elements will be scaled.
* @param v The scaling factor.
*/
void Group::scale(const Point &origin, int v)
{
for (auto y : V ){
y.scale(origin,v);
y->scale(origin,v);
}
}


/**
* @brief Destructor for the Group class.
*
* This destructor is responsible for freeing the memory allocated for the objects
* stored in the `V` vector.
*/
Group::~Group()
{
for (auto y : V ){
delete y;
}
}

/**
* @brief Creates a deep copy of the Group object.
*
* @return A pointer to the newly created Group object.
*/
SVGElement* Group::copy() const{
return new Group(VectorFigs);
std::vector<SVGElement*> temp;
for (auto y : V ){
temp.push_back(y->copy());
}
return new Group(temp);
}
}
24 changes: 17 additions & 7 deletions SVGElements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,28 @@ namespace svg
SVGElement* copy() const override; // Declaration of the Rectangle's copy function.
};

/**
* @class Group
* @brief Represents a group of SVG elements.
*/
class Group:public SVGElement
{
public:
Group(const std::vector<SVGElement*> &VectorFigs) : V(VectorFigs) {}
void draw(PNGImage &img) const override;
void translate(const Point &t) override;
void rotate(const Point &origin,
int degrees) override;
/**
* @brief Constructs a Group object with the given vector of SVGElement pointers.
*
* @param VectorFigs The vector of SVGElement pointers to be stored in the Group.
*/
Group(const std::vector<SVGElement*> &VectorFigs)
: V(VectorFigs) {}
void draw(PNGImage &img) const override; // Declaration of the Groups's draw function.
void translate(const Point &t) override; // Declaration of the Groups's translate function.
void rotate(const Point &origin,
int degrees) override; // Declaration of the Groups's rotate function.
void scale(const Point &origin,
int v) override;
int v) override; // Declaration of the Groups's scale function.
~Group();
SVGElement* copy() const override;
SVGElement* copy() const override; // Declaration of the Groups's copy function.
private:
std::vector<SVGElement*> V;
};
Expand Down
Loading

0 comments on commit a420513

Please sign in to comment.