Advertisement
Lawnknome

geom.cpp

Nov 30th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class Point
  7. {
  8.  private:
  9.  
  10.     double x;               //x coordinate
  11.     double y;               //y coordinate
  12.     double c;               //distance to
  13.  
  14.  public:
  15.  
  16.     Point()
  17.     {
  18.         x = 0;
  19.         y = 0;
  20.     }
  21.  
  22.     Point(double xCoord, double yCoord)
  23.     {
  24.         x = xCoord;
  25.         y = yCoord;
  26.     }
  27.  
  28.     double getX() const
  29.     {
  30.         return x;
  31.     }
  32.  
  33.     double getY() const
  34.     {
  35.         return y;
  36.     }
  37.  
  38.     //a^2 + b^2 = c^2 - find the hypotenuse
  39.     double distanceTo(const Point& pointTo)
  40.     {
  41.         c = sqrt(pow(abs((x - pointTo.getX())), 2) + pow(abs((y - pointTo.getY())), 2));
  42.         return c;
  43.     }
  44.  
  45. };
  46.  
  47. class LineSegment
  48. {
  49.  private:
  50.  
  51.     Point start;
  52.     Point finish;
  53.     double len;
  54.     double s;
  55.  
  56.  public:   
  57.  
  58.     LineSegment(Point line1, Point line2)
  59.     {
  60.         start = line1;
  61.         finish = line2;
  62.     }
  63.  
  64.     double length()
  65.     {
  66.          len = start.distanceTo(finish);
  67.          return len;
  68.     }
  69.  
  70.     double slope()
  71.     {
  72.         if((finish.getX()-start.getX()) != 0)
  73.         {  
  74.           s = ((finish.getY()-start.getY())/ (finish.getX()-start.getX()));
  75.           return s;
  76.         }
  77.  
  78.         else
  79.         {
  80.             s = 0;
  81.             return s;
  82.         }  
  83.     }
  84. };
  85.  
  86. int main ()
  87. {
  88.     double x1;
  89.     double x2;
  90.     double y1;
  91.     double y2;
  92.     char again;
  93.  
  94.     do{
  95.  
  96.         cout << "Please enter the coordinates for the first point, in the form of 'X, Y'." << endl;
  97.         cin >> x1;
  98.         cin >> y1;
  99.  
  100.         cout << "Please enter the coordinates for the second point, in the form 'X, Y'." << endl;
  101.         cin >> x2;
  102.         cin >> y2;
  103.  
  104.         Point point1 (x1, y1);
  105.         Point point2 (x2,y2);
  106.         LineSegment slopeFind(point1, point2);
  107.        
  108.         cout << "The distance between the points is " << slopeFind.length() << "." << endl;
  109.  
  110.         if(slopeFind.slope() == 0)
  111.         {
  112.             cout << "The line segment is vertical, thus having no slope.\n";
  113.         }  
  114.  
  115.         if(slopeFind.slope() > 0)
  116.         {
  117.             cout << "The slope of the line is " << slopeFind.slope() << ".\n";
  118.         }  
  119.  
  120.         cout << "Would you like to repeat with new coordinates? (Y/N): ";
  121.         cin >> again;
  122.  
  123.     }while(again == 'Y' || again == 'y');
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement