0% found this document useful (0 votes)
47 views11 pages

Object Oriented Programming in C++

This document describes the implementation of Point, Line, and Rectangle classes in C++ with methods to draw, move, and rotate geometric shapes. It includes code for the class definitions and implementations of common methods like draw(), move(), and rotate(). It also provides examples of using these classes and methods to move and rotate a point, line, and rectangle, outputting the results to the console and Matlab files for visualization of the transformations.

Uploaded by

Armando Lara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views11 pages

Object Oriented Programming in C++

This document describes the implementation of Point, Line, and Rectangle classes in C++ with methods to draw, move, and rotate geometric shapes. It includes code for the class definitions and implementations of common methods like draw(), move(), and rotate(). It also provides examples of using these classes and methods to move and rotate a point, line, and rectangle, outputting the results to the console and Matlab files for visualization of the transformations.

Uploaded by

Armando Lara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

HOMEWORK #1

José Armando Lara Ramos

Point class implementation


#define _USE_MATH_DEFINES
#include "Point.h"
#include <iostream>
#include <math.h>
#include <fstream>

Drawing::Point::Point()
{
this->itsX = 0.0;
this->itsY = 0.0;
}

Drawing::Point::Point(double x, double y)
{
this->itsX = x;
this->itsY = y;
}

void Drawing::Point::draw() const


{
std::cout << "X: " << this->itsX << " Y: " << this->itsY << std::endl;
}

void Drawing::Point::move(double dx, double dy)


{
this->itsX += dx;
this->itsY += dy;

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();
}

void Drawing::Point::rotate(Point const& center, double angle)


{
double angleRad = angle * M_PI / 180;
double tiltAng = atan((this->itsY - center.getY()) / (this->itsX - center.getX()));
double dist = sqrt(pow(this->itsX - center.getX(),2) + pow(this->itsY - center.getY(), 2));
this->itsX = (dist * cos(angleRad + tiltAng)) + center.getX();
this->itsY = (dist * sin(angleRad + tiltAng)) + center.getY();

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();
}

double Drawing::Point::getX() const


{
return this->itsX;
}

double Drawing::Point::getY() const


{
return this->itsY;
}

August 28, 2019 1


HOMEWORK #1
José Armando Lara Ramos

Point class testing

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;
}

Console and Matlab file output:

Graphical representation:

August 28, 2019 2


HOMEWORK #1
José Armando Lara Ramos

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;
}

Console and Matlab file output:

Graphical representation:

August 28, 2019 3


HOMEWORK #1
José Armando Lara Ramos

Line class implementation


#include "Line.h"
#include <iostream>
#include <fstream>

Drawing::Line::Line()
{
this->itsStartPoint = Point{ 0,0 };
this->itsEndPoint = Point{ 0,0 };
}

Drawing::Line::Line(Point const& p1, Point const& p2)


{
this->itsStartPoint = p1;
this->itsEndPoint = p2;
}

void Drawing::Line::draw() const


{
std::cout << "Line startPoint ";
this->itsStartPoint.draw();
std::cout << "Line endPoint ";
this->itsEndPoint.draw();
}

void Drawing::Line::move(double dx, double dy)


{
this->itsStartPoint.move(dx, dy);
this->itsEndPoint.move(dx, dy);

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();
}

void Drawing::Line::rotate(Point const& center, double angle)


{
this->itsStartPoint.rotate(center, angle);
this->itsEndPoint.rotate(center, angle);

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();
}

August 28, 2019 4


HOMEWORK #1
José Armando Lara Ramos

Line class testing

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;
}

Console and Matlab file output:

Graphical representation:

August 28, 2019 5


HOMEWORK #1
José Armando Lara Ramos

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;
}

Console and Matlab file output:

Graphical representation:

August 28, 2019 6


HOMEWORK #1
José Armando Lara Ramos

Rectangle class implementation


#include "Rectangle.h"
#include <iostream>
#include <fstream>

Drawing::Rectangle::Rectangle(Point const& p1, Point const& p2)


{
this->itsSides[0] = Line{ p1, Point{p2.getX(), p1.getY()} };
this->itsSides[1] = Line{ Point{p2.getX(), p1.getY()}, p2 };
this->itsSides[2] = Line{ p2, Point{p1.getX(), p2.getY()} };
this->itsSides[3] = Line{ Point{p1.getX(), p2.getY()}, p1 };
}

void Drawing::Rectangle::draw() const


{
for (int i=0; i < 4; i++)
{
std::cout << "Rectangle Line " << i+1 << std::endl;
this->itsSides[i].draw();
}
std::cout << std::endl;
}

void Drawing::Rectangle::move(double dx, double dy)


{
for (int i = 0; i < 4; i++)
{
this->itsSides[i].move(dx, dy);

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();
}
}

August 28, 2019 7


HOMEWORK #1
José Armando Lara Ramos

void Drawing::Rectangle::rotate(Point const& center, double angle)


{
for (int i = 0; i < 4; i++)
{
this->itsSides[i].rotate(center, angle);
}
std::ofstream matlabFile;
matlabFile.open("rotatedRectangle.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();
}

Rectangle class testing

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;
}

August 28, 2019 8


HOMEWORK #1
José Armando Lara Ramos

Console and Matlab file output:

Graphical representation:

August 28, 2019 9


HOMEWORK #1
José Armando Lara Ramos

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;
}

Console and Matlab file output:

August 28, 2019 10


HOMEWORK #1
José Armando Lara Ramos

Graphical representation:

August 28, 2019 11

You might also like