SlideShare a Scribd company logo
University Of Manchester
School of Physics and Astronomy
Object-Oriented Programming in C++ Project
Report
ListMyPolygons
Author:
Riccardo WIlliam Monfardini
Student ID: 7484334
October 29, 2013
Abstract
ListMyPolygons is a programme designed to input and modify con-
vex polygon details using the Translation, Rescale and Rotation trans-
formations. Details of the polygons are derived from their vertices; the
coordinates of them are saved using an independent matrix class; this
is designed to perform matrix arithmetic needed for transformations.
Polygons and input vertices use the STL vector container which can
grow and does not limit the number of these. Polymorphism is used
to access polygons saved as different derived classes depending on the
number of their vertices. The User interface has been designed to give
options on how to input and modify the polygons.
1 Introduction
The program ListMyPolygons is written using object-oriented programming
in C++. The aim of the program is to allow a user to input the details
of several different polygons, to make transformations such as translation,
rescaling and rotation, and display the information on the console screen. A
polygon is defined by its vertices and thus it is required to store these in a
sensible manner; it has been chosen to use matrices, specifically a 2 × 1 ma-
trix, to store the x and y coordinates of the vertices. The design of the matrix
class had to take into consideration the requirements needed to complete the
transformations, with particular attention dedicated to the rotation trans-
formation; this was obtained applying the rotation operator at each vertex,
defined by a square matrix.
2 Code design and Implementation
The code design can be divided into 3 main entities:
• matrix class,
• polygon class, and
• user interface,
and therefore a detailed explanation of each is given.
2.1 Matrix Class
As mentioned a matrix class was designed; displayed in Figure 1 is a di-
agram which points out the matrix class features of interest used in the
ListMyPolygons program. The variables of a matrix are the number of rows
1
and columns, and the value of each element of the matrix. For the purposes
of ListMyPolygons two constructors were used:
• a 2 × 1 matrix constructor used for vertices, and the translation vector
which will be discussed later in section 2.2; this will accept and store
the 2 coordinates, x and y.
• a 2×2 matrix constructor used for the rotation operator square matrix.
Constructing the new matrix class required the overloading of the addi-
tion and multiplication operators because, as shown in Figure 1, these two
operators are connected to the transformation functions. The overloaded
operators have checks to insure that the size of the matrices are compatible
for the upcoming operation. Matrix addition can occur only when they are
the same size while matrix multiplication can occur only when the number
of columns of the left matrix are equal to the number of rows of the right
matrix. The addition operator added element to element; multiplication by a
constant multiplied each element by a factor; matrix multiplication returned
the product matrix according to the standard rules. Even if this program
is designed only for 2D polygons the matrix class, which was constructed
independently, can be extended to greater dimensions by updating the con-
structors; the overloaded functions are programmed to work correctly with
matrices of any dimensions.
Figure 1: Diagram illustrating matrix class components. Notice how the matrix addi-
tion, matrix multiplication and multiply by a constant functions are linked to the external
transformation functions.
2
2.2 Polygon Class
The second main entity in the design of ListMyPolygons is the polygon class
itself. Figure 2 shows a schematic representation of the design of this entity.
An abstract class named shape was designed to be the parent of all possible
polygons. From this, 5 derived classes were defined: trigon, tetragon, pen-
tagon, hexagon, and a generic polygon . The use of an abstract class allows
the exploitation of the polymorphism feature of object oriented C++: the
abstract class can be used as pointer to access the functions of the derived
classes which are defined with the same name.
Because the derived polygon classes all had similar functions, only a
schematic representation of the tetragon class is shown in Figure 2. The
polygons can be constructed in 2 ways: with the individual matrix of each
vertex or the std::vector container formed by vertex matrices. The con-
structor determined the values of the other parameters: the arrays side and
diagonal were determined by the difference between vertices. They used two
functions:
• diagonals, which return the number of possible diagonals (NofD) given
the number of vertices (NofV), and
• magnitude, which returned the magnitude of a vector, a row matrix.
An exception is made for the polygon class. Used as a class for polygons with
more than 6 vertices, it can only be constructed by a vector of matrices; this
allows the program to define NofV by its size. Thanks to this information
the type of polygon is found, such as Isosceles Triangle, Rectangle, Irregular
Hexagon, Irregular Polygon etc. Regular polygons are determined as well, for
example Equilateral Triangle, Regular Pentagon etc.; however these polygons
were found to be unreliable, as is discussed in section 3. The function info
is designed to output the details of the polygon onto the console screen for
the user to review.
The complex feature of these classes is the transformation functions,
Translation, Rescale and Rotation. As illustrated by Figure 2 Rescale and
Rotation are dependent on Translation and the function centroid. Transla-
tion uses a 2 × 1 matrix vector which is added to each vertex thanks to the
matrix addition discussed previously. Rescale and Rotation, however, have to
be performed with respect to a point or axis. Thus, before performing these
transformations, the polygon centre, found through the centroid function, is
translated to the origin. At this point the vertices are rescaled or rotated by
multiplying by a factor or the rotation matrix operator respectively. Finally
the polygons are translated back to their original position.
3
Abstract Base
Class Shape
Class Trigon;
NofV, vertices, side
NofD, diagonal, type
Class Tetragon;
NofV, vertices, side
NofD, diagonal, type
Class Pentagon;
NofV, vertices, side
NofD, diagonal, type
Class Polygon;
NofV, vertices, side
NofD, diagonal, type
trigon( A, B, C, D)
trigon( vector<matrix> vertices )
diagonals()
centroid()
Translation
Rotation
Rescale
vector
Matrix Addition
Multiply by
a constant
Matrix Multiplication
factor
rotation
matrix operator
Constructors
Translated to the origin
rescaling and roattion
with respect to the centre
Tetragon used as an example
other classes have a similar
configuraition
info()
magintude()
Class Hexagon;
NofV, vertices, side
NofD, diagonal, type
Derivate classes
Figure 2: Diagram illustrating the Shape class and its derived classes Trigon, Tetragon,
Pentagon and Hexagon, and the generic Polygon. Tetragon is used as an example since
all derived classes have a similar configuration. The dashed line figures indicate elements
which come from outside the class. Notice how Rescale and Rotation transformations are
dependent on Translation and the centroid functions.
4
2.3 User Interface
The last entity of the program is the user interface (UI). The first part of
the UI is to ask the user to input the details of the polygon to be created;
this is illustrated by Figure 3. After a short introduction message, the user
chooses between two input methods. In method 1 he inputs the coordinates
of the vertices which are stored in a std::vector of matrices inputvertices.
The user can choose the NofV a priori or decide after each input if he wants
another vertex. Method 2 gives a short list of polygons to choose from:
one generic polygon and the 4 specific polygons, which copy method 1 but
with a undefined and predetermined NofV respectively; more interesting are
the options Rectangle, Isosceles Triangle and Square, which, based on User
input of base and height or side length, creates the std::vector of matrices
inputvertices placing one at the origin. The program creates the polygon of
class based on the size of inputvertices.
The second part of the UI, illustrated in Figure 4, allows the User to
review and modify the polygons created. The created polygon is stored
in a std::vector of shape pointers called polygons. Using the info function
previously mentioned, information about the polygon is printed to the screen.
The user is then asked if he wants to modify that newly created polygon.
A positive answer allows transformation of the polygon and printing of the
details, and the User is asked if he wants to modify it again. The destroy
option will delete the polygon. A negative answer allows the user to input a
new polygon with the same method or choose a different one. At the end of
the program all information about the polygons inserted are printed to the
screen for a final review, modification or destruction.
5
Introduction to the program
SaveMyPolygons
Choose
input
method
input each vertex
Do you know
how many
vertices?
Input vertex coordinates
Another
vertex?
Isoceles
Triangle
choose
from a list
SquareRectangle
Pentagon
Hexagon
yes
vector<matrix>
inputvertices
Determine polygon
depending on size
inputvertices
input number
of vertices
Input vertex coordinates
Create Polygon
Input base
and height
input
side
Place one vertex at origin and
determine the position
of other vertices
no
1 2
no
yes
Trigon
Tetragon
Polygon
Figure 3: Flowchart illustrating the first part of the User Interface, input of the polygon
details.
6
Figure 4: Flowchart illustrating the first part of the User Interface, reviewing and modi-
fication of the polygon details.
7
3 Results & Limitations
The ListMyPolygons results and limitations are better explained with an
example of a User utilising the program.
After a greeting message the User is prompted to choose the input method,
shown in figure 5. Here it is requested to input the vertices, whenever asked,
Figure 5: Choosing the input method with a warning message.
in the same way the User would draw them. In fact the program allows the
input of concave polygons. Thus he is advised to use the program only for
convex polygons. The User chooses the input method 1 which asks him if
he knows how many vertices he would like to input. The program will loop
NofV times a request of vertex coordinates input if defined; otherwise after 3
requests it will ask the User if he wants another vertex. In this case, shown
in Figure 6 the user has chosen 3 NofV. The User has entered the vertices of
Figure 6: Input of the vertices and details of the polygon created.
an Isosceles Triangle at (0, 0), (5, 0), and (2.5, 2.5). Notice how the program
printed an error message when an invalid type is given. The program has
been designed to check the inputs to determine if they are the right boolean,
8
integer or double type, using the std::cin goodbit flag. Then the polygon is
created and its details are printed to the console screen. The same figure can
be created using input method 2, as is shown in Figure 7. After the creation
Figure 7: Creating an Isosceles Triangle as in Figure 6 using method 2.
of each polygon the User is prompted to choose a modify option. Figure
8 shows the User rescaling the Isosceles Triangle by a factor of 2 and then
translating it by (−5, 0). Notice how the User chooses a bad option with
the input of 4; this was refused by the program. All choosing options are
checked, apart for the integer type check, to determine if the specific option
exists. During the rescaling transformation the centre has not changed, as
discussed in section 2.2, since the function embeds a translation to the origin
and back.
The User created a rectangle which he decided to rotate by −π/2, as
shown in Figure 9. Here the user had to input the rotation operator matrix,
which in this case is ( 0 1
−1 0 ). As in the rescaling transformation the centre has
not changed because of the embedded translation to the origin and back.
9
Figure 8: User rescales the Isosceles Triangle and translates it.
Figure 9: The User creates and rotates a rectangle
10
In Figure 10 the User enters a pentagon but inputs the wrong vertices.
Checking the polygon details it is seen that it describes a concave polygon,
and 2 vertices are one on top of each other. At this point, the option destroy
Figure 10: User notices that input vertices are incorrect and decides to destroy the last
polygon.
allows him to delete the polygon. This option uses the vector::pop back()
function to eliminate the last polygon which was saved, since polygons are
saved using vector::push back(). This is possible because the polygons are
stored in a vector shape pointer (std::vector<shape*>) called “polygons”;
an extra advantage of using vectors is that their size can change dynamically
and thus the User is not restricted by a predetermined maximum amount of
polygons.
Finally, after the insertion of all the polygons wanted by the user, the
complete list is printed to the screen as is shown in figure 11. Here each
polygon is numbered; in case the user wishes to modify any of the polygons,
he can use the number to select it. The end of the program is reached when
a negative answer is given to the last modify question.
Thus summarising, the code takes advantage of advanced Object Oriented
programming in C++ features in several ways:
• Polymorphism, where an abstract class shape is used to access all
derived polygon class functions.
• Templates, where a valCheck function is used to check that the input
value is of the right type; this is done by checking the state of the
std::cin goodbit flag.
• Separation of Entities, where the matrix and polygon class are sep-
arated and each with its own header file.
11
Figure 11: List of all the input polygons.
12
4 Conclusion & Discussion
The ListMyPolygons program was written to allow a User to insert several
polygons, modify them using the Translation, Rescale and Rotation trans-
formations. A polygon is essentially defined by the position of its vertices.
Thus other variables like side lengths, diagonal lengths, type of polygon were
determined from the vertices. The vertices were stored using a specifically de-
signed matrix class. This allowed matrix addition and multiplication, which
was particularly important with the matrix rotation operator in the Rota-
tion transformation. Both the vertices and the polygons were stored using
the vector STL container; this had the advantage of being able to grow, thus
not restricting the number of polygons stored or the number of vertices a
polygon could have.
The program could be improved by implementing 3D polygons. This
could be done by a small modification of the matrix class to allow 3 × 1 and
3 × 3 matrices. The 3D polygon classes could inherit from the 2D classes
already established. A correction of the truncation errors could improve the
reliability of regular polygons such as Equilateral Triangle. The program
could determine if a figure is concave to exclude them or use separate classes
to save them in.
13

