Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- const double EPS = 1e-9;
- struct Point {
- double x;
- double y;
- Point() = default;
- Point(double x_, double y_) : x(x_), y(y_) {}
- Point(const Point& p1, const Point& p2) {
- x = p2.x - p1.x;
- y = p2.y - p1.y;
- }
- double length() const {
- return hypot(x, y);
- }
- double distanceTo(const Point& p2) const {
- return hypot(x - p2.x, y - p2.y);
- }
- Point operator+(const Point& p2) const {
- return Point(x + p2.x, y + p2.y);
- }
- Point operator-(const Point& p2) const {
- return Point(x - p2.x, y - p2.y);
- }
- Point operator*(const double& scalar) const {
- return Point(x * scalar, y * scalar);
- }
- bool operator==(const Point& p2) const {
- return EPS > abs(x - p2.x) && EPS > abs(y - p2.y);
- }
- bool operator<(const Point& p2) const {
- if (EPS > abs(x - p2.x)) {
- return y < p2.y;
- } else {
- return x < p2.x;
- }
- }
- double DotProduct(const Point& p2) const {
- return x * p2.x + y * p2.y;
- }
- double AngleBetween(const Point& p2) const {
- return acos(DotProduct(p2) / length() / p2.length());
- }
- bool isOrthogonal(const Point& p2) const {
- return EPS > abs(DotProduct(p2));
- }
- double CrossProduct(const Point& p2) const {
- return x * p2.y - p2.x * y;
- }
- bool isCollinear(const Point& p2) const {
- return EPS > abs(CrossProduct(p2));
- }
- };
- istream& operator>>(istream& input, Point& point) {
- input >> point.x >> point.y;
- return input;
- }
- ostream& operator<<(ostream& output, const Point& point) {
- output << '(' << point.x << ", " << point.y << ")";
- return output;
- }
- struct Polygon {
- vector<Point> points;
- void add(const Point& p) {
- points.push_back(p);
- }
- double perimeter () const {
- double sum = 0;
- for (int i = 0; i < points.size(); ++i) {
- sum += points[i].distanceTo(points[(i + 1) % points.size()]);
- }
- return sum;
- }
- double area() const {
- double sum = 0;
- for (int i = 0; i < points.size(); ++i) {
- sum += points[i].CrossProduct(points[(i + 1) % points.size()]);
- }
- return abs(sum / 2);
- }
- bool contains(const Point& p) {
- bool plus = false, minus = false;
- for (int i = 0; i < points.size(); ++i) {
- Point &a = points[i], &b = points[(i + 1) % points.size()];
- Point ab (a, b), ap (a, p);
- double prod = ab.CrossProduct(ap);
- if (prod < -EPS) {
- minus = true;
- } else if (prod > EPS) {
- plus = true;
- }
- }
- return !(minus & plus);
- }
- };
- int main() {
- // int n;
- // cin >> n;
- Polygon _p{{{2, 2}, {2, 0}, {0, 0}, {0, 2}}};
- Point other{2, 2};
- cout << _p.contains(other);
- // for (int i = 0; i < n; ++i) {
- // Point p;
- // cin >> p;
- // _p.add(p);
- // }
- // cout << fixed << _p.area();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement