:heavy_check_mark: test/Geometry/integer_line/aoj-CGL_7_C.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C
// competitive-verifier: ERROR 0.000001

#include "Geometry/integer_line.hpp"
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    cin >> x3 >> y3;

    const IntegerLine::Point p1 = {x1, y1};
    const IntegerLine::Point p2 = {x2, y2};
    const IntegerLine::Point p3 = {x3, y3};

    const IntegerLine bisector1 = IntegerLine::perpendicular_bisector(p1, p2);

    const IntegerLine bisector2 = IntegerLine::perpendicular_bisector(p1, p3);

    const optional<IntegerLine::Intersection> intersection = bisector1.intersection(bisector2);

    assert(intersection.has_value());

    const auto [xNumerator, yNumerator, denominator] = intersection.value();

    const long double centerX = static_cast<long double>(xNumerator) / static_cast<long double>(denominator);
    const long double centerY = static_cast<long double>(yNumerator) / static_cast<long double>(denominator);

    const long double dx = centerX - x1;
    const long double dy = centerY - y1;
    const long double radius = sqrt(dx * dx + dy * dy);

    cout << fixed << setprecision(15) << centerX << " " << centerY << " " << radius << endl;

    return 0;
}
#line 1 "test/Geometry/integer_line/aoj-CGL_7_C.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_C
// competitive-verifier: ERROR 0.000001

#line 1 "Geometry/integer_line.hpp"
#include <bits/stdc++.h>
using namespace std;

struct IntegerLine
{
    using Point = std::pair<__int128_t, __int128_t>;
    using Intersection = std::array<__int128_t, 3>;

    // A*x + B*y + C = 0
    __int128_t A, B, C;

    IntegerLine(__int128_t A, __int128_t B, __int128_t C) : A(A), B(B), C(C)
    {
        assert(A != 0 || B != 0);
        normalize();
    }

    // 異なる2点を通る直線
    static IntegerLine through(Point p, Point q)
    {
        const auto [px, py] = p;
        const auto [qx, qy] = q;

        assert(p != q);

        return IntegerLine(py - qy, qx - px, px * qy - qx * py);
    }

    // 異なる2点の垂直二等分線
    static IntegerLine perpendicular_bisector(Point p, Point q)
    {
        const auto [px, py] = p;
        const auto [qx, qy] = q;

        assert(p != q);

        return IntegerLine(2 * (qx - px), 2 * (qy - py), px * px + py * py - qx * qx - qy * qy);
    }

    // 点を直線方程式の左辺へ代入した値
    __int128_t eval(Point p) const
    {
        const auto [x, y] = p;
        return A * x + B * y + C;
    }

    // 点が直線上にあるか
    bool contains(Point p) const
    {
        return eval(p) == 0;
    }

    // 2点が直線の同じ側にあるか
    // どちらかが直線上にある場合は false
    bool same_side(Point p, Point q) const
    {
        const __int128_t pValue = eval(p);
        const __int128_t qValue = eval(q);

        return (pValue > 0 && qValue > 0) || (pValue < 0 && qValue < 0);
    }

    // 2点が直線の反対側にあるか
    // どちらかが直線上にある場合は false
    bool opposite_side(Point p, Point q) const
    {
        const __int128_t pValue = eval(p);
        const __int128_t qValue = eval(q);

        return (pValue > 0 && qValue < 0) || (pValue < 0 && qValue > 0);
    }

    // 2直線が平行か
    bool is_parallel(const IntegerLine &other) const
    {
        return A * other.B == B * other.A;
    }

    // 2直線が同一か
    bool is_same(const IntegerLine &other) const
    {
        return A == other.A && B == other.B && C == other.C;
    }

    // 2直線が垂直か
    bool is_perpendicular(const IntegerLine &other) const
    {
        return A * other.A + B * other.B == 0;
    }

    // 交点を {xの分子, yの分子, 共通分母} で返す
    // 平行または同一直線の場合は std::nullopt
    std::optional<Intersection> intersection(const IntegerLine &other) const
    {
        __int128_t denominator = A * other.B - other.A * B;

        if (denominator == 0)
        {
            return std::nullopt;
        }

        __int128_t xNumerator = B * other.C - other.B * C;

        __int128_t yNumerator = C * other.A - other.C * A;

        if (denominator < 0)
        {
            denominator = -denominator;
            xNumerator = -xNumerator;
            yNumerator = -yNumerator;
        }

        return Intersection{xNumerator, yNumerator, denominator};
    }

private:
    static __int128_t abs_value(__int128_t value)
    {
        return value < 0 ? -value : value;
    }

    static __int128_t gcd(__int128_t a, __int128_t b)
    {
        a = abs_value(a);
        b = abs_value(b);

        while (b != 0)
        {
            const __int128_t remainder = a % b;
            a = b;
            b = remainder;
        }

        return a;
    }

    void normalize()
    {
        const __int128_t divisor = gcd(gcd(A, B), C);

        A /= divisor;
        B /= divisor;
        C /= divisor;

        // 最初の非ゼロ係数を正に統一する。
        // A と B が同時に 0 になることはない。
        if (A < 0 || (A == 0 && B < 0))
        {
            A = -A;
            B = -B;
            C = -C;
        }
    }
};
#line 6 "test/Geometry/integer_line/aoj-CGL_7_C.cpp"

using namespace std;

int main()
{
    long long x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    cin >> x3 >> y3;

    const IntegerLine::Point p1 = {x1, y1};
    const IntegerLine::Point p2 = {x2, y2};
    const IntegerLine::Point p3 = {x3, y3};

    const IntegerLine bisector1 = IntegerLine::perpendicular_bisector(p1, p2);

    const IntegerLine bisector2 = IntegerLine::perpendicular_bisector(p1, p3);

    const optional<IntegerLine::Intersection> intersection = bisector1.intersection(bisector2);

    assert(intersection.has_value());

    const auto [xNumerator, yNumerator, denominator] = intersection.value();

    const long double centerX = static_cast<long double>(xNumerator) / static_cast<long double>(denominator);
    const long double centerY = static_cast<long double>(yNumerator) / static_cast<long double>(denominator);

    const long double dx = centerX - x1;
    const long double dy = centerY - y1;
    const long double radius = sqrt(dx * dx + dy * dy);

    cout << fixed << setprecision(15) << centerX << " " << centerY << " " << radius << endl;

    return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_00 :heavy_check_mark: AC 3 ms 4 MB
g++ 00_sample_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 01_general_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 01_general_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 01_general_02 :heavy_check_mark: AC 2 ms 4 MB
g++ 01_general_03 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_02 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_03 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_04 :heavy_check_mark: AC 2 ms 4 MB
g++ 02_right_05 :heavy_check_mark: AC 2 ms 4 MB
g++ 03_obtuse_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 03_obtuse_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 03_obtuse_02 :heavy_check_mark: AC 2 ms 4 MB
g++ 03_obtuse_03 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_02 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_03 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_04 :heavy_check_mark: AC 2 ms 4 MB
g++ 04_isosceles_05 :heavy_check_mark: AC 2 ms 4 MB
g++ 05_extreme_small_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 05_extreme_small_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_00 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_01 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_02 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_03 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_04 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_05 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_06 :heavy_check_mark: AC 2 ms 4 MB
g++ 06_extreme_large_07 :heavy_check_mark: AC 2 ms 4 MB
Back to top page

This site uses Just the Docs, a documentation theme for Jekyll.