You are on page 1of 16

Coursework 2

ECSI405 Software Development Principles (C++)

Coursework 2

Module Leader: Mrs. Udayangi Perera Miss. Hiranya Mudunkotuwa


By: R.A.Thushara Kasun Ranawaka Student ID: 2010066
0|Page

Coursework 2 Contents
1. 0 : Introduction .........................................................................................................page 1 1.1 : 2.0 Background

: Program Polygon ..............................page 2 - 14 2.1 :Design(flow chart) 2.2 :polygon code

2.3 : Test plan 2.3.1 : Test plan 2.4 :Test results 2.3.1 : screen shots 3.0 :Conclusion ......................................................................................page 15-17

1|Page

Coursework 2

Acknowledgment.
Thank you very much everyone who help me to deliver this program

1.0 Introduction
1.1 Background polygon definition
Polygons are 2-dimensional shapes. They are made of straight lines, and the shape is "closed" (all the lines connect up).

Polygon (straight sides)

Not a Polygon (has a curve)

Not a Polygon (open, not closed)

Types of Polygons
A simple polygon has only one boundary, and it doesn't cross over itself. A complex polygon intersects itself!

Simple Polygon (this one's a Pentagon)

Complex Polygon (also a Pentagon)

A convex polygon has no angles pointing inwards. More precisely, no internal angles can be more than 180.

2|Page

Coursework 2
If there are any internal angles greater than 180 then it is concave. (Think: concave has a "cave" in it)

Convex

Concave

If all angles are equal and all sides are equal, then it is regular, otherwise it is irregular

Regular

Irregular

Complex Polygon (a "star polygon", in this case, a pentagram)

Concave Octagon

Irregular Hexagon

The program In the program 1st it takes how many sides are there in the polygon user going to calculate boundary. Then the program takes coordinates according to user entered side value. After the 2nd input program will print the line length between last 2 coordinates. End of the program it display that whether its a valid polygon or not and if its valid it will display the boundary of the polygon.

3|Page

Coursework 2 2.0
2.1 Design
Start

Program Polygon

Prompt"How many sides"

Accpect "side"

validate

i<side yes i==side-1 no no get point "i" Validate Validate create point"i" create point "i" create line "i-1" i==0 yes

no

get point "i"

Validate yes get point"i" create point "i"

create line"i-1" create line"i" display length of line"i-1"

display length of line"i-1"

lengthA+=length"i" +length"i-1";

lengthB+=length"i-1";

boundary=lengthA+lengthB

yes display "two lines intersecting" yes display "this is not a polygon"

checking intersecting no Checking is a polygon

no display "boundary"

End

4|Page

Coursework 2

2.2:Source code
Main.cpp
/*Calculate Polygon boundary *Thushara Kasun Ranawaka *28th of August 2011 */ #include<iostream> #include<limits> #include"line.h" #include"point.h" using namespace std; bool validation(Point p1,Point p2,Line lin); void star(); void uLine(); void main() { int side=0; double x=0.0,y=0.0,lengthA=0.0,lengthB=0.0; double boundry=0.0; Point arrayP[6]; Line arrayL[6]; cout<<"\t\t\t<<<<< POLYGON >>>>>\n"; uLine(); cout<<" In geometry a polygon is traditionaliny a plane figure\n"; cout<<"that is boundryed by a closed path or circuit, composed of a finite \n"; cout<<"sequence of straight line segments (i.e., by a closed polygonal \n"; cout<<"chain). These segments are calined its edges or sides, and the \n"; cout<<"points where two edges meet are the polygon's vertices (singular: \n"; cout<<"vertex) or corners. An n-gon is a polygon with n sides. The interior \n"; cout<<"of the polygon is sometimes calined its body. A polygon is a 2-\n"; cout<<"dimensional example of the more general polytope in any sideber \n"; cout<<"of dimensions.\n"; uLine(); star(); cout<<"How many sides in your polygon:"; cin>>side; cout<<endl; while(!cin||side>6||side<3) { cout<<"Invalid input! Please re-enter:";

5|Page

Coursework 2
cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>side; cout<<endl; } for(int i=0;i<side;i++) { if(i==side-1) { cout<<"Enter coordinates for P"<<i+1<<" (x y):"; cin>>x; while(!cin) { cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>x; } cin>>y; while(!cin) { cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>y; } arrayP[i].create(x,y); arrayL[i-1].create(arrayP[i-1],arrayP[i]); arrayL[i-1].displayL(i); arrayL[i].create(arrayP[i],arrayP[0]); arrayL[i].displayL(i+1); lengthA+=arrayL[i].length()+arrayL[i-1].length(); } else if (i==0) { cout<<"Enter coordinates for P"<<i+1<<" (x y):"; cin>>x; while(!cin) { cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>x; } cin>>y; while(!cin) {

6|Page

Coursework 2
cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>y; } arrayP[i].create(x,y); } else { cout<<"Enter coordinates for P"<<i+1<<" (x y):"; cin>>x; while(!cin) { cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>x; } cin>>y; while(!cin) { cout<<"Invalid input! Please re-enter:"; cin.clear( ); cin.ignore(numeric_limits <streamsize>::max( ),'\n'); cin>>y; } arrayP[i].create(x,y); arrayL[i-1].create(arrayP[i-1],arrayP[i]); arrayL[i-1].displayL(i); lengthB+=arrayL[i-1].length(); } } star(); star(); for(int i=3;i<side;i++) { if(validation(arrayP[i],arrayP[i-1],arrayL[i-3])) { cout<<"line "<<i<<" is intersecting with line "<<i-2<<'.'; } } boundry=lengthA+lengthB; for(int i=0;i<side;i++) { if(arrayL[i].slope()==arrayL[i+1].slope()) { cout<<"----------------------"<<endl; cout<<"This is not a polygon!"<<endl;

7|Page

Coursework 2
cout<<"----------------------"<<endl; break; } else if(i==side-1) { cout<<"----------------------"<<endl; cout<<"boundry of the polygon is="<<boundry<<endl; cout<<"----------------------"<<endl; } } cout<<endl; system("pause"); } bool validation(Point p1,Point p2,Line lin) { double value=0.0; value=lin.equation(p1)*lin.equation(p2); if(value<0) { return true; } else { return false; } } void star() { cout<<"*******************************************************************************\n"; } void uLine() { cout<<"_______________________________________________________________________________\n"; }

Point.h file
#ifndef POINT_H #define POINT_H class Point { public: double X; double Y; Point();//default constructor void create(double x,double y); }; #endif

8|Page

Coursework 2

Point.cpp
/*Calculate Polygon boundry *Thushara Kasun Ranawaka *28th of Augest 2011 */ #include<iostream> #include"point.h" using namespace std; Point::Point() { X=0; Y=0; } void Point::create(double x,double y) { X=x; Y=y; }

Line.h file
#ifndef LINE_H #define LINE_H #include"point.h" class Line { Point P1; Point P2; public: Line();//default constructor void create(Point p1,Point p2); double length(); void displayL(int q); double slope(); double equation(Point p1); }; #endif

Line.cpp file
/*Calculate Polygon boundry

9|Page

Coursework 2
*Thushara Kasun Ranawaka *28th of Augest 2011 */ #include<iostream> #include<cmath> #include"line.h" using namespace std; Line::Line() { //empty } void Line::create(Point p1,Point p2) { P1=p1; P2=p2; } double Line::length() { double distance=sqrt(pow((P1.X-P2.X),2.0)+pow((P1.Y-P2.Y),2.0)); return distance; } void Line::displayL(int L) { cout<<endl<<"length of the line "<<L<<" = "<<length()<<endl<<endl; } double Line::slope() { double m=(P1.Y-P2.Y)/(P1.X-P2.X); return m; } double Line::equation(Point p1) { double equa; equa=((P1.Y-P2.Y)*(P1.X-p1.X)/(P1.X-P2.X)*(P1.Y-p1.Y))-1; return equa; }

10 | P a g e

Coursework 2
2.2:Test plan of the program

2.2.1 Test plan 1


Test 1 2 sides 2 3 Data coordinates (0,0) (1,1) (a,a)(b,b) (0,0)(1,1)(2,0) (a,a)(b,b)(c,c) (0,0)(1,1)(9,9) (0,0)(0,0)(0,0) (1,1)(2,3)(4,2)(2,1) (-1,-1)(-2,-3)(-4,-2) (-2,-1) (9,9)(9,9)(9,9)(9,9) (a,b)(d,e)(e,r)(e,w) (-2,2)(0,0)(1,1)(2,1)(2,3) (0,0)(1,1)(2,1)(a,2)(-1,-1) (-4,-2)(-2,2)(2,3)(4,2) (3,-3)(-2-3) (q,q)(w,w)(e,e)(1,1)(2,2)(4,4) (1,1)(2,2)(3,3)(4,4)(5,5)(6,6) (-4,0)(-4,-2)(-2,2) (2,3)(4,2)(2,0)(-1,-3) (0,0)(-1,-1)(-1,0) (9,-3)(-4,-5)(2,5) Expcted output error error 4.82 error error error 7.70 Actual output error error 4.82 error error error 7.70 ok ok ok ok

ok

error error 11.36 error 23.16 error error error error error

error error 11.36 error 23.16 error error error error error

ok ok ok ok ok ok ok ok ok ok
11 | P a g e

6 7 8

7 a @

Coursework 2
2.3:Test results
2.3.1:screen shots

12 | P a g e

Coursework 2

13 | P a g e

Coursework 2

3.0
The problems I had face 1) How to use classes? 2) What is default constructor?

Conclusion

3) What are the maths equations to calculate length? 4) How to whether the lines are intersect or not? How I manage them 1) I again go through the lecture notes. 2) Go through the lecture notes. 3) Asked from my friend Ishara Nadun. 4) Refer the internet.

14 | P a g e

Coursework 2
Attachments

References
Polygon - Wikipedia, the free encyclopedia. 2011. Polygon - Wikipedia, the free encyclopedia. [ONLINE] Available at: http://en.wikipedia.org/wiki/Polygon. [Accessed 28 August 2011]. Definition of Polygon. 2011. Definition of Polygon. [ONLINE] Available at:http://www.mathsisfun.com/definitions/polygon.html. [Accessed 28 August 2011].

cplusplus.com - The C++ Resources Network. 2011. cplusplus.com - The C++ Resources Network. [ONLINE] Available at: http://www.cplusplus.com/. [Accessed 28 August 2011].

The End

15 | P a g e

You might also like