More Related Content

What's hot

Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters  Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
Nakul Sharma
 
Intake 38 6
Intake 38 6Intake 38 6
Intake 38 6
Mahmoud Ouf
 
Face recognition using selected topographical features
Face recognition using selected topographical features Face recognition using selected topographical features
Face recognition using selected topographical features
IJECEIAES
 
Mapping and visualization of source code a survey
Mapping and visualization of source code a surveyMapping and visualization of source code a survey
Mapping and visualization of source code a survey
Nakul Sharma
 
Cs8251 faq1
Cs8251 faq1Cs8251 faq1
Cs8251 faq1
tamilvanan76
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunEngr. Adefami Segun, MNSE
 
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
ITIIIndustries
 
Text Independent Speaker Identification Using Imfcc Integrated With Ica
Text Independent Speaker Identification Using Imfcc Integrated With IcaText Independent Speaker Identification Using Imfcc Integrated With Ica
Text Independent Speaker Identification Using Imfcc Integrated With Ica
IOSR Journals
 
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...Aravind NC
 
Ppt chapter02
Ppt chapter02Ppt chapter02
Ppt chapter02
Richard Styner
 
Matlab 1 level_1
Matlab 1 level_1Matlab 1 level_1
Matlab 1 level_1
Ahmed Farouk
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
Mahmoud Ouf
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
Richard Styner
 
Geographic Information System unit 4
Geographic Information System   unit 4Geographic Information System   unit 4
Geographic Information System unit 4
sridevi5983
 
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
ijaia
 

What's hot (16)

Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters  Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
 
Intake 38 6
Intake 38 6Intake 38 6
Intake 38 6
 
Face recognition using selected topographical features
Face recognition using selected topographical features Face recognition using selected topographical features
Face recognition using selected topographical features
 
Mapping and visualization of source code a survey
Mapping and visualization of source code a surveyMapping and visualization of source code a survey
Mapping and visualization of source code a survey
 
Cs8251 faq1
Cs8251 faq1Cs8251 faq1
Cs8251 faq1
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami Olusegun
 
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
Bayesian-Network-Based Algorithm Selection with High Level Representation Fee...
 
Text Independent Speaker Identification Using Imfcc Integrated With Ica
Text Independent Speaker Identification Using Imfcc Integrated With IcaText Independent Speaker Identification Using Imfcc Integrated With Ica
Text Independent Speaker Identification Using Imfcc Integrated With Ica
 
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
 
Ppt chapter02
Ppt chapter02Ppt chapter02
Ppt chapter02
 
Matlab 1 level_1
Matlab 1 level_1Matlab 1 level_1
Matlab 1 level_1
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
 
