diff options
Reworked exercise 0 of homework1.cpp
1 files changed, 43 insertions, 52 deletions
| -rw-r--r-- | Year 1/Programming introduction/Homeworks/homework1.cpp | 95 |
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp index 562ec4e..1a3d069 100644 --- a/Year 1/Programming introduction/Homeworks/homework1.cpp +++ b/Year 1/Programming introduction/Homeworks/homework1.cpp @@ -10,16 +10,16 @@ using namespace std; /* Floating point arithmetic */ -double const FLOAT_EPSILON_FLOOR = 0.0001, FLOAT_EPSILON_CEIL = FLOAT_EPSILON_FLOOR * 9; +double const FLOAT_EPSILON = 0.001; bool eq(double a, double b) { - return ((a - b) < 0.0 ? (b - a) : (a - b)) <= FLOAT_EPSILON_FLOOR; + return ((a - b) < 0.0 ? (b - a) : (a - b)) < FLOAT_EPSILON; } bool lt(double a, double b) { - return !eq(a, b) && (a - b) < FLOAT_EPSILON_CEIL; + return !eq(a, b) && (a - b) < FLOAT_EPSILON; } bool mt(double a, double b) @@ -29,75 +29,66 @@ bool mt(double a, double b) /* Planimetry */ -double distanceBetweenPoints(int x1, int y1, int x2, int y2) +double distanceBetweenPoints(double x1, double y1, double x2, double y2) { - int sideA = x1 - x2, sideB = y1 - y2; + double 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 - // The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828 +void printPointLocation(double x, double y) { + // The coordinates of points B, C, D and E are some combination of positive and/or negative values of 2.828, + // because BC and DE make 45 degrees angle with the abscissa 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; + SLOPE_BC = CROSS_PNT / CROSS_PNT, + SLOPE_DE = -CROSS_PNT / CROSS_PNT; + char const NOWHERE[] = "Nowhere!"; - double x, y; - cin >> x >> y; - - if (eq(x, 0)) // if it's on the Y ordinate - { - cout << "On a line" << endl; - } + if (eq(x, 0)) + cout << NOWHERE << endl; // On a line else if (lt(distanceBetweenPoints(0, 0, x, y), 4.0)) { double pointSlope = y / x; - 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)) + // Brackets in some places for improved readability + if (eq(x, -CROSS_PNT) || eq(x, CROSS_PNT) || eq(pointSlope, SLOPE_BC) || (eq(pointSlope, SLOPE_DE) && mt(y, 0))) { - cout << "On a line" << endl; + cout << NOWHERE << endl; // On a line } - else if ((mt(y, 0) || eq(y, 0)) && mt(x, 0)) // Check 1st quadrant + else if (mt(pointSlope, SLOPE_BC)) { - if (mt(pointSlope, SLOPE_B)) - cout << "Inside green area!" << endl; - else if (mt(x, CROSS_PNT)) - cout << "Inside purple area!" << endl; - else - cout << "Inside yellow area!" << endl; + cout << "Inside " << ((x > 0) ? "green" : "blue" ) << " area!" << endl; } - else if ((mt(y, 0) || eq(y, 0)) && lt(x, 0)) // Check 2nd quadrant + else if (lt(pointSlope, SLOPE_DE) && x < 0) { - if (lt(pointSlope, SLOPE_D)) - cout << "Inside red area!" << endl; - else if (x > -CROSS_PNT) - cout << "Inside pink area!" << endl; + cout << "Inside red area!" << endl; } - else if (lt(y, 0) && lt(x, 0)) // Check 3rd quadrant + else if (mt(x, CROSS_PNT)) { - if (mt(pointSlope, SLOPE_C)) - cout << "Inside blue area!" << endl; - else if (mt(x, -CROSS_PNT)) - cout << "Inside pink area!" << endl; + cout << "Inside purple area!" << endl; } - else // Check 4th quadrant + else if (mt(x, -CROSS_PNT) && x < 0) { - if (mt(pointSlope, SLOPE_E) && mt(x, CROSS_PNT)) - cout << "Inside purple area!" << endl; - else - cout << "Inside yellow area!" << endl; + cout << "Inside pink area!" << endl; } + else if (x > 0) + cout << "Inside yellow area!" << endl; + else + cout << NOWHERE << endl; } - else if ((lt(-2, x) && lt(x, 2)) && (lt(-6, y) && lt(y, -7))) // Outside circle and in rectangle - { - cout << "Inside grey area" << endl; - } + else if (lt(-2, x) && lt(x, 2) && mt(-6, y) && mt(y, -7)) + cout << "Inside grey area!" << endl; + else + cout << NOWHERE << endl; +} + +/* Exercise calculations */ + +void ex0() +{ + double x, y; + cin >> x >> y; + + printPointLocation(x, y); } /* @@ -327,5 +318,5 @@ void ex4() int main() { - ex1(); + ex0(); } |
