ForeverDev

Line / Segment classes

Oct 21st, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. class Grid {
  7.     public:
  8.         bool parallel(Grid& line) {
  9.             return line.slope == slope;
  10.         }
  11.         float slope;
  12. };
  13.  
  14. class Segment: public Grid {
  15.     public:
  16.         Segment(float _x1 = 0, float _y1 = 0, float _x2 = 0, float _y2 = 0) {
  17.             x1 = _x1;
  18.             x2 = _x2;
  19.             y1 = _y1;
  20.             y2 = _y2;
  21.             slope = (_y2 - _y1) / (_x2 - _x1);
  22.             length = sqrt(pow(_x2 - _x1, 2) + pow(_y2 - _y1, 2));
  23.         }
  24.         float length, x1, x2, y1, y2;
  25. };
  26.  
  27. class Line: public Grid {
  28.     public:
  29.         Line(float _slope) {
  30.             slope = _slope;
  31.         }
  32. };
  33.  
  34. int main() {
  35.     Line l_one(5);
  36.     Line l_two(5);
  37.     cout << l_one.parallel(l_two) << endl;  /* true, they have the same slope */
  38.    
  39.     Segment s_one(1, 2, 50, 100);
  40.     Segment s_two(-50, -100, 0, 0);
  41.     cout << s_one.parallel(s_two) << endl;  /* true, they also have the same slope */
  42. }
Advertisement
Add Comment
Please, Sign In to add comment