include slope program
This commit is contained in:
committed by
JamesFlare1212
parent
ec9a32372c
commit
ef18fb8634
@@ -150,52 +150,68 @@ Here’s code using exceptions to sort a collection of lines by slope:
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cmath> // for fabs() function
|
||||
#include <cmath>
|
||||
|
||||
class Point {
|
||||
public:
|
||||
Point(double x_, double y_) : x(x_),y(y_) {}
|
||||
double x,y;
|
||||
Point(double x_, double y_) : x(x_),y(y_) {}
|
||||
double x,y;
|
||||
};
|
||||
class Line {
|
||||
public:
|
||||
Line(const Point &a_, const Point &b_) : a(a_),b(b_) {}
|
||||
Point a,b;
|
||||
Line(const Point &a_, const Point &b_) : a(a_),b(b_) {}
|
||||
Point a,b;
|
||||
};
|
||||
|
||||
double compute_slope(const Point &a, const Point &b) {
|
||||
double rise = b.y - a.y;
|
||||
double run = b.x - a.x;
|
||||
double epsilon = 0.00001;
|
||||
if (fabs(run) < epsilon){
|
||||
throw -1;
|
||||
}
|
||||
return rise / run;
|
||||
double rise = b.y - a.y;
|
||||
double run = b.x - a.x;
|
||||
double epsilon = 0.00001;
|
||||
if (fabs(run) < epsilon){
|
||||
throw -1;
|
||||
}
|
||||
return rise / run;
|
||||
}
|
||||
|
||||
double slope(const Line &ln) {
|
||||
return compute_slope(ln.a,ln.b);
|
||||
return compute_slope(ln.a,ln.b);
|
||||
}
|
||||
|
||||
bool steeper_slope(const Line &m, const Line &n) {
|
||||
double slope_m = slope(m);
|
||||
double slope_n = slope(n);
|
||||
return slope_m > slope_n;
|
||||
double slope_m = slope(m);
|
||||
double slope_n = slope(n);
|
||||
return slope_m > slope_n;
|
||||
}
|
||||
|
||||
void organize(std::vector<Line> &lines) {
|
||||
std::sort(lines.begin(),lines.end(), steeper_slope);
|
||||
std::sort(lines.begin(),lines.end(), steeper_slope);
|
||||
}
|
||||
|
||||
int main () {
|
||||
std::vector<Line> lines;
|
||||
/* omitting code to initialize some data */
|
||||
try {
|
||||
organize(lines);
|
||||
/* omitting code to print out the results */
|
||||
} catch (int) {
|
||||
std::cout << "error: infinite slope" << std::endl;
|
||||
}
|
||||
std::vector<Line> lines;
|
||||
|
||||
// adding test lines
|
||||
lines.push_back(Line(Point(0, 0), Point(1, 1))); // slope = 1
|
||||
lines.push_back(Line(Point(0, 0), Point(2, 3))); // slope = 1.5
|
||||
lines.push_back(Line(Point(0, 0), Point(5, 2))); // slope = 0.4
|
||||
// lines.push_back(Line(Point(3, 2), Point(3, 5))); // vertical line (should throw)
|
||||
try {
|
||||
organize(lines);
|
||||
// Print the sorted lines based on steepness
|
||||
std::cout << "Sorted lines by steepest slope:" << std::endl;
|
||||
for (const Line &ln : lines) {
|
||||
std::cout << "Line from (" << ln.a.x << ", " << ln.a.y << ") to ("
|
||||
<< ln.b.x << ", " << ln.b.y << ") - Slope: ";
|
||||
try {
|
||||
std::cout << slope(ln) << std::endl;
|
||||
} catch (int) {
|
||||
std::cout << "undefined (vertical line)" << std::endl;
|
||||
}
|
||||
}
|
||||
} catch (int) {
|
||||
std::cout << "error: infinite slope" << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
66
lectures/17_exceptions/slope.cpp
Normal file
66
lectures/17_exceptions/slope.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
class Point {
|
||||
public:
|
||||
Point(double x_, double y_) : x(x_),y(y_) {}
|
||||
double x,y;
|
||||
};
|
||||
class Line {
|
||||
public:
|
||||
Line(const Point &a_, const Point &b_) : a(a_),b(b_) {}
|
||||
Point a,b;
|
||||
};
|
||||
|
||||
double compute_slope(const Point &a, const Point &b) {
|
||||
double rise = b.y - a.y;
|
||||
double run = b.x - a.x;
|
||||
double epsilon = 0.00001;
|
||||
if (fabs(run) < epsilon){
|
||||
throw -1;
|
||||
}
|
||||
return rise / run;
|
||||
}
|
||||
|
||||
double slope(const Line &ln) {
|
||||
return compute_slope(ln.a,ln.b);
|
||||
}
|
||||
|
||||
bool steeper_slope(const Line &m, const Line &n) {
|
||||
double slope_m = slope(m);
|
||||
double slope_n = slope(n);
|
||||
return slope_m > slope_n;
|
||||
}
|
||||
|
||||
void organize(std::vector<Line> &lines) {
|
||||
std::sort(lines.begin(),lines.end(), steeper_slope);
|
||||
}
|
||||
|
||||
int main () {
|
||||
std::vector<Line> lines;
|
||||
|
||||
// adding test lines
|
||||
lines.push_back(Line(Point(0, 0), Point(1, 1))); // slope = 1
|
||||
lines.push_back(Line(Point(0, 0), Point(2, 3))); // slope = 1.5
|
||||
lines.push_back(Line(Point(0, 0), Point(5, 2))); // slope = 0.4
|
||||
// lines.push_back(Line(Point(3, 2), Point(3, 5))); // vertical line (should throw)
|
||||
try {
|
||||
organize(lines);
|
||||
// Print the sorted lines based on steepness
|
||||
std::cout << "Sorted lines by steepest slope:" << std::endl;
|
||||
for (const Line &ln : lines) {
|
||||
std::cout << "Line from (" << ln.a.x << ", " << ln.a.y << ") to ("
|
||||
<< ln.b.x << ", " << ln.b.y << ") - Slope: ";
|
||||
try {
|
||||
std::cout << slope(ln) << std::endl;
|
||||
} catch (int) {
|
||||
std::cout << "undefined (vertical line)" << std::endl;
|
||||
}
|
||||
}
|
||||
} catch (int) {
|
||||
std::cout << "error: infinite slope" << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user