-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS161 Assignment 6 - Coordinates.cpp
More file actions
95 lines (76 loc) · 2.3 KB
/
CS161 Assignment 6 - Coordinates.cpp
File metadata and controls
95 lines (76 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cmath>
#include <limits.h>
#include <sstream>
using namespace std;
class Point {
private:
double x, y;
public:
Point(double x = 0, double y = 0){
this->x = x;
this->y = y;
}
double getX() { return x; }
double getY() { return y; }
void setPos (double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
string toString() { return "(" + to_string(x) + ", " + to_string(y) + ")"; }
double getDistance(Point other) {
double dx = x - other.getX();
double dy = y - other.getY();
return sqrt(dx * dx + dy * dy);
}
double getSlope(Point other) {
double dx = x - other.getX();
double dy = y - other.getY();
if (dx == 0)
return INT_MAX; // slope is undefined for vertical lines
return dy/dx;
}
int getQuadrant(){
if(x == 0 || y == 0) return 0;
if(x > 0 && y > 0) return 1;
if(x < 0 && y > 0) return 2;
if(x < 0 && y < 0) return 3;
return 4;
}
static void setPos(Point &point, double x, double y){
point.setPos(x, y);
}
};
void setPoint(int pointNum, Point &p){
double x, y = 0;
cout << "Enter the X and Y values for point " << pointNum << " (separated by a space): ";
cin >> x >> y;
Point::setPos(p, x, y);
}
bool toContinue () {
cout << "Would you like to continue? (Y/N)? ";
string input;
cin >> input;
while (input != "Y" && input != "N"){
cout << "Please respond with Y or N: ";
cin >> input;
}
return (input == "Y");
}
void runCoords (Point &p1, Point &p2){
setPoint(1, p1);
setPoint(2, p2);
cout << "Point 1: " << p1.toString() << ((p1.getQuadrant() == 0) ? " is not in a quadrant" : (" is in quadrant " + to_string(p1.getQuadrant()))) << endl;
cout << "Point 2: " << p2.toString() << ((p2.getQuadrant() == 0) ? " is not in a quadrant" : (" is in quadrant " + to_string(p2.getQuadrant()))) << endl;
cout << "The distance between the points is " << p1.getDistance(p2) << endl;
cout << "The slope of the line passing through them is " << p1.getSlope(p2) << endl;
}
int main() {
Point p1;
Point p2;
runCoords(p1, p2);
while (toContinue()) {
runCoords(p1, p2);
}
return 0;
}