luckYrat's library.

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


:heavy_check_mark: cpp/z_test/aoj-CGL_1_A.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/1/CGL_1_A"
#define ERROR 1e-8

#include "../../cpp/template/template.cpp"

#include "../../cpp/geometry/projection.cpp"

int main(){
  Point p1,p2;cin>>p1>>p2;
  Line l(p1, p2);
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    Point q;cin>>q;
    cout << fixed << setprecision(10) << projection(l,q) << endl;
  }
}
#line 1 "cpp/z_test/aoj-CGL_1_A.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/1/CGL_1_A"
#define ERROR 1e-8

#line 2 "cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 5 "cpp/z_test/aoj-CGL_1_A.test.cpp"

#line 2 "cpp/geometry/vec.hpp"

struct Vec{
  long double x,y;
  Vec operator*(long double a){
    return {x*a, y*a};
  }
  Vec operator/(long double a){
    return {x/a, y/a};
  }

  friend ostream &operator<<(ostream &os, const Vec &p){
    return os << p.x << " " << p.y;
  }
};

long double magnitude(Vec v){
  return sqrt(v.x*v.x+v.y*v.y);
}

Vec standardize(Vec v){
  return v/magnitude(v);
}
#line 3 "cpp/geometry/point.hpp"

struct Point{
  long double x,y;

  Vec operator-(Point b){
    return {x-b.x, y-b.y};
  }
  Point operator+(long double a){
    return {x+a, y+a};
  }
  Point operator+(Vec v){
    return {x+v.x, y+v.y};
  }

  friend istream &operator>>(istream &is, Point &p){
    long double a,b;
    cin>>a>>b;
    p = Point({a,b});
    return (is);
  }

  friend ostream &operator<<(ostream &os, const Point &p){
    return os << p.x << " " << p.y;
  }

};
#line 2 "cpp/geometry/line-segment.hpp"

#line 4 "cpp/geometry/line-segment.hpp"

struct LineSegment{
  Point first, second;
  LineSegment(Point a, Point b):first(a),second(b){}
};


#line 5 "cpp/geometry/linevec.hpp"

struct LineVec{
  Point base;
  Vec vec;
  LineVec(LineSegment ls){
    base = ls.first;
    vec = ls.second - ls.first;
  }
};
#line 5 "cpp/geometry/line.hpp"

struct Line{
  //ax+by+c=0 (required: a*a+b*b != 0)
  long double a,b,c;
  Line(long double a, long double b, long double c): a(a), b(b), c(c){}
  Line(LineVec lv){
    a = -lv.vec.y;
    b = lv.vec.x;
    c = -(a*lv.base.x + b*lv.base.y);
  }

  Line operator/(long double v){
    return {a/v, b/v, c/v};
  }

  Line(LineSegment ls){
    (*this) = (LineVec)(ls);
  }
  Line(Point a, Point b){
    LineSegment ls({a,b});
    (*this) = (LineSegment)(ls);
  }
};

Line standardize(Line l){
  return l/sqrt(l.a*l.a+l.b*l.b);
}
long double distance(Line l, Point p){
  Line nl = standardize(l);
  return nl.a*p.x + nl.b*p.y + nl.c;
}
Vec normal(Line l){
  Vec v = {-l.a, -l.b};
  return standardize(v);
}

#line 5 "cpp/geometry/projection.cpp"

long double projectionDistance(Line l, Point p){
  Line nl = standardize(l);
  return distance(l, p);
}

Point projection(Line l, Point p){
  long double dist = projectionDistance(l, p);
  Vec v = normal(l);
  return p+(v*dist);
}
#line 7 "cpp/z_test/aoj-CGL_1_A.test.cpp"

int main(){
  Point p1,p2;cin>>p1>>p2;
  Line l(p1, p2);
  int q;cin>>q;
  for(int i = 0; q > i; i++){
    Point q;cin>>q;
    cout << fixed << setprecision(10) << projection(l,q) << endl;
  }
}

Test cases

Env Name Status Elapsed Memory
g++ 00_int_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 00_int_01.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_int_02.in :heavy_check_mark: AC 6 ms 4 MB
g++ 00_int_03.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_int_04.in :heavy_check_mark: AC 5 ms 4 MB
g++ 00_int_05.in :heavy_check_mark: AC 5 ms 4 MB
g++ 01_real_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 01_real_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 02_online_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 02_online_01.in :heavy_check_mark: AC 6 ms 4 MB
g++ 02_online_02.in :heavy_check_mark: AC 6 ms 4 MB
g++ 02_online_03.in :heavy_check_mark: AC 6 ms 4 MB
g++ 03_horizontal_00.in :heavy_check_mark: AC 6 ms 4 MB
g++ 03_horizontal_01.in :heavy_check_mark: AC 7 ms 4 MB
g++ 04_vertical_00.in :heavy_check_mark: AC 7 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 7 ms 4 MB
g++ 05_rand_01.in :heavy_check_mark: AC 7 ms 4 MB
g++ 05_rand_02.in :heavy_check_mark: AC 7 ms 4 MB
g++ 05_rand_03.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_04.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_05.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_06.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_07.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_08.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_09.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_10.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_11.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_12.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_13.in :heavy_check_mark: AC 11 ms 4 MB
g++ 05_rand_14.in :heavy_check_mark: AC 12 ms 4 MB
g++ 05_rand_15.in :heavy_check_mark: AC 11 ms 4 MB
Back to top page