Tifa's CP Library

:heavy_check_mark: test/cpv_local/geo2d/make_c.uva12304.cpp

Depends on

Code

// competitive-verifier: LOCALCASE test/cpv_local/_data/uva/12304

#include "../../../src/geo2d/make_c/rcc_ex/lib.hpp"
#include "../../../src/geo2d/make_c/rll/lib.hpp"
#include "../../../src/geo2d/make_c/rpl/lib.hpp"
#include "../../../src/geo2d/tan/cp/lib.hpp"
#include "../../../src/geo2d/tcenter/i/lib.hpp"
#include "../../../src/geo2d/tcenter/o/lib.hpp"

using namespace tifa_libs;
using std::cin, std::cout;
using data_t = f64;
using Point2 = point<data_t>;
using Line2 = line<data_t>;
using Triangle = triangle<data_t>;
using Circle2 = circle<data_t>;

CEXP data_t PI = pi_v<data_t>;

int main() {
  cout << std::fixed << std::setprecision(6);

  strn s;
  while (cin >> s) {
    if (s == "CircumscribedCircle") {
      Triangle t;
      cin >> t;
      Point2 o = center_O(t);
      data_t r = radius_O(t);
      cout << '(' << o.x << ',' << o.y << ',' << r << ")\n";
    } else if (s == "InscribedCircle") {
      Triangle t;
      cin >> t;
      Point2 i = center_I(t);
      data_t r = radius_I(t);
      cout << '(' << i.x << ',' << i.y << ',' << r << ")\n";
    } else if (s == "TangentLineThroughPoint") {
      Circle2 c;
      Point2 p;
      cin >> c >> p;
      auto tps = tan_CP(c, p);
      if (!tps) {
        cout << "[]\n";
      } else {
        auto [tps0, tps1] = tps.value();
        Point2 d1 = Line2{p, tps0}.direction(), d2 = Line2{p, tps1}.direction();
        data_t angle1 = atan2(d1.y, d1.x) / PI * 180;
        data_t angle2 = atan2(d2.y, d2.x) / PI * 180;
        if (angle1 < 0) angle1 += 180;
        if (angle1 >= 180) angle1 -= 180;
        if (angle2 < 0) angle2 += 180;
        if (angle2 >= 180) angle2 -= 180;
        if (is_eq(angle1, angle2)) {
          cout << '[' << angle1 << "]\n";
        } else {
          if (angle1 > angle2) std::swap(angle1, angle2);
          cout << '[' << angle1 << ',' << angle2 << "]\n";
        }
      }
    } else if (s == "CircleThroughAPointAndTangentToALineWithRadius") {
      Point2 p;
      Line2 l;
      data_t r;
      cin >> p >> l >> r;
      auto cs = make_C_rPL(r, p, l);
      if (!cs) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1] = cs.value();
        if (cs1.o < cs0.o) std::swap(cs0, cs1);
        if (cs0.o == cs1.o) {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << ")]\n";
        } else {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << "),(" << cs1.o.x << ',' << cs1.o.y << ")]\n";
        }
      }
    } else if (s == "CircleTangentToTwoLinesWithRadius") {
      Line2 l1, l2;
      data_t r;
      cin >> l1 >> l2 >> r;
      auto cs_ = make_C_rLL(r, l1, l2);
      if (!cs_) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1, cs2, cs3] = cs_.value();
        vec<Circle2> cs{cs0, cs1, cs2, cs3};
        std::ranges::sort(cs, [](Circle2 CR lhs, Circle2 CR rhs) { return lhs.o < rhs.o; });
        cout << "[(" << cs[0].o.x << ',' << cs[0].o.y << "),(" << cs[1].o.x << ',' << cs[1].o.y << "),(" << cs[2].o.x << ',' << cs[2].o.y << "),(" << cs[3].o.x << ',' << cs[3].o.y << ")]\n";
      }
    } else if (s == "CircleTangentToTwoDisjointCirclesWithRadius") {
      Circle2 c1, c2;
      data_t r;
      cin >> c1 >> c2 >> r;
      auto cs = make_C_rCC_ex(r, c1, c2);
      if (!cs) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1] = cs.value();
        if (cs1.o < cs0.o) std::swap(cs0, cs1);
        if (cs0.o == cs1.o) {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << ")]\n";
        } else {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << "),(" << cs1.o.x << ',' << cs1.o.y << ")]\n";
        }
      }
    }
  }
}
#line 1 "test/cpv_local/geo2d/make_c.uva12304.cpp"
// competitive-verifier: LOCALCASE test/cpv_local/_data/uva/12304

#line 2 "src/geo2d/make_c/rcc_ex/lib.hpp"

#line 2 "src/geo2d/ins/cc/lib.hpp"

#line 2 "src/geo2d/rel/cc/lib.hpp"

#line 2 "src/geo2d/dis/pp_p/lib.hpp"

#line 2 "src/math/qpow/basic/lib.hpp"

#line 2 "src/util/alias/num/lib.hpp"

#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {

#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::ranges;
using namespace std::literals;

template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }

}  // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {

#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_

}  // namespace tifa_libs
// clang-format on
#line 4 "src/math/qpow/basic/lib.hpp"

namespace tifa_libs {

template <class T>
CEXP T qpow(T a, u64 b, cT_(T) init_v = T{1}) NE {
  T res = init_v;
  for (; b; b >>= 1, a = a * a) {
    while (!(b & 1)) b >>= 1, a = a * a;
    res = res * a;
  }
  return res;
}

}  // namespace tifa_libs
#line 2 "src/geo2d/dis/pp/lib.hpp"

#line 2 "src/geo2d/ds/p/lib.hpp"

#line 2 "src/util/func_fp/lib.hpp"

#line 2 "src/util/consts/lib.hpp"

#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();

inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
}  // namespace tifa_libs
// clang-format on
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#line 4 "src/util/traits/math/lib.hpp"

namespace tifa_libs {

template <class T> concept char_c = std::same_as<T, char> || std::same_as<T, signed char> || std::same_as<T, unsigned char>;
#pragma GCC diagnostic ignored "-Wpedantic"
template <class T> concept s128_c = std::same_as<T, __int128_t> || std::same_as<T, __int128>;
template <class T> concept u128_c = std::same_as<T, __uint128_t> || std::same_as<T, unsigned __int128>;
template <class T> concept i128_c = s128_c<T> || u128_c<T>;
#pragma GCC diagnostic warning "-Wpedantic"
template <class T> concept imost64_c = std::integral<T> && sizeof(T) * __CHAR_BIT__ <= 64;
template <class T> concept smost64_c = imost64_c<T> && std::signed_integral<T>;
template <class T> concept umost64_c = imost64_c<T> && std::unsigned_integral<T>;
template <class T> concept int_c = i128_c<T> || imost64_c<T>;
template <class T> concept sint_c = s128_c<T> || smost64_c<T>;
template <class T> concept uint_c = u128_c<T> || umost64_c<T>;
template <class T> concept arithm_c = std::is_arithmetic_v<T> || int_c<T>;
template <class T> concept mint_c = requires(T x) { {x.mod()} -> uint_c; {x.val()} -> uint_c; };
template <class T> concept dft_c = requires(T x, std::vector<TPN T::data_t> v, u32 n) { {x.size()} -> std::same_as<u32>; x.bzr(n); x.dif(v, n); x.dit(v, n); };
template <class T> concept ntt_c = dft_c<T> && requires(T x) { T::max_size; T::G; };

template <class T> struct to_sint : std::make_signed<T> {};
template <> struct to_sint<u128> { using type = i128; };
template <> struct to_sint<i128> { using type = i128; };
template <class T> using to_sint_t = TPN to_sint<T>::type;
template <class T> struct to_uint : std::make_unsigned<T> {};
template <> struct to_uint<u128> { using type = u128; };
template <> struct to_uint<i128> { using type = u128; };
template <class T> using to_uint_t = TPN to_uint<T>::type;
template <arithm_c T> struct to_bigger : std::make_unsigned<T> {};
#define _(w,ww) template <> struct to_bigger<w> { using type = ww; }
#define _2(w,ww) _(i##w,i##ww); _(u##w,u##ww);
_2(8, 16); _2(16, 32); _2(32, 64); _2(64, 128); _(f32, f64); _(f64, f128);
#undef _2
#undef _
template <class T> using to_bigger_t = TPN to_bigger<T>::type;

template <arithm_c T> CEXP T inf_v = [] {
    if CEXP(sint_c<T>) return T(to_uint_t<T>(-1) / 4 - 1);
    else if CEXP(uint_c<T>) return T(-1) / 2 - 1;
    else return std::numeric_limits<T>::max() / 2 - 1;
}();

}  // namespace tifa_libs
// clang-format on
#line 5 "src/util/func_fp/lib.hpp"

namespace tifa_libs {

template <sint_c T>
CEXP int sgn(T x) NE { return (!!x) | (x >> (sizeof(T) * 8 - 1)); }
CEXP int sgn(uint_c auto x) NE { return !!x; }
template <std::floating_point FP>
CEXP int sgn(FP x) NE { return (x > eps_v<FP>)-(x < -eps_v<FP>); }

template <class FP>
CEXP bool is_neg(FP x) NE { return sgn(x) < 0; }
template <class FP>
CEXP bool is_zero(FP x) NE { return !sgn(x); }
template <class FP>
CEXP bool is_pos(FP x) NE { return sgn(x) > 0; }

CEXP int comp(sint_c auto l, sint_c auto r) NE { return sgn(l - r); }
CEXP int comp(uint_c auto l, uint_c auto r) NE { return (!!(l - r)) | -(l < r); }
template <std::floating_point FP>
CEXP int comp(FP l, FP r) NE { return sgn((l - r) / max({abs(l), abs(r), FP(1)})); }

template <class FP>
CEXP bool is_lt(FP l, FP r) NE { return comp(l, r) < 0; }
template <class FP>
CEXP bool is_eq(FP l, FP r) NE { return !comp(l, r); }
template <class FP>
CEXP bool is_gt(FP l, FP r) NE { return comp(l, r) > 0; }

//! containing endpoints
CEXP bool is_in_middle(arithm_c auto l, arithm_c auto mid, arithm_c auto r) NE { return is_eq(l, mid) || is_eq(r, mid) || ((l < mid) ^ (r < mid)); }

//! containing endpoints
template <class FP>
CEXP bool is_intersect(FP l1, FP r1, FP l2, FP r2) NE {
  if (l1 > r1) swap(l1, r1);
  if (l2 > r2) swap(l2, r2);
  return !(is_lt(r1, l2) || is_lt(r2, l1));
}

}  // namespace tifa_libs
#line 2 "src/util/traits/others/lib.hpp"
// clang-format off
#line 2 "src/util/alias/others/lib.hpp"

#line 4 "src/util/alias/others/lib.hpp"

namespace tifa_libs {

template <class T>
struct chash {
  CEXP static u64 C = u64(pi_v<f128> * 2e18) | 71;
  CEXP u64 operator()(T x) CNE { return __builtin_bswap64(((u64)x ^ TIME) * C); }
};
// clang-format off
#define mk_(w, t) using w = t; using c##w = const t;
mk_(strn, std::string) mk_(strnv, std::string_view)
#undef mk_
template <class T> struct edge_t { T w; u32 u, v; CEXP auto operator<=>(edge_t CR) const = default; }; template <class T> using cedge_t = const edge_t<T>;
template <class T> struct pt3 { T _0, _1, _2; CEXP auto operator<=>(pt3 CR) const = default; }; template <class T> using cpt3 = const pt3<T>;
template <class T> struct pt4 { T _0, _1, _2, _3; CEXP auto operator<=>(pt4 CR) const = default; }; template <class T> using cpt4 = const pt4<T>;
#define mkT_(w, t, ...) template <class T> using w = t __VA_OPT__(, ) __VA_ARGS__; template <class T> using c##w = const t __VA_OPT__(, ) __VA_ARGS__;
mkT_(ptt, std::pair<T, T>) mkT_(alc, std::pmr::polymorphic_allocator<T>) mkT_(vec, std::vector<T>) mkT_(vvec, vec<vec<T>>) mkT_(v3ec, vvec<vec<T>>) mkT_(vecpt, vec<ptt<T>>) mkT_(vvecpt, vvec<ptt<T>>) mkT_(ptvec, ptt<vec<T>>) mkT_(ptvvec, ptt<vvec<T>>)
#undef mkT_
template <class T> using itl = std ::initializer_list<T>;
template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>; template <class T, usz N> using carr = std::array<const T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>; template <class U, class T> using vvecp = vvec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>; template <class U, class T> using vvvecp = vvec<vvec<std::pair<U, T>>>;
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class T, class C = std::less<T>> using set = __gnu_pbds::tree<T, __gnu_pbds::null_type, C>;
template <class K, class V, class C = std::less<K>> using map = __gnu_pbds::tree<K, V, C>;
// hset<u64> s({}, {}, {}, {}, {1<<16});
template <class T, class HF = chash<T>> using hset = __gnu_pbds::gp_hash_table<T, __gnu_pbds::null_type, HF>;
// hmap<u64, int> s({}, {}, {}, {}, {1<<16});
template <class K, class V, class HF = chash<K>> using hmap = __gnu_pbds::gp_hash_table<K, V, HF>;
#else
using std::set, std::map;
template <class T, class HF = chash<T>> using hset = std::unordered_set<T, HF>;
template <class K, class V, class HF = chash<K>> using hmap = std::unordered_map<K, V, HF>;
#endif
#ifdef PB_DS_PRIORITY_QUEUE_HPP
template <class T, class C = std::less<T>> using pq = __gnu_pbds::priority_queue<T, C>;
#else
template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
#endif
template <class T> using pqg = pq<T, std::greater<T>>;
// clang-format on
#define mk1_(V, A, T) using V##A = V<T>;
#define mk_(V, A, T) mk1_(V, A, T) mk1_(c##V, A, T)
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk1_(spn, A, T) mk1_(itl, A, T)
mk(b, bool) mk(c, chr) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64) mk(t, isz) mk(z, usz) mk(f, f32) mk(d, f64) mk(s, strn);
#undef mk
#undef mk_
#undef mk1_

}  // namespace tifa_libs
#line 4 "src/util/traits/others/lib.hpp"

namespace tifa_libs {

//! only for template without non-type argument
template <class, template <class...> class> CEXP bool specialized_from_v = false;
template <template <class...> class T, class... Args> CEXP bool specialized_from_v<T<Args...>, T> = true;
static_assert(specialized_from_v<vecu, std::vector>);
template <class T> concept container_c = common_range<T> && !std::is_array_v<std::remove_cvref_t<T>> && !std::same_as<std::remove_cvref_t<T>, strn> && !std::same_as<std::remove_cvref_t<T>, strnv>;
template <class T> concept istream_c = std::derived_from<T, std::istream> || std::derived_from<T, std::wistream> || requires(T is) { is.peek(); };
template <class T> concept ostream_c = std::derived_from<T, std::ostream> || std::derived_from<T, std::wostream> || requires(T os) { os.flush(); };

}  // namespace tifa_libs
// clang-format on
#line 5 "src/geo2d/ds/p/lib.hpp"

namespace tifa_libs {

template <class FP>
struct point {
  using FP_t = FP;

  FP x, y;
  CEXP point() = default;
  CEXP point(FP x, FP y) NE : x{x}, y{y} {}

  friend auto& operator>>(istream_c auto& is, point& p) NE { return is >> p.x >> p.y; }
  friend auto& operator<<(ostream_c auto& os, point CR p) NE { return os << p.x << ' ' << p.y; }
  // s + (t - s) * r
  template <std::floating_point T>
  friend CEXP point lerp(point CR s, point CR t, T r) NE { return s + (t - s) * r; }
  friend CEXP point mid_point(point CR s, point CR t) NE { return lerp(s, t, .5); }
  CEXP point& operator+=(arithm_c auto n) NE {
    this->x += n, this->y += n;
    return *this;
  }
  CEXP point& operator-=(arithm_c auto n) NE {
    this->x -= n, this->y -= n;
    return *this;
  }
  CEXP point& operator*=(arithm_c auto n) NE {
    this->x *= n, this->y *= n;
    return *this;
  }
  CEXP point& operator/=(arithm_c auto n) NE {
    this->x /= n, this->y /= n;
    return *this;
  }
  friend CEXP point operator+(point x, arithm_c auto n) NE { return x += n; }
  friend CEXP point operator+(arithm_c auto n, point x) NE { return x += n; }
  friend CEXP point operator-(point x, arithm_c auto n) NE { return x -= n; }
  friend CEXP point operator-(arithm_c auto n, point x) NE { return x -= n; }
  friend CEXP point operator*(point x, arithm_c auto n) NE { return x *= n; }
  friend CEXP point operator*(arithm_c auto n, point x) NE { return x *= n; }
  friend CEXP point operator/(point x, arithm_c auto n) NE { return x /= n; }
  friend CEXP point operator/(arithm_c auto n, point x) NE { return x /= n; }
  CEXP point& operator+=(point CR p) NE {
    this->x += p.x, this->y += p.y;
    return *this;
  }
  CEXP point& operator-=(point CR p) NE {
    this->x -= p.x, this->y -= p.y;
    return *this;
  }
  CEXP point operator+(point CR p) CNE { return point(*this) += p; }
  CEXP point operator-(point CR p) CNE { return point(*this) -= p; }
  CEXP point operator-() CNE { return point{-x, -y}; }
  CEXP auto operator<=>(point CR p) CNE {
    if (auto CR c = comp(x, p.x); c) return c;
    return comp(y, p.y);
  }
  CEXP bool operator==(point CR p) CNE { return (*this <=> p) == 0; }
  CEXP FP operator*(point CR p) CNE { return x * p.x + y * p.y; }
  CEXP FP operator^(point CR p) CNE { return x * p.y - y * p.x; }
  CEXP FP arg() CNE {
    static_assert(std::is_floating_point_v<FP>);
    return std::atan2(y, x);
  }
  CEXP FP arg_2pi() CNE {
    FP res = arg();
    retif_((is_neg(res)), res + 2 * pi_v<FP>, res);
  }
  CEXP FP norm2() CNE { return x * x + y * y; }
  CEXP FP norm() CNE {
    static_assert(std::is_floating_point_v<FP>);
    return std::hypot(x, y);
  }
  CEXP point& do_unit() NE {
    static_assert(std::is_floating_point_v<FP>);
    return *this /= norm();
  }
  static CEXP arr<u32, 9> QUAD__{6, 7, 8, 5, 0, 1, 4, 3, 2};
  // 4 3 2
  // 5 0 1
  // 6 7 8
  ND CEXP u32 quad() CNE { return QUAD__[(sgn(y) + 1) * 3 + sgn(x) + 1]; }
  CEXP int toleft(point CR p) CNE { return sgn(*this ^ p); }
  CEXP point& do_rot(FP theta) NE {
    const FP x0 = x, y0 = y, ct = std::cos(theta), st = std::sin(theta);
    x = x0 * ct - y0 * st, y = x0 * st + y0 * ct;
    return *this;
  }
  friend CEXP point rot(point p, FP theta) NE { return p.do_rot(theta); }
  CEXP point& do_rot90() NE {
    const FP _ = x;
    x = -y, y = _;
    return *this;
  }
  friend CEXP point rot90(point p) NE { return p.do_rot90(); }
  CEXP point& do_rot270() NE {
    const FP _ = y;
    y = -x, x = _;
    return *this;
  }
  friend CEXP point rot270(point p) NE { return p.do_rot270(); }
};

}  // namespace tifa_libs
#line 4 "src/geo2d/dis/pp/lib.hpp"

namespace tifa_libs {

// distance of two points
// @return $(|x_1-x_2|^p + |y_1-y_2|^p)^{1/p}$, p = 0 means $\infty$
template <class FP, u32 p = 2>
CEXP FP dist_PP(point<FP> CR p1, point<FP> CR p2) NE {
  static_assert(p < 2 || std::floating_point<FP>);
  if CEXP (p == 0) return max(abs(p1.x - p2.x), abs(p1.y - p2.y));   // Chebyshev
  else if CEXP (p == 1) return abs(p1.x - p2.x) + abs(p1.y - p2.y);  // Manhattan
  else if CEXP (p == 2) return (p1 - p2).norm();                     // Euclidian
  else return std::pow(std::pow(abs(p1.x - p2.x), p) + std::pow(abs(p1.y - p2.y), p), FP{1} / p);
}

}  // namespace tifa_libs
#line 5 "src/geo2d/dis/pp_p/lib.hpp"

namespace tifa_libs {

// distance of two points
// @return Chebyshev dis if p = 0, otherwise $(|x_1-x_2|^p + |y_1-y_2|^p)$
template <class FP, u32 p = 2>
CEXP FP distp_PP(point<FP> CR p1, point<FP> CR p2) NE {
  if CEXP (p == 0) return max(abs(p1.x - p2.x), abs(p1.y - p2.y));   // Chebyshev
  else if CEXP (p == 1) return abs(p1.x - p2.x) + abs(p1.y - p2.y);  // Manhattan
  else if CEXP (p == 2) return (p1 - p2).norm2();                    // Euclidian
  else return qpow(abs(p1.x - p2.x), p) + qpow(abs(p1.y - p2.y), p);
}

template <class FP, u32 p = 2>
CEXP auto comp_distp(point<FP> CR p1, point<FP> CR p2, FP r) NE {
  if CEXP (std::floating_point<FP>) return comp(dist_PP<FP, p>(p1, p2), r);
  else return comp(distp_PP<FP, p>(p1, p2), qpow(r, p ? p : 1));
}
template <class FP, u32 p = 2>
CEXP auto comp_distp(point<FP> CR p1, point<FP> CR p2, point<FP> CR q1, point<FP> CR q2) NE {
  if CEXP (std::floating_point<FP>) return comp(dist_PP<FP, p>(p1, p2), dist_PP<FP, p>(q1, q2));
  else return comp(distp_PP<FP, p>(p1, p2), distp_PP<FP, p>(q1, q2));
}

}  // namespace tifa_libs
#line 2 "src/geo2d/ds/c/lib.hpp"

#line 4 "src/geo2d/ds/c/lib.hpp"

namespace tifa_libs {

template <class FP>
struct circle {
  point<FP> o;
  FP r;

  CEXP circle() = default;
  CEXP circle(point<FP> CR c, FP r) NE : o(c), r(r) {}
  CEXP circle(FP c_x, FP c_y, FP r_) NE : o(c_x, c_y), r(r_) {}

  friend auto& operator>>(istream_c auto& is, circle& c) NE { return is >> c.o >> c.r; }
  friend auto& operator<<(ostream_c auto& os, circle CR c) NE { return os << c.o << ' ' << c.r; }
  friend CEXP bool operator==(circle CR l, circle CR r) NE { return l.o == r.o && l.r == r.r; }
  friend CEXP auto operator<=>(circle CR l, circle CR r) NE {
    if (auto c = l.o <=> r.o; c) return c;
    return comp(l.r, r.r);
  }

  CEXP FP area(FP angle = pi_v<FP> * 2) CNE { return angle * r * r / 2; }
  CEXP FP crown_area(FP angle = pi_v<FP> * 2) CNE { return (angle - std::sin(angle)) * r * r / 2; }
  CEXP FP arc(FP angle = pi_v<FP> * 2) CNE { return angle * r; }
};

}  // namespace tifa_libs
#line 5 "src/geo2d/rel/cc/lib.hpp"

namespace tifa_libs {

// relation between circle and circle
// clang-format off
enum class RELCC : u8 { lyingin, touchin, intersect, touchex, lyingout };
// clang-format on

template <class FP>
CEXP RELCC relation_CC(circle<FP> CR c1, circle<FP> CR c2) NE {
  if (const auto d1 = comp_distp(c1.o, c2.o, c1.r + c2.r); d1 > 0) return RELCC::lyingout;
  else if (d1 == 0) return RELCC::touchex;
  if (const auto d2 = comp_distp(c1.o, c2.o, abs(c1.r - c2.r)); d2 > 0) return RELCC::intersect;
  else if (d2 == 0) return RELCC::touchin;
  return RELCC::lyingin;
}

}  // namespace tifa_libs
#line 4 "src/geo2d/ins/cc/lib.hpp"

namespace tifa_libs {

// intersection point of two circles
//! need to check whether two circles are the same
// maybe duplicate
template <class FP>
CEXP auto ins_CC(circle<FP> CR c1, circle<FP> CR c2) NE {
  assert(!is_eq(c1.o.x, c2.o.x) || !is_eq(c1.o.y, c2.o.y) || !is_eq(c1.r, c2.r));
  std::optional<ptt<point<FP>>> ret;
  if (auto state = relation_CC(c1, c2); state == RELCC::lyingin || state == RELCC::lyingout) return ret;
  const FP d = min(dist_PP(c1.o, c2.o), c1.r + c2.r),
           y = (c1.r * c1.r - c2.r * c2.r + d * d) / (2 * d),
           x = std::sqrt(c1.r * c1.r - y * y);
  const point dr = (c2.o - c1.o).do_unit(), q1 = c1.o + dr * y, q2 = rot90(dr) * x;
  ret.emplace(q1 - q2, q1 + q2);
  return ret;
}

}  // namespace tifa_libs
#line 5 "src/geo2d/make_c/rcc_ex/lib.hpp"

namespace tifa_libs {

// make circle by radius and 2 external tagante circle
// maybe duplicate
template <class FP>
CEXP auto make_C_rCC_ex(FP r, circle<FP> CR c1, circle<FP> CR c2) NE {
  std::optional<ptt<circle<FP>>> ret;
  if (relation_CC(c1, c2) == RELCC::lyingin) return ret;
  auto CR ps = ins_CC<FP>({c1.o, c1.r + r}, {c2.o, c2.r + r});
  if (!ps) return ret;
  ret.emplace(circle{ps->first, r}, circle{ps->second, r});
  return ret;
}

}  // namespace tifa_libs
#line 2 "src/geo2d/make_c/rll/lib.hpp"

#line 2 "src/geo2d/ins/ll/lib.hpp"

#line 2 "src/geo2d/ds/l/lib.hpp"

#line 2 "src/geo2d/cross/lib.hpp"

#line 4 "src/geo2d/cross/lib.hpp"

namespace tifa_libs {

template <class P>
CEXP auto cross(P CR o, P CR a, P CR b) NE { return (a - o) ^ (b - o); }
template <class P>
requires std::floating_point<TPN P::FP_t>
CEXP auto cross_unit(P CR o, P CR a, P CR b) NE { return (a - o).do_unit() ^ (b - o).do_unit(); }
template <class P>
requires std::floating_point<TPN P::FP_t>
CEXP int sgn_cross(P CR o, P CR a, P CR b) NE { return sgn(cross_unit(o, a, b)); }
template <class P>
CEXP int sgn_cross(P CR o, P CR a, P CR b) NE { return sgn(cross(o, a, b)); }

}  // namespace tifa_libs
#line 5 "src/geo2d/ds/l/lib.hpp"

namespace tifa_libs {

template <class FP>
struct line {
  point<FP> l, r;

  CEXP line() = default;
  CEXP line(point<FP> CR s, point<FP> CR t) NE : l(s), r(t) {}
  CEXP line(point<FP> CR s, FP angle_x) NE : l(s), r(s + is_eq(angle_x, pi_v<FP> / 2) ? point<FP>{0, 1} : point<FP>{1, std::tan(angle_x)}) { assert(angle_x > 0 && angle_x < pi_v<FP>); }
  // ax + by + c = 0
  CEXP line(FP a, FP b, FP c) NE {
    if (is_zero(a)) l = {0, -c / b}, r = {1, -c / b};
    else if (is_zero(b)) l = {-c / a, 0}, r = {-c / a, 1};
    else l = {0, -c / b}, r = {1, -(c + a) / b};
  }
  CEXP line(FP s_x, FP s_y, FP t_x, FP t_y) NE : l{s_x, s_y}, r{t_x, t_y} {}

  friend std::istream& operator>>(std::istream& is, line& l) NE { return is >> l.l >> l.r; }
  friend std::ostream& operator<<(std::ostream& os, line CR l) NE { return os << l.l << ' ' << l.r; }
  CEXP point<FP> direction() CNE { return r - l; }
  CEXP bool is_parallel(line CR r) CNE { return is_zero(direction() ^ r.direction()); }
  friend CEXP bool is_parallel(line CR l, line CR r) NE { return l.is_parallel(r); }
  CEXP bool is_same_dir(line CR r) CNE { return is_parallel(r) && is_pos(direction() * r.direction()); }
  friend CEXP bool is_same_dir(line CR l, line CR r) NE { return l.is_same_dir(r); }
  friend CEXP bool operator==(line CR l, line CR r) NE { return l.l == r.l && l.r == r.r; }
  friend CEXP auto operator<=>(line CR l, line CR r) NE {
    if (l == r) return 0;
    if (l.is_same_dir(r)) {
      retif_((r.is_include_strict(l.l)), -1, 1);
    } else if (const auto vl = l.direction(), vr = r.direction(); vl.quad() != vr.quad()) return (i32)vl.quad() - (i32)vr.quad();
    else return -sgn(vl ^ vr);
  }
  CEXP int toleft(point<FP> CR p) CNE { return sgn_cross(l, r, p); }
  // half plane
  CEXP bool is_include_strict(point<FP> CR p) CNE { return toleft(p) > 0; }
  // half plane
  CEXP bool is_include(point<FP> CR p) CNE { return toleft(p) >= 0; }
  // translate @dist along the direction of half plane
  CEXP line& do_push(FP dist) NE {
    const point delta = direction().do_rot90().do_unit() * dist;
    l += delta, r += delta;
    return *this;
  }
};

}  // namespace tifa_libs
#line 4 "src/geo2d/ins/ll/lib.hpp"

namespace tifa_libs {

// judge if two lines are intersected or not
template <class FP>
CEXP bool is_ins_LL(line<FP> CR l1, line<FP> CR l2) NE { return !is_zero(cross(l2.l, l2.r, l1.l) - cross(l2.l, l2.r, l1.r)); }
// intersection point of two lines
template <class FP>
CEXP point<FP> ins_LL(line<FP> CR l1, line<FP> CR l2) NE {
  const FP a1 = cross(l2.l, l2.r, l1.l), a2 = -cross(l2.l, l2.r, l1.r);
  return (l1.l * a2 + l1.r * a1) / (a1 + a2);
}
template <class FP>
CEXP point<FP> ins_LL(line<FP> CR l, FP a, FP b, FP c) NE {
  const FP a1 = abs(a * l.l.x + b * l.l.y + c), a2 = abs(a * l.r.x + b * l.r.y + c);
  return (l.l * a2 + l.r * a1) / (a1 + a2);
}

}  // namespace tifa_libs
#line 5 "src/geo2d/make_c/rll/lib.hpp"

namespace tifa_libs {

// make circle by radius and 2 tagante lines
template <class FP>
CEXP auto make_C_rLL(FP r, line<FP> CR l1, line<FP> CR l2) NE {
  std::optional<pt4<circle<FP>>> ret;
  if (is_parallel(l1, l2)) return ret;
  point<FP> dir1 = l1.direction(), dir2 = l2.direction();
  dir1 *= r / dir1.norm(), dir2 *= r / dir2.norm();
  const point<FP> dirl1 = rot90(dir1), dirr1 = rot270(dir1),
                  dirl2 = rot90(dir2), dirr2 = rot270(dir2);
  const line<FP> u1 = {l1.l + dirl1, l1.r + dirl1}, u2 = {l1.l + dirr1, l1.r + dirr1},
                 v1 = {l2.l + dirl2, l2.r + dirl2}, v2 = {l2.l + dirr2, l2.r + dirr2};
  ret.emplace(circle{ins_LL(u1, v1), r}, circle{ins_LL(u1, v2), r}, circle{ins_LL(u2, v1), r}, circle{ins_LL(u2, v2), r});
  return ret;
}

}  // namespace tifa_libs
#line 2 "src/geo2d/make_c/rpl/lib.hpp"

#line 2 "src/geo2d/dis/pl/lib.hpp"

#line 2 "src/geo2d/proj/lib.hpp"

#line 4 "src/geo2d/proj/lib.hpp"

namespace tifa_libs {

// projection to a line
template <class FP>
CEXP point<FP> proj(line<FP> CR l, point<FP> CR p) NE {
  const point dir = l.direction();
  return l.l + dir * (dir * (p - l.l) / dir.norm2());
}
// reflection about a line
template <class FP>
CEXP point<FP> reflect(line<FP> CR l, point<FP> CR p) NE { return proj(l, p) * 2 - p; }

}  // namespace tifa_libs
#line 4 "src/geo2d/dis/pl/lib.hpp"

namespace tifa_libs {

// min dist_PP from a point to a line
template <class FP>
CEXP FP dist_PL(point<FP> CR p, line<FP> CR s) NE { retif_((s.l == s.r), dist_PP(s.l, p), dist_PP(p, proj(s, p))); }

}  // namespace tifa_libs
#line 2 "src/geo2d/ins/cl/lib.hpp"

#line 6 "src/geo2d/ins/cl/lib.hpp"

namespace tifa_libs {

// intersection point of circle and line
// maybe duplicate
template <class FP>
CEXP auto ins_CL(circle<FP> CR c, line<FP> CR l) NE {
  std::optional<ptt<point<FP>>> ret;
  if (is_gt(abs((c.o - l.l) ^ (l.r - l.l) / dist_PP(l.l, l.r)), c.r)) return ret;
  const FP x = (l.l - c.o) * (l.r - l.l),
           y = l.direction().norm2(),
           d = max(x * x - y * ((l.l - c.o).norm2() - c.r * c.r), FP{});
  const point m = l.l - l.direction() * (x / y), dr = l.direction() * (std::sqrt(d) / y);
  ret.emplace(m - dr, m + dr);
  return ret;
}

}  // namespace tifa_libs
#line 5 "src/geo2d/make_c/rpl/lib.hpp"

namespace tifa_libs {

// make circle by radius, a point passed through and a tagante lines
// maybe duplicate
template <class FP>
CEXP auto make_C_rPL(FP r, point<FP> CR p, line<FP> CR l) NE {
  std::optional<ptt<circle<FP>>> ret;
  FP dis = dist_PL(p, l);
  if (is_pos(dis - r * 2)) return ret;
  point dir = l.direction();
  dir *= r / dir.norm();
  const point dirl = rot90(dir), dirr = rot270(dir);
  if (is_zero(dis)) {
    ret.emplace(circle{p + dirl, r}, circle{p + dirr, r});
    return ret;
  }
  const circle c{p, r};
  auto ps = ins_CL(c, {l.l + dirl, l.r + dirl});
  if (!ps && !(ps = ins_CL(c, {l.l + dirr, l.r + dirr}))) return ret;
  ret.emplace(circle{ps->first, r}, circle{ps->second, r});
  return ret;
}

}  // namespace tifa_libs
#line 2 "src/geo2d/tan/cp/lib.hpp"

#line 4 "src/geo2d/tan/cp/lib.hpp"

namespace tifa_libs {

// tagante points of point to circle
// maybe duplicate
template <class FP>
CEXP auto tan_CP(circle<FP> CR c, point<FP> CR p) NE {
  std::optional<ptt<point<FP>>> ret;
  point v = p - c.o;
  const FP x = v.norm2(), d = x - c.r * c.r;
  if (is_neg(d)) return ret;
  const point q1 = c.o + v * (c.r * c.r / x), q2 = v.do_rot90() * (c.r * std::sqrt(d) / x);
  // counter clock-wise
  ret.emplace(q1 - q2, q1 + q2);
  return ret;
}

}  // namespace tifa_libs
#line 2 "src/geo2d/tcenter/i/lib.hpp"

#line 2 "src/geo2d/ds/t/lib.hpp"

#line 2 "src/geo2d/ang_pp/lib.hpp"

#line 4 "src/geo2d/ang_pp/lib.hpp"

namespace tifa_libs {

// clamp angle of two points, result in $(-\pi,\pi]$
template <class FP>
CEXP FP ang_PP(point<FP> CR p1, point<FP> CR p2) NE { return std::atan2(p1 ^ p2, p1 * p2); }
// clamp angle of two points, result in $[0,2\pi)$
template <class FP>
CEXP FP ang2pi_PP(point<FP> CR p1, point<FP> CR p2) NE {
  const FP res = ang_PP(p1, p2);
  retif_((is_neg(res)), res + 2 * pi_v<FP>, res);
}

}  // namespace tifa_libs
#line 2 "src/geo2d/dot/lib.hpp"

#line 4 "src/geo2d/dot/lib.hpp"

namespace tifa_libs {

template <class P>
CEXP TPN P::FP_t dot(P CR o, P CR a, P CR b) NE { return (a - o) * (b - o); }
template <class P>
CEXP int sgn_dot(P CR o, P CR a, P CR b) NE { return sgn(dot(o, a, b)); }

}  // namespace tifa_libs
#line 7 "src/geo2d/ds/t/lib.hpp"

namespace tifa_libs {

template <class FP>
struct triangle {
  point<FP> A, B, C;

  CEXP triangle() = default;
  CEXP triangle(point<FP> CR a, point<FP> CR b, point<FP> CR c) NE : A(a), B(b), C(c) {}
  CEXP triangle(FP a_x, FP a_y, FP b_x, FP b_y, FP c_x, FP c_y) NE : A(a_x, a_y), B(b_x, b_y), C(c_x, c_y) {}

  friend auto& operator>>(istream_c auto& is, triangle& t) NE { return is >> t.A >> t.B >> t.C; }
  friend auto& operator<<(ostream_c auto& os, triangle CR t) NE { return os << t.A << ' ' << t.B << ' ' << t.C; }
  friend CEXP bool operator==(triangle CR l, triangle CR r) NE { return l.A == r.A && l.B == r.B && l.C == r.C; }
  // (a, b, c)
  CEXP pt3<FP> edges() CNE { return {dist_PP(B, C), dist_PP(C, A), dist_PP(A, B)}; }
  // (A, B, C)
  CEXP pt3<FP> angles() CNE { return {abs(ang_PP(C - A, B - A)), abs(ang_PP(A - B, C - B)), abs(ang_PP(A - C, B - C))}; }
  CEXP point<FP> trilinears(FP x, FP y, FP z) CNE {
    auto [a, b, c] = edges();
    x *= a, y *= b, z *= c;
    return (A * x + B * y + C * z) / (x + y + z);
  }
  CEXP point<FP> barycentrics(FP u, FP v, FP w) CNE { return (A * u + B * v + C * w) / (u + v + w); }
  CEXP FP area() CNE { return abs(cross(A, B, C)) / 2; }
  ND CEXP bool is_acute() CNE { return is_pos(dot(A, B, C)) && is_pos(dot(B, C, A)) && is_pos(dot(C, A, B)); }
  ND CEXP bool is_right() CNE { return is_zero(dot(A, B, C)) || is_zero(dot(B, C, A)) || is_zero(dot(C, A, B)); }
  ND CEXP bool is_obtuse() CNE { return is_neg(dot(A, B, C)) || is_neg(dot(B, C, A)) || is_neg(dot(C, A, B)); }
};

}  // namespace tifa_libs
#line 4 "src/geo2d/tcenter/i/lib.hpp"

namespace tifa_libs {

// radius of inscribed circle
template <class FP>
CEXP FP radius_I(triangle<FP> CR t) NE {
  auto [a, b, c] = t.edges();
  return 2 * t.area() / (a + b + c);
}
// incenter (X1)
template <class FP>
CEXP point<FP> center_I(triangle<FP> CR t) NE { return t.trilinears(1, 1, 1); }

}  // namespace tifa_libs
#line 2 "src/geo2d/tcenter/o/lib.hpp"

#line 5 "src/geo2d/tcenter/o/lib.hpp"

namespace tifa_libs {

// radius of circumscribed circle
template <class FP>
CEXP FP radius_O(triangle<FP> CR t) NE { return dist_PP(t.B, t.C) / std::sin(abs(ang_PP(t.B - t.A, t.C - t.A))) / 2; }
// circumcenter (X3)
template <class FP>
CEXP point<FP> center_O(triangle<FP> CR t) NE {
  // auto [A, B, C] = t.angles();
  // return t.trilinears(std::cos(A), std::cos(B), std::cos(C));
  const point<FP> p1 = mid_point(t.B, t.C), p2 = mid_point(t.C, t.A);
  return ins_LL<FP>({p1, p1 + (t.B - t.C).do_rot90()}, {p2, p2 + (t.C - t.A).do_rot90()});
}

}  // namespace tifa_libs
#line 9 "test/cpv_local/geo2d/make_c.uva12304.cpp"

using namespace tifa_libs;
using std::cin, std::cout;
using data_t = f64;
using Point2 = point<data_t>;
using Line2 = line<data_t>;
using Triangle = triangle<data_t>;
using Circle2 = circle<data_t>;

CEXP data_t PI = pi_v<data_t>;

int main() {
  cout << std::fixed << std::setprecision(6);

  strn s;
  while (cin >> s) {
    if (s == "CircumscribedCircle") {
      Triangle t;
      cin >> t;
      Point2 o = center_O(t);
      data_t r = radius_O(t);
      cout << '(' << o.x << ',' << o.y << ',' << r << ")\n";
    } else if (s == "InscribedCircle") {
      Triangle t;
      cin >> t;
      Point2 i = center_I(t);
      data_t r = radius_I(t);
      cout << '(' << i.x << ',' << i.y << ',' << r << ")\n";
    } else if (s == "TangentLineThroughPoint") {
      Circle2 c;
      Point2 p;
      cin >> c >> p;
      auto tps = tan_CP(c, p);
      if (!tps) {
        cout << "[]\n";
      } else {
        auto [tps0, tps1] = tps.value();
        Point2 d1 = Line2{p, tps0}.direction(), d2 = Line2{p, tps1}.direction();
        data_t angle1 = atan2(d1.y, d1.x) / PI * 180;
        data_t angle2 = atan2(d2.y, d2.x) / PI * 180;
        if (angle1 < 0) angle1 += 180;
        if (angle1 >= 180) angle1 -= 180;
        if (angle2 < 0) angle2 += 180;
        if (angle2 >= 180) angle2 -= 180;
        if (is_eq(angle1, angle2)) {
          cout << '[' << angle1 << "]\n";
        } else {
          if (angle1 > angle2) std::swap(angle1, angle2);
          cout << '[' << angle1 << ',' << angle2 << "]\n";
        }
      }
    } else if (s == "CircleThroughAPointAndTangentToALineWithRadius") {
      Point2 p;
      Line2 l;
      data_t r;
      cin >> p >> l >> r;
      auto cs = make_C_rPL(r, p, l);
      if (!cs) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1] = cs.value();
        if (cs1.o < cs0.o) std::swap(cs0, cs1);
        if (cs0.o == cs1.o) {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << ")]\n";
        } else {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << "),(" << cs1.o.x << ',' << cs1.o.y << ")]\n";
        }
      }
    } else if (s == "CircleTangentToTwoLinesWithRadius") {
      Line2 l1, l2;
      data_t r;
      cin >> l1 >> l2 >> r;
      auto cs_ = make_C_rLL(r, l1, l2);
      if (!cs_) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1, cs2, cs3] = cs_.value();
        vec<Circle2> cs{cs0, cs1, cs2, cs3};
        std::ranges::sort(cs, [](Circle2 CR lhs, Circle2 CR rhs) { return lhs.o < rhs.o; });
        cout << "[(" << cs[0].o.x << ',' << cs[0].o.y << "),(" << cs[1].o.x << ',' << cs[1].o.y << "),(" << cs[2].o.x << ',' << cs[2].o.y << "),(" << cs[3].o.x << ',' << cs[3].o.y << ")]\n";
      }
    } else if (s == "CircleTangentToTwoDisjointCirclesWithRadius") {
      Circle2 c1, c2;
      data_t r;
      cin >> c1 >> c2 >> r;
      auto cs = make_C_rCC_ex(r, c1, c2);
      if (!cs) {
        cout << "[]\n";
      } else {
        auto [cs0, cs1] = cs.value();
        if (cs1.o < cs0.o) std::swap(cs0, cs1);
        if (cs0.o == cs1.o) {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << ")]\n";
        } else {
          cout << "[(" << cs0.o.x << ',' << cs0.o.y << "),(" << cs1.o.x << ',' << cs1.o.y << ")]\n";
        }
      }
    }
  }
}

Test cases

Env Name Status Elapsed Memory
verify-g++ 1 :heavy_check_mark: AC 9 ms 25 MB
verify-g++ 2 :heavy_check_mark: AC 15 ms 24 MB
coverage-g++ 1 :heavy_check_mark: AC 2 ms 4 MB
coverage-g++ 2 :heavy_check_mark: AC 6 ms 4 MB
Back to top page