diff options
Added comments and moved number to constant in ex0 homework1.cpp
1 files changed, 17 insertions, 9 deletions
| -rw-r--r-- | Year 1/Programming introduction/Homeworks/homework1.cpp | 26 |
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp index b0eed83..0ae657c 100644 --- a/Year 1/Programming introduction/Homeworks/homework1.cpp +++ b/Year 1/Programming introduction/Homeworks/homework1.cpp @@ -8,6 +8,8 @@ using namespace std; * Exercise 0 */ +/* Floating point arithmetic */ + double const FLOAT_EPSILON_FLOOR = 0.0001, FLOAT_EPSILON_CEIL = FLOAT_EPSILON_FLOOR * 9; bool eq(double a, double b) { @@ -22,18 +24,24 @@ bool mt(double a, double b) { return lt(b, a); } +/* Planimetry */ + double distanceBetweenPoints(int x1, int y1, int x2, int y2) { int sideA = x1 - x2, sideB = y1 - y2; return sqrt(sideA * sideA + sideB * sideB); // Pythagorean theorem } +/* Exercise calculations */ + void ex0() { // These should be precalculated, but this shows how they're found out - float const SLOPE_B = 2.828 / 2.828, - SLOPE_C = -2.828 / -2.828, - SLOPE_D = -2.828 / 2.828, - SLOPE_E = 2.828 / -2.828; + // The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828 + float const CROSS_PNT = 2.828, + SLOPE_B = CROSS_PNT / CROSS_PNT, + SLOPE_C = -CROSS_PNT / -CROSS_PNT, + SLOPE_D = -CROSS_PNT / CROSS_PNT, + SLOPE_E = CROSS_PNT / -CROSS_PNT; double x, y; cin >> x >> y; @@ -43,12 +51,12 @@ void ex0() { else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0)) { double pointSlope = y / x; - if (eq(x, -2.828) || eq(x, 2.828) || eq(pointSlope, SLOPE_B) || eq(pointSlope, SLOPE_C) || eq(pointSlope, SLOPE_D) || eq(pointSlope, SLOPE_E)) + if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT) || eq(pointSlope, SLOPE_B) || eq(pointSlope, SLOPE_C) || eq(pointSlope, SLOPE_D) || eq(pointSlope, SLOPE_E)) cout << "On a line" << endl; else if ((mt(y, 0) || eq(y, 0)) && mt(x, 0)) { // Check 1st quadrant if (mt(pointSlope, SLOPE_B)) cout << "Inside green area!" << endl; - else if (mt(x, 2.828)) + else if (mt(x, CROSS_PNT)) cout << "Inside purple area!" << endl; else cout << "Inside yellow area!" << endl; @@ -56,17 +64,17 @@ void ex0() { else if ((mt(y, 0) || eq(y, 0)) && lt(x, 0)) { // Check 2nd quadrant if (lt(pointSlope, SLOPE_D)) cout << "Inside red area!" << endl; - else if (x > -2.828) + else if (x > -CROSS_PNT) cout << "Inside pink area!" << endl; } else if (lt(y, 0) && lt(x, 0)) { // Check 3rd quadrant if (mt(pointSlope, SLOPE_C)) cout << "Inside blue area!" << endl; - else if (mt(x, -2.828)) + else if (mt(x, -CROSS_PNT)) cout << "Inside pink area!" << endl; } else { // Check 4th quadrant - if (mt(pointSlope, SLOPE_E) && mt(x, 2.828)) + if (mt(pointSlope, SLOPE_E) && mt(x, CROSS_PNT)) cout << "Inside purple area!" << endl; else cout << "Inside yellow area!" << endl; |
