Search code examples
c++svgdrawrectangle

Draw Rectangles with any defined point


hello I'm in engineering student and I'm currently working on C++ project. I need to draw a couple of rectangles with this function but I can only draw them by defining the same top left corner. I'd like to draw rectangles by defining one of the 9 points of the recangle ( middle center, middle left , middle right, top right , top left, Bottom center…). could you please help me ?

void Svgfile::addRectangle(int x1,int y1,int width,int height, std::string color)
{
          m_ostrm << "<polygon points=\" "
            << x1 << "," << y1 << " "
            << x1+width << "," << y1 << " "
            << x1+width << "," << y1+height << " "
            << x1 << "," << y1+height << " "
            << x1 << "," << y1
            << "\" style=\"fill:" << color
            << "\" />\n";
}


Solution

  • Transform all the points in question to top left point and use the same code. For transforming for each point type you can use a switch, if/else or function calls. For example if it is a mid point or top right point, you can define them as follows

    void Svgfile::addRectangleTopRight(int x1,int y1,int width,int height, std::string color)
    {
       addRectangle(x1-width, x2, width, height, color) ;       
    }
    
    void Svgfile::addRectangleMid(int x1,int y1,int width,int height, std::string color)
    {
       addRectangle(x1-width/2, x2-height/2, width, height, color) ;       
    }