compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:heavy_check_mark: geometry/tangent.hpp

Depends on

Required by

Verified with

Code

#pragma once

#include <cmath>
#include <vector>

#include "circle.hpp"
#include "line.hpp"
#include "rotate.hpp"
#include "unit-vector.hpp"

namespace geometry {
    // 円の共通接線
    inline std::vector<Line> tangent(const Circle &a, const Circle &b) {
        std::vector<Line> ret;
        // 2円の中心間の距離
        D g = std::abs(a.p - b.p);
        // 円が内包されている場合
        if(equal(g, 0)) return ret;
        Point u = unitVector(b.p - a.p);
        Point v = rotate(u, PI / 2);
        for(int s : {-1, 1}) {
            D h = (a.r + b.r * s) / g;
            if(equal(h * h, 1)) {
                ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,
                                 a.p + (h > 0 ? u : -u) * a.r + v);

            } else if(1 - h * h > 0) {
                Point U = u * h, V = v * std::sqrt(1 - h * h);
                ret.emplace_back(a.p + (U + V) * a.r,
                                 b.p - (U + V) * (b.r * s));
                ret.emplace_back(a.p + (U - V) * a.r,
                                 b.p - (U - V) * (b.r * s));
            }
        }
        return ret;
    }
} // namespace geometry
#line 2 "geometry/tangent.hpp"

#include <cmath>
#include <vector>

#line 2 "geometry/circle.hpp"

#line 2 "geometry/base.hpp"

#line 4 "geometry/base.hpp"
#include <complex>

namespace geometry {
    // Point : 複素数型を位置ベクトルとして扱う
    // 実軸(real)をx軸、挙軸(imag)をy軸として見る
    using D = long double;
    using Point = std::complex<D>;
    const D EPS = 1e-7;
    const D PI = std::acos(D(-1));

    inline bool equal(const D &a, const D &b) { return std::fabs(a - b) < EPS; }
} // namespace geometry
#line 4 "geometry/circle.hpp"

namespace geometry {
    // Circle : 円を表す構造体
    // pが中心の位置ベクトル、rは半径
    struct Circle {
        Point p;
        D r;

        Circle() = default;

        Circle(Point p, D r) : p(p), r(r) {}
    };
} // namespace geometry
#line 2 "geometry/line.hpp"

#line 4 "geometry/line.hpp"

namespace geometry {
    // Line : 直線を表す構造体
    // b - a で直線・線分を表せる
    struct Line {
        Point a, b;
        Line() = default;
        Line(Point a, Point b) : a(a), b(b) {}
        // Ax+By=C
        Line(D A, D B, D C) {
            if(equal(A, 0)) {
                a = Point(0, C / B), b = Point(1, C / B);
            } else if(equal(B, 0)) {
                a = Point(C / A, 0), b = Point(C / A, 1);
            } else if(equal(C, 0)) {
                a = Point(0, C / B), b = Point(1, (C - A) / B);
            } else {
                a = Point(0, C / B), b = Point(C / A, 0);
            }
        }
    };
} // namespace geometry
#line 2 "geometry/rotate.hpp"

#line 4 "geometry/rotate.hpp"

namespace geometry {
    // 点pを反時計回りにtheta度回転
    // thetaはラジアン!!!
    inline Point rotate(const Point &p, const D &theta) {
        return Point(std::cos(theta) * p.real() - std::sin(theta) * p.imag(),
                     std::sin(theta) * p.real() + std::cos(theta) * p.imag());
    }
} // namespace geometry
#line 2 "geometry/unit-vector.hpp"

#line 4 "geometry/unit-vector.hpp"

namespace geometry {
    // 単位ベクトル(unit vector)を求める
    inline Point unitVector(const Point &a) { return a / std::abs(a); }
} // namespace geometry
#line 10 "geometry/tangent.hpp"

namespace geometry {
    // 円の共通接線
    inline std::vector<Line> tangent(const Circle &a, const Circle &b) {
        std::vector<Line> ret;
        // 2円の中心間の距離
        D g = std::abs(a.p - b.p);
        // 円が内包されている場合
        if(equal(g, 0)) return ret;
        Point u = unitVector(b.p - a.p);
        Point v = rotate(u, PI / 2);
        for(int s : {-1, 1}) {
            D h = (a.r + b.r * s) / g;
            if(equal(h * h, 1)) {
                ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,
                                 a.p + (h > 0 ? u : -u) * a.r + v);

            } else if(1 - h * h > 0) {
                Point U = u * h, V = v * std::sqrt(1 - h * h);
                ret.emplace_back(a.p + (U + V) * a.r,
                                 b.p - (U + V) * (b.r * s));
                ret.emplace_back(a.p + (U - V) * a.r,
                                 b.p - (U - V) * (b.r * s));
            }
        }
        return ret;
    }
} // namespace geometry
Back to top page