Object Oriented Programming in C++
Object Oriented Programming in C++
Object Oriented Programming in C++
Drawing::Point::Point()
{
this->itsX = 0.0;
this->itsY = 0.0;
}
Drawing::Point::Point(double x, double y)
{
this->itsX = x;
this->itsY = y;
}
std::ofstream matlabFile;
matlabFile.open("movedPoint.m");
matlabFile << "plot(" << this->itsX << "," << this->itsY << ", 'ro')\n";
matlabFile << "axis equal\n";
matlabFile << "grid on\n";
matlabFile << "set(gca, 'ytick', 0:10)";
matlabFile.close();
}
std::ofstream matlabFile;
matlabFile.open("rotatedPoint.m");
matlabFile << "plot(" << this->itsX << "," << this->itsY << ", 'ro')\n";
matlabFile << "axis equal\n";
matlabFile << "grid on\n";
matlabFile << "set(gca, 'ytick', 0:10)";
matlabFile.close();
}
Move Function
Moving point (5, 6) by 1 unit to the right and 2 units to the bottom.
Main code:
int main()
{
Point pointRed = Point{ 5,6 };
pointRed.draw();
pointRed.move(1,-2);
pointRed.draw();
return 0;
}
Graphical representation:
Rotate Function
Rotating the point (4, 2) by π/2 or 90° with respect of point (2, 3).
Main code:
int main()
{
Point pointRed = Point{ 4,2 };
pointRed.draw();
Point pointBlue = Point{ 2,3 };
pointBlue.draw();
pointRed.rotate(pointBlue, 90);
pointRed.draw();
return 0;
}
Graphical representation:
Drawing::Line::Line()
{
this->itsStartPoint = Point{ 0,0 };
this->itsEndPoint = Point{ 0,0 };
}
std::ofstream matlabFile;
matlabFile.open("movedLine.m");
matlabFile << "x = linspace(" << this->itsStartPoint.getX() << "," << this->itsEndPoint.getX() <<
");\n";
matlabFile << "y = linspace(" << this->itsStartPoint.getY() << "," << this->itsEndPoint.getY() <<
");\n";
matlabFile << "plot(x, y, 'r-');\n";
matlabFile << "axis equal\n";
matlabFile << "grid on\n";
matlabFile << "set(gca, 'ytick', 0:10)\n";
matlabFile.close();
}
std::ofstream matlabFile;
matlabFile.open("rotatedLine.m");
matlabFile << "x = linspace(" << this->itsStartPoint.getX() << "," << this->itsEndPoint.getX() <<
");\n";
matlabFile << "y = linspace(" << this->itsStartPoint.getY() << "," << this->itsEndPoint.getY() <<
");\n";
matlabFile << "plot(x, y, 'r-');\n";
matlabFile << "axis equal\n";
matlabFile << "grid on\n";
matlabFile << "set(gca, 'ytick', 0:10)\n";
matlabFile.close();
}
Move Function
Moving line (4, 2)->(6,3) by 3 units to the left and 2 units to the bottom.
Main code:
int main()
{
Line lineRed = Line{ Point{4,2}, Point{6,3} };
lineRed.draw();
lineRed.move(-3, -2);
lineRed.draw();
return 0;
}
Graphical representation:
Rotate Function
Rotating line (4, 2)->(6,3) by π/2 or 90° with respect of point (2, 3).
Main code:
int main()
{
Line lineRed = Line{ Point{4,2}, Point{6,3} };
lineRed.draw();
lineRed.rotate(Point{ 2, 3 }, 90);
lineRed.draw();
return 0;
}
Graphical representation:
std::ofstream matlabFile;
matlabFile.open("movedRectangle.m");
matlabFile << "x = linspace(" << this->itsSides[0].getStartPoint().getX() << "," << this-
>itsSides[0].getEndPoint().getX() << ");\n";
matlabFile << "y = linspace(" << this->itsSides[0].getStartPoint().getY() << "," << this-
>itsSides[0].getEndPoint().getY() << ");\n";
matlabFile << "plot(x,y,'r-');\n";
matlabFile << "hold on\n";
matlabFile << "x = linspace(" << this->itsSides[1].getStartPoint().getX() << "," << this-
>itsSides[1].getEndPoint().getX() << ");\n";
matlabFile << "y = linspace(" << this->itsSides[1].getStartPoint().getY() << "," << this-
>itsSides[1].getEndPoint().getY() << ");\n";
matlabFile << "plot(x,y,'r-');\n";
matlabFile << "hold on\n";
matlabFile << "x = linspace(" << this->itsSides[2].getStartPoint().getX() << "," << this-
>itsSides[2].getEndPoint().getX() << ");\n";
matlabFile << "y = linspace(" << this->itsSides[2].getStartPoint().getY() << "," << this-
>itsSides[2].getEndPoint().getY() << ");\n";
matlabFile << "plot(x,y,'r-');\n";
matlabFile << "hold on\n";
matlabFile << "x = linspace(" << this->itsSides[3].getStartPoint().getX() << "," << this-
>itsSides[3].getEndPoint().getX() << ");\n";
matlabFile << "y = linspace(" << this->itsSides[3].getStartPoint().getY() << "," << this-
>itsSides[3].getEndPoint().getY() << ");\n";
matlabFile << "plot(x,y,'r-');\n";
matlabFile << "axis equal\n";
matlabFile << "grid on\n";
matlabFile << "set(gca, 'ytick', 0:10)\n";
matlabFile.close();
}
}
Move Function
Moving rectangle with corners (6, 3), (10, 3), (10, 5) and (6, 5) by 2 units to the left and 1 unit
to the bottom.
Main code:
int main()
{
Rectangle rectRed = Rectangle{ Point{6,3} , Point{10,5} };
rectRed.draw();
rectRed.move(-2, -1);
rectRed.draw();
return 0;
}
Graphical representation:
Rotate Function
Rotating Rectangle with corners (6, 3), (10, 3), (10, 5) and (6, 5) by π/3 or 60° with respect of
point (3, 2).
Main code:
int main()
{
Rectangle rectRed = Rectangle{ Point{6,3} , Point{10,5} };
rectRed.draw();
Point pointBlue = Point{ 3, 2 };
pointBlue.draw();
rectRed.rotate(pointBlue, 60);
rectRed.draw();
return 0;
}
Graphical representation: