993A - Two Squares - CodeForces Solution


geometry implementation *1600

Please click on ads to support us..

Python Code:

ans = False
x22 = -105
y22 = -105
minx2 = 121
miny2 = 112
mxt = -105
maximusy = -105
mixxxx = 112
miniy = 121
maximusy= -105


y1, x1,y2,x2,y3,x3, y4,x4 = map(int, input().split())
x5,y5,x6,y6,x7,y7,x8,y8 = map(int, input().split())

ys = [x1,x2,x3,x4]
xs = [y1,y2,y3,y4]

for i in xs:
    mxt = max(mxt,i)
    mixxxx = min(mixxxx,i)

for i in ys:
    maximusy = max(maximusy,i)
    miniy = min(miniy,i)



xs = [x5,x6,x7,x8]
ys = [y5,y6,y7,y8]
for i in xs:
    x22 = max(x22,i)
    minx2 = min(minx2,i)

for i in ys:
    y22 = max(y22,i)
    miny2 = min(miny2,i)

mid = (x22-minx2)//2
cont = 0

for i in range(minx2, x22+1):
    if(mid>0):
        for j in range(miny2 + mid,y22 -mid + 1):
            if i <= mxt and i >=mixxxx and j <= maximusy and j >= miniy:
                ans = True
                break
        mid-=1
    else:
        for j in range(miny2 + cont, y22-cont+1):
            if(i<= mxt and i>= mixxxx and j <= maximusy and j >= miniy):
                ans = True
                break
        cont+=1

print('YES' if ans else 'NO')
 	 			     			    	      	 				

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector< pair<double, double> >norm_square;
    vector< pair<double, double> >rotate_square;
    for (int i=0; i<4; i++) {
        double x, y;
        cin >> x >> y;
        norm_square.push_back({x, y});
    }
    norm_square.push_back(norm_square[0]);
    for (int i=0; i<4; i++) {
        double x, y;
        cin >> x >> y;
        rotate_square.push_back({x, y});
    }
    rotate_square.push_back(rotate_square[0]);
    bool flag = true;
    for (auto p : rotate_square) {
        double x, y;
        x = p.first;
        y = p.second;
        for (int j=0; j<norm_square.size() - 1; j++) {
            double x1 = norm_square[j].first, y1 = norm_square[j].second;
            double x2 = norm_square[j + 1].first, y2 = norm_square[j + 1].second;
            double A = y2 - y1;
            double B = x1 - x2;
            double C = x1 * (y1 - y2) + y1 * (x2 - x1);
            double a = B;
            double b = -A;
            double c = -B * x + A * y;
            // cout << A << " " << B << " " << C << "\n";
            // cout << a << " " << b << " " << c << "\n";
            double px, py;
            C *= -1;
            c *= -1;
            if (a == 0) {
                py = c / b;
                px = (C - B * py) / A;
            }
            else if (A == 0) {
                py = C / B;
                // py = abs(py);
                // cout << py << "\n";
                px = (c - b * py) / a;
                // cout << px << "\n";
            }
            else if (b == 0) {
                px = c / a;
                py = (C - A * px) / B;
            }
            else if (B == 0) {
                px = C / A;
                py = (c - a * px) / b;
            }
            else {
                double k = (a / A);
                py = (c - C * k) / (b - B * k);
                px = (-b * py + c) / a;
            }
            // cout << px << " " << py << "\n";
            // return 0;
            if (min(x1, x2) <= px && px <= max(x1, x2)) {
                if (min(y1, y2) <= py && py <= max(y1, y2)) {
                    // flag = true;
                }
                else {
                    // cout << A << " " << B << " " << C << "\n";
                    // cout << a << " " << b << " " << c << "\n";
                    // cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
                    // cout << x << " " << y << "\n";
                    // cout << px << " " << py << "\n";
                    // return 0;
                    flag = false;
                    // cout << j << "\n";
                    break;
                }
            }
            else {
                flag = false;
                // cout << A << " " << B << " " << C << "\n";
                // cout << a << " " << b << " " << c << "\n";
                // cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
                // cout << x << " " << y << "\n";
                // cout << px << " " << py << "\n";
                // return 0;
                // cout << px << " " << py << "\n";
                break;
            }
        }
    }
    if (flag) {
        cout << "YES";
        return 0;
    }
    flag = true;
    for (auto p : norm_square) {
        double x, y;
        x = p.first;
        y = p.second;
        // bool flag = true;
        for (int j=0; j<rotate_square.size() - 1; j++) {
            double x1 = rotate_square[j].first, y1 = rotate_square[j].second;
            double x2 = rotate_square[j + 1].first, y2 = rotate_square[j + 1].second;
            double A = y2 - y1;
            double B = x1 - x2;
            double C = x1 * (y1 - y2) + y1 * (x2 - x1);
            double a = B;
            double b = -A;
            double c = -B * x + A * y;
            // cout << A << " " << B << " " << C << "\n";
            // cout << a << " " << b << " " << c << "\n";
            double px, py;
            C *= -1;
            c *= -1;
            if (a == 0) {
                py = c / b;
                px = (C - B * py) / A;
            }
            else if (A == 0) {
                py = C / B;
                py = abs(py);
                px = (c - b * py) / a;
                // cout << px << "\n";
            }
            else if (b == 0) {
                px = c / a;
                py = (C - A * px) / B;
            }
            else if (B == 0) {
                px = C / A;
                py = (c - a * px) / b;
            }
            else {
                double k = (a / A);
                py = (c - C * k) / (b - B * k);
                px = (-b * py + c) / a;
            }
            // cout << px << " " << py << "\n";
            // return 0;
            if (min(x1, x2) <= px && px <= max(x1, x2)) {
                if (min(y1, y2) <= py && py <= max(y1, y2)) {
                    // flag = true;
                }
                else {
                    flag = false;
                    // cout << j << "\n";
                    break;
                }
            }
            else {
                flag = false;
                // cout << px << " " << py << "\n";
                break;
            }
        }
    }
    if (flag) {
        // cout << x << " " << y << "\n";
        cout << "YES";
        return 0;
    }
    for (int i=0; i<rotate_square.size() - 1; i++) {
        for (int j=0; j<norm_square.size() - 1; j++) {
            double x1 = norm_square[j].first, y1 = norm_square[j].second;
            double x2 = norm_square[j + 1].first, y2 = norm_square[j + 1].second;
            double x3 = rotate_square[i].first, y3 = rotate_square[i].second;
            double x4 = rotate_square[i + 1].first, y4 = rotate_square[i + 1].second;
            double A = y2 - y1;
            double B = x1 - x2;
            double C = x1 * (y1 - y2) + y1 * (x2 - x1);
            double a = y4 - y3;
            double b = x3 - x4;
            double c = x3 * (y3 - y4) + y3 * (x4 - x3);
            // cout << A << " " << B << " " << C << "\n";
            // cout << a << " " << b << " " << c << "\n";
            double px, py;
            C *= -1;
            c *= -1;
            if (a == 0) {
                py = c / b;
                px = (C - B * py) / A;
            }
            else if (A == 0) {
                py = C / B;
                py = abs(py);
                px = (c - b * py) / a;
                // cout << px << "\n";
            }
            else if (b == 0) {
                px = c / a;
                py = (C - A * px) / B;
            }
            else if (B == 0) {
                px = C / A;
                py = (c - a * px) / b;
            }
            else {
                double k = (a / A);
                py = (c - C * k) / (b - B * k);
                px = (-b * py + c) / a;
            }
            // cout << px << " " << py << "\n";
            if (min(x1, x2) <= px && px <= max(x1, x2)) {
                if (min(y1, y2) <= py && py <= max(y1, y2)) {
                    if (min(x3, x4) <= px && px <= max(x3, x4)) {
                        if (min(y3, y4) <= py && py <= max(y3, y4)) {
                            // cout << x1 << " " << y1 << "--> " << x2 << " " << y2 << "\n";
                            // cout << x3 << " " << y3 << "--> " << x4 << " " << y4 << "\n";
                            cout << "YES";
                            return 0;
                        }
                    }
                }
            }
        }
        // return 0;
    }
    cout << "NO";
    return 0;
}


Comments

Submit
0 Comments
More Questions

1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments