Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- class Point
- {
- private:
- double x; //x coordinate
- double y; //y coordinate
- double c; //distance to
- public:
- Point()
- {
- x = 0;
- y = 0;
- }
- Point(double xCoord, double yCoord)
- {
- x = xCoord;
- y = yCoord;
- }
- double getX() const
- {
- return x;
- }
- double getY() const
- {
- return y;
- }
- //a^2 + b^2 = c^2 - find the hypotenuse
- double distanceTo(const Point& pointTo)
- {
- c = sqrt(pow(abs((x - pointTo.getX())), 2) + pow(abs((y - pointTo.getY())), 2));
- return c;
- }
- };
- class LineSegment
- {
- private:
- Point start;
- Point finish;
- double len;
- double s;
- public:
- LineSegment(Point line1, Point line2)
- {
- start = line1;
- finish = line2;
- }
- double length()
- {
- len = start.distanceTo(finish);
- return len;
- }
- double slope()
- {
- if((finish.getX()-start.getX()) != 0)
- {
- s = ((finish.getY()-start.getY())/ (finish.getX()-start.getX()));
- return s;
- }
- else
- {
- s = 0;
- return s;
- }
- }
- };
- int main ()
- {
- double x1;
- double x2;
- double y1;
- double y2;
- char again;
- do{
- cout << "Please enter the coordinates for the first point, in the form of 'X, Y'." << endl;
- cin >> x1;
- cin >> y1;
- cout << "Please enter the coordinates for the second point, in the form 'X, Y'." << endl;
- cin >> x2;
- cin >> y2;
- Point point1 (x1, y1);
- Point point2 (x2,y2);
- LineSegment slopeFind(point1, point2);
- cout << "The distance between the points is " << slopeFind.length() << "." << endl;
- if(slopeFind.slope() == 0)
- {
- cout << "The line segment is vertical, thus having no slope.\n";
- }
- if(slopeFind.slope() > 0)
- {
- cout << "The slope of the line is " << slopeFind.slope() << ".\n";
- }
- cout << "Would you like to repeat with new coordinates? (Y/N): ";
- cin >> again;
- }while(again == 'Y' || again == 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement