Hi all

I need help with C++, I need to develop a class specification and an irregular polygon application. I am stuck and don't know what to do.I have tried to solve it but it keeps giving me errors.
If anyone is kindly to help me out, I would be very grateful for it :)

these are the requirements:

The point P1 has coordinates {X1,Y1}.
The point P2 has coordinates {X2,Y2}.
The length of the line joining P1 and P2 is given by the equation:
{ (X2 - X1)2 + (Y2 - Y1)2 }1/2

Develop a class specification for the class Point (the data members and the prototypes for the member functions) in the file point.h.
Develop the implementation of the member functions in the file point.cc

Develop a class specification for the class Line (the data members and the prototypes for the member functions) in the file line.h.
Develop the implementation of the member functions in the file line.cc

to determine the length of the boundary of an irregular polygon.
The polygon may have between 3 and 9 sides.
The user should be prompted to enter the number of sides and the coordinates of each vertex point.
The program should then calculate and display the total length of the boundary.
You should write a main function to implement this using your Point and Line classes and test the program thoroughly.


Thanks in advance

No ball no play...

In other words, what have you done so far. Post it...

this is what ive got so far...and not sure if its right and am DEFENITELY sure that something is missing.

/ point.h
class Point
{
public:
Point();
Point(int x, int y);
int X() const;
int Y() const;
void SetX(int);
void SetY(int);

private:
int m_X;
int m_Y;
};

// point.cpp
Point::Point()
{
}

Point::Point(int x, int y) : m_X(x), m_Y(y)
{
}

int Point::X() const {return m_X;}
int Point::Y() const {return m_Y;}
void Point::SetX(int x) {m_X = x;}
void Point::SetY(int y) {m_Y = y;}

// line.h
class Line
{
public:
Line(const Point&, const Point&);
Point StartPoint() const;
Point EndPoint() const;
void SetStartPoint(const Point&);
void SetEndPoint(const Point&);
double Length() const;

private:
Point m_StartPoint;
Point m_EndPoint;
};

Line::Line (
const Point& start,
const Point& end) : m_StartPoint(start), m_EndPoint(end)
{
}

Point Line::StartPoint() const {return m_StartPoint;}
Point Line::EndPoint() const {return m_EndPoint;}
void Line::SetStartPoint(const Point& start) {m_StartPoint = start;}
void Line::SetEndPoint(const Point& end) {m_EndPoint = end;}
double Line::Length() const
{
int diffX = m_EndPoint.X() - m_StartPoint.X();
int diffY = m_EndPoint.Y() - m_StartPoint.Y();
return sqrt(double(diffX*diffX + diffY*diffY));
}

to determine the length of the boundary of an irregular polygon.
The polygon may have between 3 and 9 sides.
The user should be prompted to enter the number of sides and the coordinates of each vertex point.
The program should then calculate and display the total length of the boundary.
You should write a main function to implement this using your Point and Line classes and test the program thoroughly.

It seems as though you've done everything up until this point. For this section you need to:

  • somehow prompt the user for some coordinates
  • use these coordinates to form some lines
  • sum the length of the lines with the Length method you wrote for Line

All of this needs to be done inside a main method in order for it to be run.

Give that a go and repost if you still have problems. :)

It seems as though you've done everything up until this point. For this section you need to:

  • somehow prompt the user for some coordinates
  • use these coordinates to form some lines
  • sum the length of the lines with the Length method you wrote for Line

All of this needs to be done inside a main method in order for it to be run.

Give that a go and repost if you still have problems. :)

oke thanks, an answer which is useful :) ill give it a try although thats the point am actually stuck on!

For testing, I'd wack it all in one file with a main...

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
public:
   Point();
   Point ( int x, int y );
   int X() const;
   int Y() const;
   void SetX ( int );
   void SetY ( int );

private:
   int m_X;
   int m_Y;
};

// point.cpp
Point::Point()
{}

Point::Point ( int x, int y ) : m_X ( x ), m_Y ( y )
{}

int Point::X() const
{
   return m_X;
}
int Point::Y() const
{
   return m_Y;
}
void Point::SetX ( int x )
{
   m_X = x;
}
void Point::SetY ( int y )
{
   m_Y = y;
}

// line.h
class Line
{
public:
   Line ( const Point&, const Point& );
   Point StartPoint() const;
   Point EndPoint() const;
   void SetStartPoint ( const Point& );
   void SetEndPoint ( const Point& );
   double Length() const;

private:
   Point m_StartPoint;
   Point m_EndPoint;
};

Line::Line (
   const Point& start,
   const Point& end ) : m_StartPoint ( start ), m_EndPoint ( end )
{}

Point Line::StartPoint() const
{
   return m_StartPoint;
}
Point Line::EndPoint() const
{
   return m_EndPoint;
}
void Line::SetStartPoint ( const Point& start )
{
   m_StartPoint = start;
}
void Line::SetEndPoint ( const Point& end )
{
   m_EndPoint = end;
}
double Line::Length() const
{
   int diffX = m_EndPoint.X() - m_StartPoint.X();
   int diffY = m_EndPoint.Y() - m_StartPoint.Y();
   return sqrt ( double ( diffX*diffX + diffY*diffY ) );
}


int main()
{
    Point a(4,3);
    //a.SetX(4);
    //a.SetY(3);
    
    Point b(2,1);
    //b.SetX(2);
    //b.SetY(1);
    
    Line myLine(a,b);
    cout << myLine.Length();
    cout << "\n";
    
    system("pause");
}

oke i tested it, gives me these errors

test.cc: In function `int main()':
test.cc:104: implicit declaration of function `int system(...)'

So is it what you might expect? Try it out with some other values.


The next thing you will have to do is use all what you have so far to evaluate the total perimeter of your n sided polygon.

Maybe something like:-

int main()
{
    int myPoints;
    
    cout << "Enter number of points:" <<endl;
    cin >> myPoints;
    
    
    int totalBorder = 0;
    
    for ( int i = 0; i < myPoints; i++ )
    {
        int a,b;
        cout << "Enter x coord:";
        cin >> a;
        
        cout << "Enter y coord:";
        cin >> b;
        
        Point one(a,b);
        Line myLine( previous_point, point_just_entered)
       ...
        
        totalBorder = totalBorder + myLine.Length(); //add up the lengths
    }
    
    cout << "The total border length is " << totalBorder;
    
}

I've left it so it doesn't compile so you can see what you need to do.

This question has already been answered. Start a new discussion instead.