Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------- myPoint.h -----------------------------//
- #include <iostream>
- #ifndef myPoint_h
- #define myPoint_h
- class Point
- {
- friend void showPoint(Point); // friend function that prints one point
- public: // class methods
- //Point(); // default constructor
- Point(int =0,int =0); // class constructor
- Point(const Point &); // copy constructor
- void setX(int); // set the x value of the point
- void setY(int); // set the y value of the point
- int getX() const; // get the x value of the point
- int getY() const; // get the y value of the point
- bool checkEqualy(Point) const; //-- do i need it? // return answer if 2 points are equal
- const Point &operator=(const Point &); // assign points
- bool operator==(const Point &) const; // compare equal
- bool operator!=(const Point &) const; // compare not equal
- private: // class members
- int x; // x coordinate
- int y; // y coordinate
- };
- #endif
- //----------------------------- myPoint.h -----------------------------//
- //----------------------------- myLine.h -----------------------------//
- #include <iostream>
- #include <math.h>
- #include "myPoint.h"
- #ifndef myLine_h
- #define myLine_h
- class Line
- {
- friend void ShowLine(Line) ; // presents line details
- public: // class methods:
- Line (); // default constructor
- Line(Point, Point); // class constructor
- Line(const Line &); // copy constructor
- void SetStart(Point); // set the start point of the line
- void SetEnd(Point); // set the end point of the line
- Point GetStart() const; // get the start point of the line
- Point GetEnd() const; // get the end point of the line
- double GetLength() const; // get the length of the line
- double GetSlope() const; // get the slope of the line
- double CalcLength() const; // calculate the length of the line
- double CalcSlope() const; // calculate the slope of the line
- double LineAddition(Line,Line) const; // calculate the length of line1+line2
- double LineSubtraction(Line,Line) const; // calculate the length of line1-line2
- void LineAdd(Line); // add the other line to current line (this->line +=new line)
- void LineSub(Line); // subtract the other line to current line (this->line -=new line)
- const Line &operator=(const Line &); // assign lines
- private:
- // class members
- Point Start; // start point of the line
- Point End; // end point of the line
- double length; // the length of the line
- double Slope; // the slope of the line
- };
- #endif
- //----------------------------- myLine.h -----------------------------//
- /*
- * Author : Yossi Yadgar
- * ID number : 200973683
- */
- #include "myPoint.h"
- #include "myLine.h"
- #include <iostream>
- int main ()
- {
- return 0;
- }
- //----------------------------- myLine.cpp -----------------------------//
- #include "myLine.h"
- using namespace std;
- void ShowLine(Line l1)
- {
- cout << "The line details:" << endl;
- cout << "start point :" << endl;
- showPoint(l1.GetStart());
- cout << "end point :" << endl;
- showPoint(l1.GetEnd());
- cout << "The point slope: " << l1.GetSlope() << endl;
- cout << "The point length " << l1.GetLength() << endl;
- }
- Line :: Line (){
- this->Start.setX(0);
- this->Start.setY(0);
- this->End.setX(0);
- this->End.setY(0);
- this->length = 0;
- this->Slope = 0;
- }
- Line :: Line (const Line &l1)
- {
- this->Start = l1.Start;
- this->End = l1.End;
- this->length = l1.length;
- this->Slope = l1.Slope;
- }
- Line :: Line(Point p1, Point p2)
- {
- this->Start=p1;
- this->End=p2;
- this->Slope = CalcSlope();
- this->length= CalcLength();
- }
- void Line :: SetStart(Point p)
- {
- this->Start.setX(p.getX());
- this->Start.setY(p.getY());
- }
- void Line :: SetEnd(Point p)
- {
- this->End.setX(p.getX());
- this->End.setY(p.getY());
- }
- Point Line :: GetStart() const
- {
- return this->Start;
- }
- Point Line :: GetEnd() const
- {
- return this->End;
- }
- double Line :: GetLength() const
- {
- return this->length;
- }
- double Line :: GetSlope() const
- {
- return this->Slope;
- }
- double Line :: CalcLength() const
- {
- int x1=this->Start.getX();
- int x2=this->End.getX();
- int y1=this->Start.getY();
- int y2=this->End.getY();
- return sqrt(((x1-x2)*(x1-x2))+ ((y1-y2)*(y1-y2)));
- }
- double Line :: CalcSlope() const
- {
- int x1=this->Start.getX();
- int x2=this->End.getX();
- int y1=this->Start.getY();
- int y2=this->End.getY();
- return ((y2-y1)/(x2-x1));
- }
- double Line :: LineAddition(Line l1,Line l2) const
- {
- return ( l1.GetLength() + l2.GetLength() );
- }
- double Line :: LineSubtraction(Line l1,Line l2) const
- {
- return ( l1.GetLength() - l2.GetLength() );
- }
- void Line ::LineAdd(Line l1)
- {
- if (this->Slope == l1.GetSlope() )
- {
- // if current before l1
- if (this->GetEnd().checkEqualy(l1.GetStart()))
- this->SetEnd(l1.GetEnd());
- // if l1 before current
- if (this->GetStart().checkEqualy(l1.GetEnd()))
- this->SetStart(l1.GetStart());
- }
- // else the other line will make from the current line
- // 2 lines, so we don't handle with this case
- }
- void Line :: LineSub(Line l2)
- {
- if ( (this->GetLength() >= l2.GetLength()) && (this->GetSlope() == l2.GetSlope()) )
- {
- // if they have the same start
- if (this->GetStart().checkEqualy(l2.GetStart()))
- {
- this->SetStart(l2.GetEnd());
- return;
- }
- // if they have the same end
- if (this->GetEnd().checkEqualy(l2.GetEnd()))
- {
- this->SetEnd(l2.GetStart());
- return;
- }
- // else the other line will make from the current line
- // 2 lines, so we don't handle with this case
- }
- }
- const Line & Line :: operator=(const Line &l1){
- this->Start = l1.Start;
- this->End = l1.End;
- this->length = l1.length;
- this->Slope = l1.Slope;
- return *this;
- }
- //----------------------------- myPoint.cpp -----------------------------//
- #include "myPoint.h"
- using namespace std;
- /*
- Point::Point(void)
- {
- x=y=0;
- }
- */
- Point::Point(int x, int y)
- {
- setX(x);
- setY(y);
- }
- Point :: Point(const Point &p)
- {
- this->setX(p.getX());
- this->setY(p.getY());
- }
- void Point::setX(int x)
- {
- this->x=x;
- }
- void Point::setY(int y)
- {
- this->y=y;
- }
- int Point::getX() const
- {
- return this->x;
- }
- int Point::getY() const
- {
- return this->y;
- }
- bool Point ::checkEqualy(Point p1) const
- {
- if ( (this->getX() == p1.getX()) && (this->getY() == p1.getY()) )
- return true;
- return false;
- }
- void ShowPoint (Point p1)
- {
- cout << "The point coordinates are: " << endl;
- cout << "x= " << p1.getX() <<" y= "<< p1.getY() << endl;
- }
- const Point & Point ::operator=(const Point &p){
- if (&p !=this)
- {
- this->setX(p.getX());
- this->setY(p.getY());
- }
- return *this;
- }
- bool Point :: operator==(const Point &p) const {
- if ( (this->getX() == p.getX()) && (this->getY() == p.getY()) )
- return true;
- return false;
- }
- bool Point :: operator!=(const Point &p) const {
- return !(*this==p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement