M
moi
hi,
im fairly new to c++ and as part of a module at uni im learning it. i know
the basics i suppose, but as our final hand-in we have to alter code we
wrote for an earlier assignment to use classes as opposed to structures. the
program basically modelled a simple polygon using point, line and polyogon
structures. and had various functions to find the properties of the shape
such as area, perimeter, etc.
the question reads along the lines of:-
rewrite the data structures as classes and assign private access to all data
members with appropriate public access member functions. all previous global
functions must now be redesigned as member functions of the corresponding
classes, thus the following member functions must be supported:
point: distance(), display()
line: length(), display()
polygon: area(), centroid(), perimeter(), display().
i have copied and pasted my code below. obviously im not looking for someone
to do my work for me (man, that would be nice) but i could really use a
nudge in the right direction, and a bit of a foothold to get started from.
thanks,
M.o'N
-----------------------------------
#include<iostream.h> //c++ I/O
#include<conio.h> //getche()
#include<math.h> // sqrt(),...
const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;
//Data Structures:-
//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};
//Straight line segment
struct Line
{
Point p1, p2; //two end points of a line
};
//Polygon with Point vertices and Line straight-line edges.
struct Polygon
{
int num_vertices, num_edges; //number of vertices and edges.
Point verts[MAX_NO_VERTICES]; //polygon vertices
Line edges[MAX_NO_EDGES]; //polygon edges
};
//3D vector
struct Vector3D
{
double a, b, c; //vector components
};
//adds two Vector3D objects
Vector3D Add(const Vector3D& u, const Vector3D& v)
{
Vector3D add;
add.a = u.a + v.a;
add.b = u.b + v.b;
add.c = u.c + v.c;
return add;
}
//returns the cross product of two vectors (right-hand screw rule)
// UxV = [(UyVz-UzVy), (UzVx-UxVz), (UxVy-UyVx)]
Vector3D CrossProduct(const Vector3D& u, const Vector3D& v)
{
Vector3D cross;
cross.a = u.b*v.c - u.c*v.b;
cross.b = u.c*v.a - u.a*v.c;
cross.c = u.a*v.b - u.b*v.a;
return cross;
}
//returns the norm or magnitued (i.e. lenght) of a vector
double Norm (const Vector3D& v)
{return sqrt(v.a*v.a + v.b*v.b + v.c*v.c);}
//Point functions:
//returns the distance between 2 points
double Distance(const Point& p1, const Point& p2)
{
double x = p2.x-p1.x;
double y = p2.y-p1.y;
double z = p2.z-p1.z;
return sqrt(x*x + y*y + z*z);
}
//retunrs v=p-q, ie a Vector3D object
Vector3D Subtract(const Point& p, const Point& q)
{
Vector3D v;
v.a = p.x - q.x;
v.b = p.y - q.y;
v.c = p.z - q.z;
return v;
}
//o/p a point
void Display(const Point& p)
{ cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" ; }
//Line functions:
//returns the length of a Line
double Length(const Line& l)
{ return Distance(l.p1, l.p2);}
//o/p a line
void Display(const Line& l)
{cout << "["; Display(l.p1) ; cout << ", " ; Display(l.p2) ; cout << "]" ;}
//polygon functions
//returns the perimeter of a polygon
double Perimeter(const Polygon& p)
{
double perimeter(0.0);
for (int i = 0; i<p.num_edges; i++)
perimeter += Length(p.edges);
return perimeter;
}
//returns the centroid of a polygon (uses averaging of vertices)
Point Centroid(const Polygon& p)
{
Point centroid = {0.0,0.0,0.0};
for (int i=0; i<p.num_vertices; i++)
{
centroid.x += p.verts.x;
centroid.y += p.verts.y;
centroid.z += p.verts.z;
}
centroid.x /= p.num_vertices;
centroid.y /= p.num_vertices;
centroid.z /= p.num_vertices;
return centroid;
}
//returns the area of a polygon (uses the cross-product rule)
double Area(const Polygon& p)
{
Vector3D vi1v0, vi2v0, sum_vector;
for (int i=0; i<p.num_vertices-2; i++)
{
//use vertex 0 as 'local' origin
vi1v0 = Subtract(p.verts[i+1], p.verts[0]);
vi2v0 = Subtract(p.verts[i+2], p.verts[0]);
sum_vector = Add(sum_vector, CrossProduct(vi1v0, vi2v0));
}
return Norm(sum_vector)/2.0;
}
//o/p a polygon
void Display(const Polygon& p)
{
cout << "polygon vertices: ";
for (int i=0; i<p.num_vertices; i++)
{
Display(p.verts);
if (i != p.num_vertices-1)
cout << ",";
}
cout << endl;
cout << "polygon edges: ";
for (int i=0; i<p.num_edges; i++)
{
Display(p.edges);
if (i != p.num_edges-1)
cout << ",";
}
}
void main()
{
//define a polygon [triangle in this instance]
Polygon poly;
//set poly's number of vertices and edges
poly.num_vertices = 3;
poly.num_edges = 3;
//vertices
poly.verts[0].x = 1.0 ; poly.verts[0].y = 1.0 ; poly.verts[0].z = 0.0;
poly.verts[1].x = 4.0 ; poly.verts[1].y = 1.0 ; poly.verts[1].z = 0.0;
poly.verts[2].x = 2.0 ; poly.verts[2].y = 3.0 ; poly.verts[2].z = 0.0;
//edges
poly.edges[0].p1 = poly.verts[0] ; poly.edges[0].p2 = poly.verts[1];
poly.edges[1].p1 = poly.verts[1] ; poly.edges[1].p2 = poly.verts[2];
poly.edges[2].p1 = poly.verts[2] ; poly.edges[2].p2 = poly.verts[0];
//properties
double perimeter = Perimeter(poly);
Point centroid = Centroid(poly);
double area = Area(poly);
//o/p
cout << "polygon: " <<endl;
Display(poly) ; cout << endl;
cout << "perimeter of polygon: " << perimeter << endl;
cout << "centroid of polygon: ";
Display(centroid) ; cout << endl;
cout <<"area of polygon: " << area << endl;
cout << "press any key to continue...";
getche();
}
im fairly new to c++ and as part of a module at uni im learning it. i know
the basics i suppose, but as our final hand-in we have to alter code we
wrote for an earlier assignment to use classes as opposed to structures. the
program basically modelled a simple polygon using point, line and polyogon
structures. and had various functions to find the properties of the shape
such as area, perimeter, etc.
the question reads along the lines of:-
rewrite the data structures as classes and assign private access to all data
members with appropriate public access member functions. all previous global
functions must now be redesigned as member functions of the corresponding
classes, thus the following member functions must be supported:
point: distance(), display()
line: length(), display()
polygon: area(), centroid(), perimeter(), display().
i have copied and pasted my code below. obviously im not looking for someone
to do my work for me (man, that would be nice) but i could really use a
nudge in the right direction, and a bit of a foothold to get started from.
thanks,
M.o'N
-----------------------------------
#include<iostream.h> //c++ I/O
#include<conio.h> //getche()
#include<math.h> // sqrt(),...
const int MAX_NO_VERTICES = 25; //max no of polygon vertices and edges.
const int MAX_NO_EDGES = 25;
//Data Structures:-
//Cartesian point
struct Point
{
double x, y, z; //triple coordinates of a point.
};
//Straight line segment
struct Line
{
Point p1, p2; //two end points of a line
};
//Polygon with Point vertices and Line straight-line edges.
struct Polygon
{
int num_vertices, num_edges; //number of vertices and edges.
Point verts[MAX_NO_VERTICES]; //polygon vertices
Line edges[MAX_NO_EDGES]; //polygon edges
};
//3D vector
struct Vector3D
{
double a, b, c; //vector components
};
//adds two Vector3D objects
Vector3D Add(const Vector3D& u, const Vector3D& v)
{
Vector3D add;
add.a = u.a + v.a;
add.b = u.b + v.b;
add.c = u.c + v.c;
return add;
}
//returns the cross product of two vectors (right-hand screw rule)
// UxV = [(UyVz-UzVy), (UzVx-UxVz), (UxVy-UyVx)]
Vector3D CrossProduct(const Vector3D& u, const Vector3D& v)
{
Vector3D cross;
cross.a = u.b*v.c - u.c*v.b;
cross.b = u.c*v.a - u.a*v.c;
cross.c = u.a*v.b - u.b*v.a;
return cross;
}
//returns the norm or magnitued (i.e. lenght) of a vector
double Norm (const Vector3D& v)
{return sqrt(v.a*v.a + v.b*v.b + v.c*v.c);}
//Point functions:
//returns the distance between 2 points
double Distance(const Point& p1, const Point& p2)
{
double x = p2.x-p1.x;
double y = p2.y-p1.y;
double z = p2.z-p1.z;
return sqrt(x*x + y*y + z*z);
}
//retunrs v=p-q, ie a Vector3D object
Vector3D Subtract(const Point& p, const Point& q)
{
Vector3D v;
v.a = p.x - q.x;
v.b = p.y - q.y;
v.c = p.z - q.z;
return v;
}
//o/p a point
void Display(const Point& p)
{ cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" ; }
//Line functions:
//returns the length of a Line
double Length(const Line& l)
{ return Distance(l.p1, l.p2);}
//o/p a line
void Display(const Line& l)
{cout << "["; Display(l.p1) ; cout << ", " ; Display(l.p2) ; cout << "]" ;}
//polygon functions
//returns the perimeter of a polygon
double Perimeter(const Polygon& p)
{
double perimeter(0.0);
for (int i = 0; i<p.num_edges; i++)
perimeter += Length(p.edges);
return perimeter;
}
//returns the centroid of a polygon (uses averaging of vertices)
Point Centroid(const Polygon& p)
{
Point centroid = {0.0,0.0,0.0};
for (int i=0; i<p.num_vertices; i++)
{
centroid.x += p.verts.x;
centroid.y += p.verts.y;
centroid.z += p.verts.z;
}
centroid.x /= p.num_vertices;
centroid.y /= p.num_vertices;
centroid.z /= p.num_vertices;
return centroid;
}
//returns the area of a polygon (uses the cross-product rule)
double Area(const Polygon& p)
{
Vector3D vi1v0, vi2v0, sum_vector;
for (int i=0; i<p.num_vertices-2; i++)
{
//use vertex 0 as 'local' origin
vi1v0 = Subtract(p.verts[i+1], p.verts[0]);
vi2v0 = Subtract(p.verts[i+2], p.verts[0]);
sum_vector = Add(sum_vector, CrossProduct(vi1v0, vi2v0));
}
return Norm(sum_vector)/2.0;
}
//o/p a polygon
void Display(const Polygon& p)
{
cout << "polygon vertices: ";
for (int i=0; i<p.num_vertices; i++)
{
Display(p.verts);
if (i != p.num_vertices-1)
cout << ",";
}
cout << endl;
cout << "polygon edges: ";
for (int i=0; i<p.num_edges; i++)
{
Display(p.edges);
if (i != p.num_edges-1)
cout << ",";
}
}
void main()
{
//define a polygon [triangle in this instance]
Polygon poly;
//set poly's number of vertices and edges
poly.num_vertices = 3;
poly.num_edges = 3;
//vertices
poly.verts[0].x = 1.0 ; poly.verts[0].y = 1.0 ; poly.verts[0].z = 0.0;
poly.verts[1].x = 4.0 ; poly.verts[1].y = 1.0 ; poly.verts[1].z = 0.0;
poly.verts[2].x = 2.0 ; poly.verts[2].y = 3.0 ; poly.verts[2].z = 0.0;
//edges
poly.edges[0].p1 = poly.verts[0] ; poly.edges[0].p2 = poly.verts[1];
poly.edges[1].p1 = poly.verts[1] ; poly.edges[1].p2 = poly.verts[2];
poly.edges[2].p1 = poly.verts[2] ; poly.edges[2].p2 = poly.verts[0];
//properties
double perimeter = Perimeter(poly);
Point centroid = Centroid(poly);
double area = Area(poly);
//o/p
cout << "polygon: " <<endl;
Display(poly) ; cout << endl;
cout << "perimeter of polygon: " << perimeter << endl;
cout << "centroid of polygon: ";
Display(centroid) ; cout << endl;
cout <<"area of polygon: " << area << endl;
cout << "press any key to continue...";
getche();
}