CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2006
    Posts
    26

    bit confussed about a couple of classes

    Hi.

    Ive been working on this code for an assinment, and overall feel like i am doing ok. There are 2 Classes (Polygon and Point), Point hold x and y coords, and Polygon stores the coords for a polygon in Point (but not inherited) but theres a method i must put in polygon and another line of code that is confussing me.

    "Polygon rhombus(4, (Point(0,0), Point(1,0), Point(2,1), Point(1,1)));"
    this should inisilise a rhombus.

    (as i have added extra varibles to my constructor i think it should look like
    Polygon rhombus(4, cx, cy, diam, rad, (Point(0,0), Point(1,0), Point(2,1), Point(1,1)));

    I get the rhombus, and i would have made an array (Point rhombus[4]) filled in the data and then pointed Polygon to it. I dont think this is what the teacher wants (but shes not made it very clear).

    The other point that is confusing me is:

    "point& vertex(unsigned int n); // 0 <= n < number of sides.
    vertex(n) should return a reference to the nth point (numbered from zero)"

    I sort of get this, but im a little unsure how to impiment it. It is later going to used by an inherited square class. Maybe i have been doing it for to long but i think i am confusing my self on a simple task.

    Any help or nuges in the right direction would be great. Below is the code i have done so far.

    Thanks in advanced.

    Point.h
    Code:
     #ifndef Point_H
    #define Point_H
    #include <iostream>
    
    using std::istream;
    using std::ostream;
    
    class Point
    {
    	double x;
    	double y;
    	friend istream &operator>>( istream&,Point&); //used for the in put of coords
    	friend ostream &operator<<( ostream&,const Point&); // outputs in a certain way
    	public:
    		Point (void);
    		Point(double dx, double dy); // 2nd constructor
    		void set_x(double d);
    		void set_y(double d);
    		double const get_x();
    		double const get_y();
    		double distance_from_orign(); // display the distance from 0,0
    		double distance_from(const Point &other); // displays the distane between to points
    		~Point(void); // deconstuctor
    };
    
    #endif
    Point Methods
    Code:
     #include <cmath>
    #include "Point.h"
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::istream;
    using std::ostream; 
    using std::endl;
    
    	istream &operator>>(istream &input, Point &num)
    		{
    			input >> num.x;
    			input.ignore(2);
    			input >> num.y;
    			return input;
    		}
    
    	ostream &operator<<( ostream &output, const Point &cord)
    		{
    			output << "(" << cord.x << "," << cord.y << ")";
    			return output;
    		}
    
    	Point::Point(void)
    		{
    			x = 0;
    			y = 0;
    		}
    
    	Point::Point(double dx, double dy)
    		{
    			x = dx;
    			y = dy;
    		}
    
    	void Point::set_x(double d)
    		{
    			if  (d <0)
    			{
    				x = 0;
    			}
    			else
    			{
    				x = d;
    			}
    		}
    
    	void Point::set_y(double d)
    		{
    			if  (d <0)
    			{
    				y = 0;
    			}
    			else
    			{
    				y = d;
    			}
    		}
    
    	double const Point::get_x()
    		{
    			return (x);
    		}
    
    	double const Point::get_y() 
    		{
    			return (y);
    		}
    
    	double Point::distance_from_orign()
    		{
    			return (sqrt((x * x)+(y * y)));
    		}
    
    		
    	double Point::distance_from(const Point &other)
    		{
    			double w;
    			double z;
    			w = other.x - x;
    			z = other.y - y;
    			return (sqrt((z * z)+(w * w)));
    		}
    
    	Point::~Point(void)
    		{
    			x = 0;
    			y = 0;
    		}
    Polygon.h
    Code:
     #ifndef POLYGON_H
    #define POLYGON_H
    
    #include "Point.h"
    using std::istream;
    using std::ostream; 
    
    class Polygon
    {
    	Point *coord; // Points to Coordinates stored in Point
    	int nPoints; // number of points in the polygon
    	double doDiameter; // auctally the radius, need to change the varible name
    	double doCenter_x; // center of polygon x and y
    	double doCenter_y;
    	double rad; // radians used for working out the coord of each point
    	
    	public:
    		Polygon (void); //default constructor
    		Polygon(int sides, double cx, double cy, double diam, double radian, Point *other ); // 2nd constructor
    		void set_rad();
    		void set_point(int poi) {nPoints = poi; set_rad();}
    		void set_diameter(double diam) {doDiameter = diam;}
    		void set_center_x(double cx) {doCenter_x = cx;}
    		void set_center_y(double cy) {doCenter_y = cy;} // Polygon, diameter, x, and y
    		int const get_nPoints() {return (nPoints);}
    		double const get_doDiameter() {return (doDiameter);}
    		double const get_doCenter_x() {return (doCenter_x);}
    		double const get_doCenter_y() {return (doCenter_y);}
    		double const get_rad() {return (rad);}
    		double perimeter();
    		//Point& vertex(int n);
    		friend ostream& operator<<(ostream&,const Polygon&); // out puts coords in a certian way
    		// http://www.codeguru.com/forum/showthread.php?t=405727 You can pass non-const arguments to functions that expect const arguments, but not vice versa
    		friend istream &operator>>( istream&, Polygon&); // not currently used
    		~Polygon(void); // decostructor
    };
    
    #endif
    Polygon Methods
    Code:
     #include <iostream>
    #include <cmath>
    #include "Polygon.h"
    #include "Point.h"
    
    using std::cout;
    using std::cin;
    using std::istream;
    using std::ostream; 
    using std::endl;
    
    	Polygon::Polygon(void)
    		{
    			nPoints = 0;
    			coord = NULL;
    			doDiameter = 0;
    			doCenter_x = 0;
    			doCenter_y = 0;
    			rad = 0;
    		}
    	
    	Polygon::Polygon(int sides, double cx, double cy, double diam, double radian, Point *other)
    			{
    			nPoints = sides;
    			coord = other;
    			doDiameter = diam;
    			doCenter_x = cx;
    			doCenter_y = cy;
    			rad = radian;
    			}
    
    	void Polygon::set_rad()
    		{
    			rad = (180.0/(360/nPoints));
    		}
    
    	double Polygon::perimeter()	
    		{
    		double perm = 0;
    
    		for (int i = 0; i < nPoints; i ++)
    		{
    			if (i  != (nPoints - 1))
    				{
    				perm += coord[i].distance_from(coord[i+1]);	
    				}
    			else
    				{
    					perm += coord[0].distance_from(coord[nPoints - 1]);
    				}
    		}
    		return (perm);			
    		}
    
    	istream &operator>>(istream &input, Polygon &cor)
    		{
    			input >> cor.doCenter_x;
    			input.ignore(2);
    			input >> cor.doCenter_y;
    			return input;
    		}
    
    	ostream &operator<<(ostream &output,const Polygon &cord)
    		{
    			output << "[";
    
    				for (int i = 0; i < cord.nPoints; i++)
    				{
    					output << "(" << cord.coord[i].get_x() << ", " << cord.coord[i].get_y() << ")";
    					if (i != cord.nPoints - 1)
    						output << ",";
    				}
    
    				output << "]" << endl << endl;
    				return output;
    		}
    
    	Polygon::~Polygon(void)
    		{
    
    			doDiameter = 0;
    			doCenter_x = 0;
    			doCenter_y = 0;
    			rad = 0;
    			nPoints = 0;
    		}

    Main code
    Code:
    #include <cmath>
    #include "Point.h"
    #include "Polygon.h"
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::istream;
    using std::ostream; 
    using std::endl;
    
    void main()
    {
    	int poi = 0;
    	double diam = 0;
    	double cx = 0;
    	double cy = 0;
    	double rad = 0;
    	
    //******************************************************************************************
    // asking user about the polygon
    
    	cout << "\nPlease enter amount of points: ";
    	cin >> poi;
    	cout << "where would you like the center (x, y): ";
    	cin >> cx >> cy;
    	cout <<  "\nwhat is the diameter: ";
    	cin >> diam;
    	diam = diam/2;
    
    	rad = (180.0/(360/poi));
    //******************************************************************************************
    // array of cord to class point
    				
    			Point par[8]; 
    
    			for (int i=0; i<8; i++)
    		{
    			par[i].set_x(cx + (diam * (sin(i * 3.1415927/rad))));//3.14 = PI
    			par[i].set_y(cy + (diam * (cos(i * 3.1415927/rad))));//3.14 = PI
    		
    		}
    
    //********************************************************************************************
    	
    	Polygon poly(8, cx, cy, diam, rad, par); // new object polygon using a pointer to Point par
    	
    	cout << "nPoint =   " << poly.get_nPoints() <<endl; // display results to check if correct
    	cout << "Diameter = " << poly.get_doDiameter() <<endl;
    	cout << "Diameter = " << poly.get_doCenter_x() <<endl;
    	cout << "Diameter = " << poly.get_doCenter_y() <<endl << endl;
    	cout << "rad =      " << poly.get_rad() << endl;
    	
    	//	Polygon rhombus(4,(Point (0,0), Point (1,0), Point 3(2,1), Point 4(1,1)));
    	
    	cout << poly; // outputs using overload<<
    
    	cout << "distan = " << poly.perimeter(); // displays perimeter of poly
    
    //*********************************************************************************************
    // Point class codeing
    
    	Point first_dot(100, 50); // inisilising different Points checking the constructors work
    	Point next_dot;
    	const Point screen_limit(640,480); 
    	const Point origin;
    	Point off(3.2, -1.8);
    	Point hexagonaaa[6];
    
    	cout << endl << "First_dot =    "	<< first_dot; // display results
    	cout << endl << "next_dot =     "	<< next_dot;
    	cout << endl << "screen_limit = "	<< screen_limit;
    	cout << endl << "point origin = "	<< origin;
    	cout << endl << "off =          "	<< off;
    	cout << endl << "Hexagon =      [";
    	
    	for(int a = 0; a < 6; a++)
    		{
    		cout << hexagonaaa[a] ;
    		}
    	
    	cout << "]"<< endl <<endl;
     	
    	cout << "\nplease input coordanites for first Point (x y): ";
    	cin >> first_dot;
    
    	cout << "\nplease input coordanites for second Point (x y): ";
    	cin >> next_dot;
    
    	cout << "Coordanate 1: "<< first_dot << endl;
    	cout << "Coordanate 2: "<< next_dot << endl;
    
    	cout << endl << "Distance from orign is: " << first_dot.distance_from_orign() << endl << endl; // displays distance from origin
    
    	cout << "Distance from Point to Point is: " << first_dot.distance_from(next_dot) << endl << endl; // displays distance between to points
    
      }
    Thanks for looking.

    Helen

  2. #2
    Join Date
    Apr 2006
    Posts
    26

    Re: bit confussed about a couple of classes

    Well im not sure if i have figured out the rhombus, but i have tryed

    Code:
    	Point rhom[4] = {Point(0,0),Point(1,0),Point(2,1),Point(1,1)};
    	Polygon rhombus(4,cx, cy, diam, rad, rhom);
    This seems to work, but im still unsure if that is what was intended.

    What does anyone think?

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured