Advertisement
Guest User

Assignment 1 updated

a guest
Nov 29th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.26 KB | None | 0 0
  1.  
  2. /*
  3.  *      Author          : Yossi Yadgar
  4.  *              ID number   : 200973683
  5.  */
  6.  
  7. #include "myPoint.h"
  8. #include "myLine.h"
  9. #include <iostream>
  10.  
  11. int main ()
  12. {
  13.         return 0;
  14. }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------  myLine.cpp  -----------------------------//
  22.  
  23. #include "myLine.h"
  24. using namespace std;
  25.  
  26. void showLine(const Line &l1)
  27. {
  28.         cout << "The line details:" << endl;
  29.         cout << "start point :" << endl;
  30.         showPoint(l1.getStart());
  31.         cout << "end point :" << endl;
  32.         showPoint(l1.getEnd());
  33.         cout << "The point slope: " << l1.getSlope() << endl;
  34.         cout << "The point length " << l1.getLength() << endl;
  35. }
  36.  
  37. Line :: Line (){
  38.         this->start.setPoint(0,0);
  39.         this->end.setPoint(0,0);
  40.         this->length = 0;
  41.         this->slope = 0;
  42. }
  43.  
  44. Line :: Line (const Line &l1)           ///--------------------------- problem ?
  45. {
  46.         *this = l1;
  47. }
  48.  
  49. void Line ::copyLine (const Line &l1){
  50.     this->start = l1.getStart();
  51.     this->end = l1.getEnd();
  52.     this->length = l1.getLength();
  53.     this->slope = l1.getSlope();
  54. }
  55.  
  56.  
  57. Line :: Line(const Point &p1, const Point &p2)
  58. {
  59.         this->start=p1;
  60.         this->end=p2;
  61.         this->slope = calcSlope();
  62.         this->length= calcLength();
  63. }
  64.  
  65.  
  66. void Line :: setStart(const Point &p)
  67. {
  68.         this->start = p;
  69. }
  70.  
  71.  
  72. void Line :: setEnd(const Point &p)
  73. {
  74.         this->end = p;
  75. }
  76.  
  77.  
  78. Point Line :: getStart() const
  79. {
  80.         return this->start;
  81. }
  82.  
  83.  
  84. Point Line :: getEnd() const
  85. {
  86.         return this->end;
  87. }
  88.  
  89.  
  90. double Line :: getLength() const
  91. {
  92.         return this->length;
  93. }
  94.  
  95.  
  96. double Line :: getSlope() const
  97. {
  98.         return this->slope;
  99. }
  100.  
  101.  
  102. double Line :: calcLength() const
  103. {
  104. int x1=this->start.getX();
  105. int x2=this->end.getX();
  106. int y1=this->start.getY();
  107. int y2=this->end.getY();
  108.  
  109.         return  sqrt(((x1-x2)*(x1-x2))+ ((y1-y2)*(y1-y2)));
  110. }
  111.  
  112.  
  113. double Line :: calcSlope() const
  114. {
  115. int x1=this->start.getX();
  116. int x2=this->end.getX();
  117. int y1=this->start.getY();
  118. int y2=this->end.getY();
  119.  
  120.         return ((y2-y1)/(x2-x1));
  121.  
  122. }
  123.  
  124.  
  125. double Line :: LineAddition(const Line &l1, const Line &l2) const
  126. {
  127.         return ( l1.getLength() + l2.getLength() );
  128. }
  129.  
  130.  
  131. double Line :: LineSubtraction(const Line &l1, const Line &l2) const
  132. {
  133.         return ( l1.getLength() - l2.getLength() );
  134. }
  135.  
  136.  
  137. void Line ::LineAdd(const Line &l1)
  138. {
  139.         if ( !(*this ==l1) )
  140.         {
  141.         if  (this->getSlope() == l1.getSlope() )
  142.                 {
  143.                 // if current before l1
  144.                 if (this->getEnd() == l1.getStart() )
  145.                         this->setEnd( l1.getEnd() );
  146.                 // if l1 before current
  147.                 if (this->getStart() == l1.getEnd() )
  148.                         this->setStart(l1.getStart());
  149.                 }
  150.         }
  151.         // else the other line will make from the current line
  152.         // 2 lines, so we don't handle with this case
  153.  
  154. }
  155.  
  156.  
  157. void Line :: LineSub(const Line &l2)
  158. {
  159.         // if this bigger then l2, and they have the same slope
  160.         if (*this == l2)
  161.             *this = Line();     //------------------ legal ??
  162.         if ( (this->getLength() >= l2.getLength()) && (this->getSlope() == l2.getSlope()) )
  163.         {
  164.                 // if they have the same start
  165.                 if (this->getStart() == l2.getStart() )
  166.                 {
  167.                         this->setStart(l2.getEnd());
  168.                         return;
  169.                 }
  170.                 // if they have the same end
  171.                 if (this->getEnd() == l2.getEnd() )
  172. {
  173.                         this->setEnd(l2.getStart());
  174.                         return;
  175.                 }
  176.                 // else the other line will make from the current line
  177.                 // 2 lines, so we don't handle with this case
  178.         }
  179. }
  180.  
  181. const Line & Line :: operator =(const Line &l1){
  182.         this->copyLine(l1);
  183.         return *this;
  184. }
  185.  
  186. bool Line :: operator ==(const Line &l0){
  187.     if ( this->getStart() != l0.getStart() )
  188.         return false;
  189.     if ( this->getEnd() != l0.getEnd() )
  190.         return false;
  191.     if ( this->getLength() != l0.getLength() )
  192.         return false;
  193.     if ( this->getSlope() != l0.getSlope() )
  194.         return false;
  195.     return true;
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. //-----------------------------  myLine.h  -----------------------------//
  204.  
  205. #include <iostream>
  206. #include <math.h>
  207. #include "myPoint.h"
  208.  
  209. #ifndef myLine_h
  210. #define myLine_h
  211.  
  212. class Line
  213. {
  214.  
  215. friend void showLine(const Line &) ;                    // presents line details
  216.  
  217. public:                                                 // class methods:
  218.         Line ();                                        // default constructor
  219.         Line(const Point &, const Point &);             // class constructor
  220.         Line(const Line &);                             // copy constructor
  221.         void setStart(const Point &);                   // set the start point of the line
  222.         void setEnd(const Point &);                     // set the end point of the line
  223.         Point getStart() const;                         // get the start point of the line
  224.         Point getEnd() const;                           // get the end point of the line
  225.         double getLength() const;                       // get the length of the line
  226.         double getSlope() const;                        // get the slope of the line
  227.         double calcLength() const;                      // calculate the length of the line
  228.         double calcSlope() const;                       // calculate the slope of the line
  229.         double LineAddition(const Line &, const Line &) const;    // calculate the length of line1+line2
  230.         double LineSubtraction(const Line &, const Line &) const; // calculate the length of line1-line2
  231.         void LineAdd(const Line &);                     // add the other line to current line (this->line +=new line)
  232.         void LineSub(const Line &);                     // subtract the other line to current line (this->line -=new line)
  233.         const Line & operator=(const Line &);           // assign lines
  234.         bool operator==(const Line &) const;            // compare equal
  235.         bool operator!=(const Line &) const;            // compare not equal
  236.         void copyLine(const Line &);                    // Assistant function
  237.  
  238.  
  239. private:
  240.                                // class members
  241.         Point start;            // start point of the line
  242.         Point end;              // end point of the line
  243.         double length;          // the length of the line
  244.         double slope;           // the slope of the line
  245. };
  246.  
  247. #endif
  248.  
  249. //-----------------------------  myLine.h  -----------------------------//
  250.  
  251.  
  252.  
  253.  
  254.  
  255. //====================myPoint.cpp=======================
  256.  
  257. #include "myPoint.h"
  258. using namespace std;
  259.  
  260. Point::Point(void)
  261. {
  262.         x=y=0;
  263. }
  264.  
  265. Point::Point(int x, int y)
  266. {
  267.        setPoint(x,y);
  268. }
  269.  
  270. Point::Point(const Point &p1){
  271.     *this = p1;
  272. }
  273.  
  274. void Point::setX(int x)
  275. {
  276.         this->x=x;
  277. }
  278.  
  279. void Point::setY(int y)
  280. {
  281.         this->y=y;
  282. }
  283.  
  284. void Point ::setPoint(int x, int y){
  285.     this->setX(x);
  286.     this->setY(y);
  287. }
  288.  
  289. int Point::GetX() const
  290. {
  291.         return this->x;
  292. }
  293.  
  294. int Point::GetY() const
  295. {
  296.         return this->y;
  297. }
  298.  
  299. /*
  300. bool Point ::checkEqualy(Point p1) const
  301. {
  302.     if ( (this->GetX() == p1.GetX()) && (this->GetY() == p1.GetY()) )
  303.         return true;
  304.     return false;
  305. }
  306. */
  307.  
  308. void ShowPoint (Point p1)
  309. {
  310.     cout << "The point coordinates are: " << endl;
  311.     cout << "x= " << p1.GetX() <<" y= "<< p1.GetY() << endl;
  312. }
  313.  
  314. const Point & Point :: operator =(const Point &p1){
  315.     this->setPoint(p1.GetX(), p1.getY());
  316.     return *this;
  317. }
  318.  
  319. bool Point :: operator ==(const Point &p1) const{
  320.     if ( (this->getX() == p1.getX() ) && (this->getY() == p1.getY()) )
  321.         return true;
  322.     return false;
  323. }
  324.  
  325. bool Point ::operator !=(const Point &p1) const {
  326.     return !(*this == p1);
  327. }
  328.  
  329.  
  330.  
  331. //-----------------------------  myPoint.h  -----------------------------//
  332.  
  333. #include <iostream>
  334.  
  335. #ifndef myPoint_h
  336. #define myPoint_h
  337.  
  338. class Point
  339. {
  340.  
  341. friend void showPoint(Point);                           // friend function that prints one point
  342.  
  343. public:                                                 // class methods
  344.         //Point();                                      // default constructor
  345.         Point(int =0,int =0);                           // class constructor
  346.         Point(const Point &);                           // copy constructor
  347.         void setX(int);                                 // set the x value of the point
  348.         void setY(int);                                 // set the y value of the point
  349.         void setPoint(int, int);                        // set one point
  350.         int getX() const;                               // get the x value of the point
  351.         int getY() const;                               // get the y value of the point
  352.         bool checkEqualy(Point) const;     //-- do i need it?    // return answer if 2 points are equal
  353.         const Point &operator=(const Point &);                  // assign points
  354.         bool operator==(const Point &) const;                   // compare equal
  355.         bool operator!=(const Point &) const;                   // compare not equal
  356.  
  357. private:                // class members
  358.         int x;          // x coordinate
  359.         int y;          // y coordinate
  360. };
  361.  
  362. #endif
  363.  
  364. //-----------------------------  myPoint.h  -----------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement