📚Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View on GitHub

:heavy_check_mark: library/test/aoj/CGL_2_C-Cross_Point.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/CGL_2_C"
#include <bits/stdc++.h>
using namespace std;
#include "../../geometry/2dSegmentAndLine.hpp"
#define ERROR 0.00000001

int main() {
    ios_base::sync_with_stdio(0);
    int q;
    cin>>q;
    while(q--) {
        Point p0, p1, p2, p3;
        cin>>p0>>p1>>p2>>p3;
        Line l1 = Line(p0, p1);
        Line l2 = Line(p2, p3);

        Point p = cross_point_ll(l1, l2);

        cout<<p<<'\n';
    }
    return 0;
}
#line 1 "library/test/aoj/CGL_2_C-Cross_Point.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/CGL_2_C"
#include <bits/stdc++.h>
using namespace std;
#line 2 "library/geometry/2dGeometryTemplate.hpp"

using Real = double;
using Point = complex<Real>;
using Polygon = vector<Point>;
const Real EPS = 1e-8, PI = acos(-1);

Point operator*(const Point& p, const Real& d) {
    return Point(p.real() * d, p.imag() * d);
}

Point operator/(const Point& p, const Real& d) {
    return Point(p.real() / d, p.imag() / d);
}

istream& operator>>(istream& is, Point& p) {
    Real a, b;
    is >> a >> b;
    p = Point(a, b);
    return is;
}

ostream& operator<<(ostream& os, const Point& p) {
    return os << fixed << setprecision(20) << p.real() << " " << p.imag();
}

int sign(const Real& r) {
    if (r <= -EPS) return -1;
    if (r >= +EPS) return +1;
    return 0;
}

bool equals(const Real& a, const Real& b) {
    return sign(a - b) == 0;
}

namespace std {
bool operator<(const Point& a, const Point& b) {
    if (equals(a.real(), b.real())) return a.imag() < b.imag();
    return a.real() < b.real();
}
}  // namespace std

Real dot(const Point& a, const Point& b) {
    return (conj(a) * b).real();
}

Real cross(const Point& a, const Point& b) {
    return (conj(a) * b).imag();
}

struct Line {
    Point a, b;
    Line() = default;
    Line(Point a, Point b) : a(a), b(b) {}
};
using Segment = Line;
#line 3 "library/geometry/2dPointAndVector.hpp"

Point projection(const Line& l, const Point& p) {
    return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
}

Point reflection(const Line& l, const Point& p) {
    return p + (projection(l, p) - p) * 2.0;
}

int ccw(const Point& a, Point b, Point c) {
    b -= a, c -= a;
    if (sign(cross(b, c)) == +1) return +1; // COUNTER_CLOCKWISE
    if (sign(cross(b, c)) == -1) return -1;// CLOCKWISE
    if (sign(dot(b, c)) == -1) return +2; // ONLINE_BACK
    if (norm(b) < norm(c)) return -2; // ONLINE_FRONT
    return 0; // ON_SEGMENT
}
#line 2 "library/geometry/2dSegmentAndLine.hpp"

bool is_orthogonal(const Line& a, const Line& b) {
    return equals(dot(a.b - a.a, b.b - b.a), 0);
}

bool is_parallel(const Line& a, const Line& b) {
    return equals(cross(a.b - a.a, b.b - b.a), 0);
}

bool is_intersect_ss(const Segment& s, const Segment& t) {
    return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
           ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

Point cross_point_ll(const Line& l, const Line& m) {
    Real A = cross(l.b - l.a, m.b - m.a);
    Real B = cross(l.b - l.a, l.b - m.a);
    if (equals(abs(A), 0) && equals(abs(B), 0)) return m.a;
    return m.a + (m.b - m.a) * B / A;
}

Real distance_sp(const Segment& s, const Point& p) {
    Point r = projection(s, p);
    if (ccw(s.a, s.b, r) == 0) return abs(r - p);
    return min(abs(s.a - p), abs(s.b - p));
}

Real distance_ss(const Segment& a, const Segment& b) {
    if (is_intersect_ss(a, b)) return 0;
    return min({distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b)});
}
#line 5 "library/test/aoj/CGL_2_C-Cross_Point.test.cpp"
#define ERROR 0.00000001

int main() {
    ios_base::sync_with_stdio(0);
    int q;
    cin>>q;
    while(q--) {
        Point p0, p1, p2, p3;
        cin>>p0>>p1>>p2>>p3;
        Line l1 = Line(p0, p1);
        Line l2 = Line(p2, p3);

        Point p = cross_point_ll(l1, l2);

        cout<<p<<'\n';
    }
    return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_small_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 00_small_01.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_small_02.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_small_03.in :heavy_check_mark: AC 5 ms 4 MB
g++ 01_onsegment_00.in :heavy_check_mark: AC 5 ms 4 MB
g++ 01_onsegment_01.in :heavy_check_mark: AC 5 ms 4 MB
g++ 02_extreme_00.in :heavy_check_mark: AC 5 ms 4 MB
g++ 02_extreme_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 03_horizontal_00.in :heavy_check_mark: AC 5 ms 4 MB
g++ 03_horizontal_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 04_vertical_00.in :heavy_check_mark: AC 5 ms 4 MB
g++ 04_vertical_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 05_rand_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 05_rand_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 05_rand_02.in :heavy_check_mark: AC 6 ms 4 MB
g++ 05_rand_03.in :heavy_check_mark: AC 6 ms 4 MB
g++ 06_rand_00.in :heavy_check_mark: AC 12 ms 4 MB
g++ 06_rand_01.in :heavy_check_mark: AC 9 ms 4 MB
g++ 06_rand_02.in :heavy_check_mark: AC 13 ms 4 MB
g++ 06_rand_03.in :heavy_check_mark: AC 9 ms 4 MB
Back to top page