Geographic Information System unit 4
Geographic Information System   unit 4Geographic Information System   unit 4
Geographic Information System unit 4
 
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
 

Viewers also liked

Swap Administration in linux platform
Swap Administration in linux platformSwap Administration in linux platform
Swap Administration in linux platform
ashutosh123gupta
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
Enter center c++ oop chapter 2
Enter center c++ oop chapter 2Enter center c++ oop chapter 2
Enter center c++ oop chapter 2Vuthea Chheang
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
krismishra
 
History of c++
History of c++ History of c++
History of c++
Ihsan Wassan
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
Dreamtech Labs
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
Rome468
 
Oop project report using c++
Oop project report using c++Oop project report using c++
Oop project report using c++
Lameck Elias
 
Linux System Administration Crash Course
Linux System Administration Crash CourseLinux System Administration Crash Course
Linux System Administration Crash Course
Jason Cannon
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
Linux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScITLinux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScIT
vignesh0009
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
k v
 
Project report
Project reportProject report
Project report
meenalpandey
 
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
WE-IT TUTORIALS
 
c++ report file for theatre management project
c++ report file for theatre management projectc++ report file for theatre management project
c++ report file for theatre management project
Rajesh Gangireddy
 
linux interview questions and answers
linux interview questions and answerslinux interview questions and answers
linux interview questions and answers
Ganapathi Raju
 
Deep C Programming
Deep C ProgrammingDeep C Programming
Deep C Programming
Wang Hao Lee
 

Viewers also liked (20)

Swap Administration in linux platform
Swap Administration in linux platformSwap Administration in linux platform
Swap Administration in linux platform
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Enter center c++ oop chapter 2
Enter center c++ oop chapter 2Enter center c++ oop chapter 2
Enter center c++ oop chapter 2
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
History of c++
History of c++ History of c++
History of c++
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
Oop project report using c++
Oop project report using c++Oop project report using c++
Oop project report using c++
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Linux System Administration Crash Course
Linux System Administration Crash CourseLinux System Administration Crash Course
Linux System Administration Crash Course
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Linux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScITLinux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScIT
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Project report
Project reportProject report
Project report
 
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
 
c++ report file for theatre management project
c++ report file for theatre management projectc++ report file for theatre management project
c++ report file for theatre management project
 
linux interview questions and answers
linux interview questions and answerslinux interview questions and answers
linux interview questions and answers
 
Deep C Programming
Deep C ProgrammingDeep C Programming
Deep C Programming
 

Similar to ListMyPolygons 0.6

Human Head Counting and Detection using Convnets
Human Head Counting and Detection using ConvnetsHuman Head Counting and Detection using Convnets
Human Head Counting and Detection using Convnets
rahulmonikasharma
 
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
gerogepatton
 
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
csandit
 
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
IRJET Journal
 
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...Sreekanth Sivaraman
 
IRJET- Classification of Hindi Maatras by Encoding Scheme
IRJET- Classification of Hindi Maatras by Encoding SchemeIRJET- Classification of Hindi Maatras by Encoding Scheme
IRJET- Classification of Hindi Maatras by Encoding Scheme
IRJET Journal
 
Semantic Segmentation Of Aerial Image.pptx
Semantic Segmentation Of Aerial Image.pptxSemantic Segmentation Of Aerial Image.pptx
Semantic Segmentation Of Aerial Image.pptx
BikashSingh913806
 
Reconfigurable Design of Rectangular to Polar Converter using Linear Convergence
Reconfigurable Design of Rectangular to Polar Converter using Linear ConvergenceReconfigurable Design of Rectangular to Polar Converter using Linear Convergence
Reconfigurable Design of Rectangular to Polar Converter using Linear Convergence
AnuragVijayAgrawal
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
module 3.4 Variables.ppt
module 3.4 Variables.pptmodule 3.4 Variables.ppt
module 3.4 Variables.ppt
ssuserd973fe
 
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
IJCI JOURNAL
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
Editor IJCATR
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
Editor IJCATR
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
Editor IJCATR
 
A High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
A High Speed Transposed Form FIR Filter Using Floating Point Dadda MultiplierA High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
A High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
IJRES Journal
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
IRJET Journal
 
Ooad quiz
Ooad quizOoad quiz

Similar to ListMyPolygons 0.6 (20)

Human Head Counting and Detection using Convnets
Human Head Counting and Detection using ConvnetsHuman Head Counting and Detection using Convnets
Human Head Counting and Detection using Convnets
 
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
A BI-OBJECTIVE MODEL FOR SVM WITH AN INTERACTIVE PROCEDURE TO IDENTIFY THE BE...
 
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
A FLOATING POINT DIVISION UNIT BASED ON TAYLOR-SERIES EXPANSION ALGORITHM AND...
 
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
IRJET - Design of a Low Power Serial- Parallel Multiplier with Low Transition...
 
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...
A VBA Based Computer Program for Nonlinear FEA of Large Displacement 2D Beam ...
 
IRJET- Classification of Hindi Maatras by Encoding Scheme
IRJET- Classification of Hindi Maatras by Encoding SchemeIRJET- Classification of Hindi Maatras by Encoding Scheme
IRJET- Classification of Hindi Maatras by Encoding Scheme
 
Semantic Segmentation Of Aerial Image.pptx
Semantic Segmentation Of Aerial Image.pptxSemantic Segmentation Of Aerial Image.pptx
Semantic Segmentation Of Aerial Image.pptx
 
Reconfigurable Design of Rectangular to Polar Converter using Linear Convergence
Reconfigurable Design of Rectangular to Polar Converter using Linear ConvergenceReconfigurable Design of Rectangular to Polar Converter using Linear Convergence
Reconfigurable Design of Rectangular to Polar Converter using Linear Convergence
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
module 3.4 Variables.ppt
module 3.4 Variables.pptmodule 3.4 Variables.ppt
module 3.4 Variables.ppt
 
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
BARRACUDA, AN OPEN SOURCE FRAMEWORK FOR PARALLELIZING DIVIDE AND CONQUER ALGO...
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
 
