Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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(const 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.setPoint(0,0);
- this->end.setPoint(0,0);
- this->length = 0;
- this->slope = 0;
- }
- Line :: Line (const Line &l1) ///--------------------------- problem ?
- {
- *this = l1;
- }
- void Line ::copyLine (const Line &l1){
- this->start = l1.getStart();
- this->end = l1.getEnd();
- this->length = l1.getLength();
- this->slope = l1.getSlope();
- }
- Line :: Line(const Point &p1, const Point &p2)
- {
- this->start=p1;
- this->end=p2;
- this->slope = calcSlope();
- this->length= calcLength();
- }
- void Line :: setStart(const Point &p)
- {
- this->start = p;
- }
- void Line :: setEnd(const Point &p)
- {
- this->end = p;
- }
- 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(const Line &l1, const Line &l2) const
- {
- return ( l1.getLength() + l2.getLength() );
- }
- double Line :: LineSubtraction(const Line &l1, const Line &l2) const
- {
- return ( l1.getLength() - l2.getLength() );
- }
- void Line ::LineAdd(const Line &l1)
- {
- if ( !(*this ==l1) )
- {
- if (this->getSlope() == l1.getSlope() )
- {
- // if current before l1
- if (this->getEnd() == l1.getStart() )
- this->setEnd( l1.getEnd() );
- // if l1 before current
- if (this->getStart() == 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(const Line &l2)
- {
- // if this bigger then l2, and they have the same slope
- if (*this == l2)
- *this = Line(); //------------------ legal ??
- if ( (this->getLength() >= l2.getLength()) && (this->getSlope() == l2.getSlope()) )
- {
- // if they have the same start
- if (this->getStart() == l2.getStart() )
- {
- this->setStart(l2.getEnd());
- return;
- }
- // if they have the same end
- if (this->getEnd() == 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->copyLine(l1);
- return *this;
- }
- bool Line :: operator ==(const Line &l0){
- if ( this->getStart() != l0.getStart() )
- return false;
- if ( this->getEnd() != l0.getEnd() )
- return false;
- if ( this->getLength() != l0.getLength() )
- return false;
- if ( this->getSlope() != l0.getSlope() )
- return false;
- return true;
- }
- //----------------------------- myLine.h -----------------------------//
- #include <iostream>
- #include <math.h>
- #include "myPoint.h"
- #ifndef myLine_h
- #define myLine_h
- class Line
- {
- friend void showLine(const Line &) ; // presents line details
- public: // class methods:
- Line (); // default constructor
- Line(const Point &, const Point &); // class constructor
- Line(const Line &); // copy constructor
- void setStart(const Point &); // set the start point of the line
- void setEnd(const 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(const Line &, const Line &) const; // calculate the length of line1+line2
- double LineSubtraction(const Line &, const Line &) const; // calculate the length of line1-line2
- void LineAdd(const Line &); // add the other line to current line (this->line +=new line)
- void LineSub(const Line &); // subtract the other line to current line (this->line -=new line)
- const Line & operator=(const Line &); // assign lines
- bool operator==(const Line &) const; // compare equal
- bool operator!=(const Line &) const; // compare not equal
- void copyLine(const Line &); // Assistant function
- 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 -----------------------------//
- //====================myPoint.cpp=======================
- #include "myPoint.h"
- using namespace std;
- Point::Point(void)
- {
- x=y=0;
- }
- Point::Point(int x, int y)
- {
- setPoint(x,y);
- }
- Point::Point(const Point &p1){
- *this = p1;
- }
- void Point::setX(int x)
- {
- this->x=x;
- }
- void Point::setY(int y)
- {
- this->y=y;
- }
- void Point ::setPoint(int x, int y){
- this->setX(x);
- this->setY(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 &p1){
- this->setPoint(p1.GetX(), p1.getY());
- return *this;
- }
- bool Point :: operator ==(const Point &p1) const{
- if ( (this->getX() == p1.getX() ) && (this->getY() == p1.getY()) )
- return true;
- return false;
- }
- bool Point ::operator !=(const Point &p1) const {
- return !(*this == p1);
- }
- //----------------------------- 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
- void setPoint(int, int); // set one 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 -----------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement