zawatins-library

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

View the Project on GitHub zawa-tin/zawatins-library

:heavy_check_mark: test/projection.test.cpp

Depends on

Code

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

#include "../src/geometryR2/projection.hpp"

#include <iostream>
#include <iomanip>

int main() {
	using namespace geoR2;
	point p1, p2; std::cin >> p1 >> p2;
	// std::cout << p1 << '\n' << p2 << std::endl;
	const line l(p1, p2);
	// std::cout << l.a << '\n' << l.b << std::endl;
	int q; std::cin >> q;
	std::cout << std::fixed << std::setprecision(10);
	for (int _ = 0 ; _ < q ; _++) {
		point p0; std::cin >> p0;
		std::cout << projection(p0, l) << std::endl;
	}
}
#line 1 "test/projection.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/1/CGL_1_A"
#define ERROR 0.00000001

#line 2 "src/geometryR2/projection.hpp"

#line 2 "src/geometryR2/line.hpp"

#line 2 "src/geometryR2/point.hpp"

#line 2 "src/geometryR2/base.hpp"

#include <cmath>

namespace geoR2 {

using real = long double;

const real PI = acosl(-1);

inline real &eps() {
	static real EPS = 1e-14;
	return EPS;
}

inline void setEps(const real& EPS = 1e-14) {
	eps() = EPS;
}

inline int sgn(const real& value) {
	return (value < -eps() ? -1 : (value > +eps() ? 1 : 0));
}

inline bool equals(const real& a, const real& b) {
	return sgn(a - b) == 0;
}

inline real toRadian(const real& value) {
	return (value * PI) / 180;
}

} // namespace geoR2

namespace literals {

geoR2::real operator"" _rad(unsigned long long value) {
	return (static_cast<geoR2::real>(value) * geoR2::PI) / static_cast<geoR2::real>(180);
}

} // namespace literals

using namespace literals;
#line 4 "src/geometryR2/point.hpp"

#line 6 "src/geometryR2/point.hpp"
#include <istream>
#include <ostream>

namespace geoR2 {

struct point {
	real x, y;
	
	point() : x(0), y(0) {}
	point(const real& _x, const real& _y) : x(_x), y(_y) {}

	friend std::ostream &operator <<(std::ostream& os, const point& p) {
		return os << p.x << ' ' << p.y;
	}
	friend std::istream &operator >>(std::istream& is, point& p) {
		is >> p.x >> p.y;
		return is;
	}
	
	point &operator +=(const point& p) {
		x += p.x; y += p.y;
		return *this;
	}
	point &operator -=(const point& p) {
		x -= p.x; y -= p.y;
		return *this;
	}
	point &operator *=(const real& k) {
		x *= k; y *= k;
		return *this;
	}
	point &operator /=(const real& k) {
		x /= k; y /= k;
		return *this;
	}

	point operator +(const point& p) const {
		return point(*this) += p;	
	}
	point operator -(const point& p) const {
		return point(*this) -= p;
	}
	point operator *(const real& k) const {
		return point(*this) *= k;
	}
	friend point operator *(const real& r, const point& p) {
		return point(p) *= r;
	}
	point operator /(const real& k) const {
		return point(*this) /= k;
	}
	point operator +() const {
		return point(*this);
	}
	point operator -() const {
		return point(-x, -y);
	}

	real squareDistance() const {
		return x * x + y * y;
	}

	real distance() const {
		return sqrtl(x * x + y + y);
	}

	point rotated(const real& theta) const {
		return point(x * cosl(theta) - y * sinl(theta), x * sinl(theta) + y * cosl(theta));
	}
};

real dot(const point& a, const point& b) {
	return a.x * b.x + a.y * b.y;
}

real cross(const point& a, const point& b) {
	return a.x * b.y - a.y * b.x;
}

bool equals(const point& a, const point& b) {
	return equals(a.x, b.x) and equals(a.y, b.y);
}

using vec2 = point;

} // namespace geoR2
#line 4 "src/geometryR2/line.hpp"

#include <cassert>

namespace geoR2 {

struct line {
	point a, b;

	line() : a(0, 0), b(0, 0) {}
	line(const point& _a, const point& _b) : a(_a), b(_b) {}

	inline bool isValid() const {
		return !equals(a, b);
	}

};

} // namespace geo2d
#line 5 "src/geometryR2/projection.hpp"

#line 7 "src/geometryR2/projection.hpp"

namespace geoR2 {

point projection(const point& p, const line& l) {
	assert(l.isValid());
	real coeff = dot(l.b - l.a, p - l.a) / vec2(l.b - l.a).squareDistance();
	return coeff * l.b + (static_cast<real>(1) - coeff) * l.a;
}

} // namespace geoR2
#line 5 "test/projection.test.cpp"

#include <iostream>
#include <iomanip>

int main() {
	using namespace geoR2;
	point p1, p2; std::cin >> p1 >> p2;
	// std::cout << p1 << '\n' << p2 << std::endl;
	const line l(p1, p2);
	// std::cout << l.a << '\n' << l.b << std::endl;
	int q; std::cin >> q;
	std::cout << std::fixed << std::setprecision(10);
	for (int _ = 0 ; _ < q ; _++) {
		point p0; std::cin >> p0;
		std::cout << projection(p0, l) << std::endl;
	}
}
Back to top page