Duplicate Code Detection using Control Statements
Duplicate Code Detection using Control StatementsDuplicate Code Detection using Control Statements
Duplicate Code Detection using Control Statements
 
A High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
A High Speed Transposed Form FIR Filter Using Floating Point Dadda MultiplierA High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
A High Speed Transposed Form FIR Filter Using Floating Point Dadda Multiplier
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
IRJET- Automatic Detection of Characteristics of Clothing using Image Process...
 
Ooad quiz
Ooad quizOoad quiz
Ooad quiz
 

ListMyPolygons 0.6

  • 1. University Of Manchester School of Physics and Astronomy Object-Oriented Programming in C++ Project Report ListMyPolygons Author: Riccardo WIlliam Monfardini Student ID: 7484334 October 29, 2013
  • 2. Abstract ListMyPolygons is a programme designed to input and modify con- vex polygon details using the Translation, Rescale and Rotation trans- formations. Details of the polygons are derived from their vertices; the coordinates of them are saved using an independent matrix class; this is designed to perform matrix arithmetic needed for transformations. Polygons and input vertices use the STL vector container which can grow and does not limit the number of these. Polymorphism is used to access polygons saved as different derived classes depending on the number of their vertices. The User interface has been designed to give options on how to input and modify the polygons. 1 Introduction The program ListMyPolygons is written using object-oriented programming in C++. The aim of the program is to allow a user to input the details of several different polygons, to make transformations such as translation, rescaling and rotation, and display the information on the console screen. A polygon is defined by its vertices and thus it is required to store these in a sensible manner; it has been chosen to use matrices, specifically a 2 × 1 ma- trix, to store the x and y coordinates of the vertices. The design of the matrix class had to take into consideration the requirements needed to complete the transformations, with particular attention dedicated to the rotation trans- formation; this was obtained applying the rotation operator at each vertex, defined by a square matrix. 2 Code design and Implementation The code design can be divided into 3 main entities: • matrix class, • polygon class, and • user interface, and therefore a detailed explanation of each is given. 2.1 Matrix Class As mentioned a matrix class was designed; displayed in Figure 1 is a di- agram which points out the matrix class features of interest used in the ListMyPolygons program. The variables of a matrix are the number of rows 1
  • 3. and columns, and the value of each element of the matrix. For the purposes of ListMyPolygons two constructors were used: • a 2 × 1 matrix constructor used for vertices, and the translation vector which will be discussed later in section 2.2; this will accept and store the 2 coordinates, x and y. • a 2×2 matrix constructor used for the rotation operator square matrix. Constructing the new matrix class required the overloading of the addi- tion and multiplication operators because, as shown in Figure 1, these two operators are connected to the transformation functions. The overloaded operators have checks to insure that the size of the matrices are compatible for the upcoming operation. Matrix addition can occur only when they are the same size while matrix multiplication can occur only when the number of columns of the left matrix are equal to the number of rows of the right matrix. The addition operator added element to element; multiplication by a constant multiplied each element by a factor; matrix multiplication returned the product matrix according to the standard rules. Even if this program is designed only for 2D polygons the matrix class, which was constructed independently, can be extended to greater dimensions by updating the con- structors; the overloaded functions are programmed to work correctly with matrices of any dimensions. Figure 1: Diagram illustrating matrix class components. Notice how the matrix addi- tion, matrix multiplication and multiply by a constant functions are linked to the external transformation functions. 2
  • 4. 2.2 Polygon Class The second main entity in the design of ListMyPolygons is the polygon class itself. Figure 2 shows a schematic representation of the design of this entity. An abstract class named shape was designed to be the parent of all possible polygons. From this, 5 derived classes were defined: trigon, tetragon, pen- tagon, hexagon, and a generic polygon . The use of an abstract class allows the exploitation of the polymorphism feature of object oriented C++: the abstract class can be used as pointer to access the functions of the derived classes which are defined with the same name. Because the derived polygon classes all had similar functions, only a schematic representation of the tetragon class is shown in Figure 2. The polygons can be constructed in 2 ways: with the individual matrix of each vertex or the std::vector container formed by vertex matrices. The con- structor determined the values of the other parameters: the arrays side and diagonal were determined by the difference between vertices. They used two functions: • diagonals, which return the number of possible diagonals (NofD) given the number of vertices (NofV), and • magnitude, which returned the magnitude of a vector, a row matrix. An exception is made for the polygon class. Used as a class for polygons with more than 6 vertices, it can only be constructed by a vector of matrices; this allows the program to define NofV by its size. Thanks to this information the type of polygon is found, such as Isosceles Triangle, Rectangle, Irregular Hexagon, Irregular Polygon etc. Regular polygons are determined as well, for example Equilateral Triangle, Regular Pentagon etc.; however these polygons were found to be unreliable, as is discussed in section 3. The function info is designed to output the details of the polygon onto the console screen for the user to review. The complex feature of these classes is the transformation functions, Translation, Rescale and Rotation. As illustrated by Figure 2 Rescale and Rotation are dependent on Translation and the function centroid. Transla- tion uses a 2 × 1 matrix vector which is added to each vertex thanks to the matrix addition discussed previously. Rescale and Rotation, however, have to be performed with respect to a point or axis. Thus, before performing these transformations, the polygon centre, found through the centroid function, is translated to the origin. At this point the vertices are rescaled or rotated by multiplying by a factor or the rotation matrix operator respectively. Finally the polygons are translated back to their original position. 3
  • 5. Abstract Base Class Shape Class Trigon; NofV, vertices, side NofD, diagonal, type Class Tetragon; NofV, vertices, side NofD, diagonal, type Class Pentagon; NofV, vertices, side NofD, diagonal, type Class Polygon; NofV, vertices, side NofD, diagonal, type trigon( A, B, C, D) trigon( vector<matrix> vertices ) diagonals() centroid() Translation Rotation Rescale vector Matrix Addition Multiply by a constant Matrix Multiplication factor rotation matrix operator Constructors Translated to the origin rescaling and roattion with respect to the centre Tetragon used as an example other classes have a similar configuraition info() magintude() Class Hexagon; NofV, vertices, side NofD, diagonal, type Derivate classes Figure 2: Diagram illustrating the Shape class and its derived classes Trigon, Tetragon, Pentagon and Hexagon, and the generic Polygon. Tetragon is used as an example since all derived classes have a similar configuration. The dashed line figures indicate elements which come from outside the class. Notice how Rescale and Rotation transformations are dependent on Translation and the centroid functions. 4
  • 6. 2.3 User Interface The last entity of the program is the user interface (UI). The first part of the UI is to ask the user to input the details of the polygon to be created; this is illustrated by Figure 3. After a short introduction message, the user chooses between two input methods. In method 1 he inputs the coordinates of the vertices which are stored in a std::vector of matrices inputvertices. The user can choose the NofV a priori or decide after each input if he wants another vertex. Method 2 gives a short list of polygons to choose from: one generic polygon and the 4 specific polygons, which copy method 1 but with a undefined and predetermined NofV respectively; more interesting are the options Rectangle, Isosceles Triangle and Square, which, based on User input of base and height or side length, creates the std::vector of matrices inputvertices placing one at the origin. The program creates the polygon of class based on the size of inputvertices. The second part of the UI, illustrated in Figure 4, allows the User to review and modify the polygons created. The created polygon is stored in a std::vector of shape pointers called polygons. Using the info function previously mentioned, information about the polygon is printed to the screen. The user is then asked if he wants to modify that newly created polygon. A positive answer allows transformation of the polygon and printing of the details, and the User is asked if he wants to modify it again. The destroy option will delete the polygon. A negative answer allows the user to input a new polygon with the same method or choose a different one. At the end of the program all information about the polygons inserted are printed to the screen for a final review, modification or destruction. 5
  • 7. Introduction to the program SaveMyPolygons Choose input method input each vertex Do you know how many vertices? Input vertex coordinates Another vertex? Isoceles Triangle choose from a list SquareRectangle Pentagon Hexagon yes vector<matrix> inputvertices Determine polygon depending on size inputvertices input number of vertices Input vertex coordinates Create Polygon Input base and height input side Place one vertex at origin and determine the position of other vertices no 1 2 no yes Trigon Tetragon Polygon Figure 3: Flowchart illustrating the first part of the User Interface, input of the polygon details. 6
  • 8. Figure 4: Flowchart illustrating the first part of the User Interface, reviewing and modi- fication of the polygon details. 7
  • 9. 3 Results & Limitations The ListMyPolygons results and limitations are better explained with an example of a User utilising the program. After a greeting message the User is prompted to choose the input method, shown in figure 5. Here it is requested to input the vertices, whenever asked, Figure 5: Choosing the input method with a warning message. in the same way the User would draw them. In fact the program allows the input of concave polygons. Thus he is advised to use the program only for convex polygons. The User chooses the input method 1 which asks him if he knows how many vertices he would like to input. The program will loop NofV times a request of vertex coordinates input if defined; otherwise after 3 requests it will ask the User if he wants another vertex. In this case, shown in Figure 6 the user has chosen 3 NofV. The User has entered the vertices of Figure 6: Input of the vertices and details of the polygon created. an Isosceles Triangle at (0, 0), (5, 0), and (2.5, 2.5). Notice how the program printed an error message when an invalid type is given. The program has been designed to check the inputs to determine if they are the right boolean, 8
  • 10. integer or double type, using the std::cin goodbit flag. Then the polygon is created and its details are printed to the console screen. The same figure can be created using input method 2, as is shown in Figure 7. After the creation Figure 7: Creating an Isosceles Triangle as in Figure 6 using method 2. of each polygon the User is prompted to choose a modify option. Figure 8 shows the User rescaling the Isosceles Triangle by a factor of 2 and then translating it by (−5, 0). Notice how the User chooses a bad option with the input of 4; this was refused by the program. All choosing options are checked, apart for the integer type check, to determine if the specific option exists. During the rescaling transformation the centre has not changed, as discussed in section 2.2, since the function embeds a translation to the origin and back. The User created a rectangle which he decided to rotate by −π/2, as shown in Figure 9. Here the user had to input the rotation operator matrix, which in this case is ( 0 1 −1 0 ). As in the rescaling transformation the centre has not changed because of the embedded translation to the origin and back. 9
  • 11. Figure 8: User rescales the Isosceles Triangle and translates it. Figure 9: The User creates and rotates a rectangle 10
  • 12. In Figure 10 the User enters a pentagon but inputs the wrong vertices. Checking the polygon details it is seen that it describes a concave polygon, and 2 vertices are one on top of each other. At this point, the option destroy Figure 10: User notices that input vertices are incorrect and decides to destroy the last polygon. allows him to delete the polygon. This option uses the vector::pop back() function to eliminate the last polygon which was saved, since polygons are saved using vector::push back(). This is possible because the polygons are stored in a vector shape pointer (std::vector<shape*>) called “polygons”; an extra advantage of using vectors is that their size can change dynamically and thus the User is not restricted by a predetermined maximum amount of polygons. Finally, after the insertion of all the polygons wanted by the user, the complete list is printed to the screen as is shown in figure 11. Here each polygon is numbered; in case the user wishes to modify any of the polygons, he can use the number to select it. The end of the program is reached when a negative answer is given to the last modify question. Thus summarising, the code takes advantage of advanced Object Oriented programming in C++ features in several ways: • Polymorphism, where an abstract class shape is used to access all derived polygon class functions. • Templates, where a valCheck function is used to check that the input value is of the right type; this is done by checking the state of the std::cin goodbit flag. • Separation of Entities, where the matrix and polygon class are sep- arated and each with its own header file. 11
  • 13. Figure 11: List of all the input polygons. 12
  • 14. 4 Conclusion & Discussion The ListMyPolygons program was written to allow a User to insert several polygons, modify them using the Translation, Rescale and Rotation trans- formations. A polygon is essentially defined by the position of its vertices. Thus other variables like side lengths, diagonal lengths, type of polygon were determined from the vertices. The vertices were stored using a specifically de- signed matrix class. This allowed matrix addition and multiplication, which was particularly important with the matrix rotation operator in the Rota- tion transformation. Both the vertices and the polygons were stored using the vector STL container; this had the advantage of being able to grow, thus not restricting the number of polygons stored or the number of vertices a polygon could have. The program could be improved by implementing 3D polygons. This could be done by a small modification of the matrix class to allow 3 × 1 and 3 × 3 matrices. The 3D polygon classes could inherit from the 2D classes already established. A correction of the truncation errors could improve the reliability of regular polygons such as Equilateral Triangle. The program could determine if a figure is concave to exclude them or use separate classes to save them in. 13