Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include "Geometry.hpp"
  2. #include "World.hpp"
  3.  
  4. #include <cmath>
  5. #include <stdlib.h>
  6.  
  7. namespace world {
  8.  
  9. constexpr double eps = 1e-5;
  10.  
  11. //Point
  12.  
  13. Point::Point() {
  14.     x_ = abs(rand()) % X_FIELD_SIZE;
  15.     y_ = abs(rand()) % Y_FIELD_SIZE;
  16. }
  17.  
  18. Point::Point(const Point &other) : x_(other.x_), y_(other.y_) {}
  19.  
  20. Point &Point::operator=(const Point &other) {
  21.     x_ = other.x_;
  22.     y_ = other.y_;
  23.     return *this;
  24. }
  25.  
  26. Point &Point::operator+=(const Vector &v) {
  27.     x_ += v.x_;
  28.     y_ += v.y_;
  29.     return *this;
  30. }
  31.  
  32.  
  33. bool Point::operator==(const Point &point) {
  34.     return (abs(x_ - point.x_) < eps && abs(y_ - point.y_) < eps);
  35. }
  36.  
  37. Point Point::operator+(const Vector &v) const {
  38.     Point cur(*this);
  39.     cur += v;
  40.     return cur;
  41. }
  42.  
  43. Vector Point::operator-(const Point &other) const {
  44.     Vector cur;
  45.     cur.x_ = x_ - other.x_;
  46.     cur.y_ = y_ - other.y_;
  47.     return cur;
  48. }
  49.  
  50. //Point
  51. //Vector
  52.  
  53.  
  54. Vector &Vector::operator*(const int val) {
  55.     x_*= val;
  56.     y_ *= val;
  57.     return *this;
  58. }
  59.  
  60. Vector &Vector::operator*=(const int val) {
  61.     *this = *this * val;
  62.     return *this;
  63. }
  64.  
  65. Vector &Vector::operator+=(const Vector &v) {
  66.     x_ += v.x_;
  67.     y_ += v.y_;
  68.     return *this;
  69. }
  70.  
  71. //Vector
  72. //Segment
  73. Segment::Segment(const Point &a, const Point &b) : a_(a), b_(b) {}
  74.  
  75. //Segment
  76.  
  77. //Polygon
  78.  
  79. bool point_in_polygon(const Point &point, const Polygon &polygon) { //TODO
  80.     return false;
  81. }
  82.  
  83. std::vector<Point> &convex_hull(std::vector<Point> &v) { //TODO
  84.     return v;
  85. }
  86.  
  87.  
  88. Polygon::Polygon(const World &world) {
  89.     size_ = abs(rand()) % 10 + 3;
  90.     for (size_t i = 0; i < size_; ++i) {
  91.         auto cur = Point();
  92.         for (auto polygon : world.obstaclesArray_) {
  93.             if (!point_in_polygon(cur, polygon)) {
  94.                 coord_.push_back(cur);
  95.             }
  96.         }
  97.     }
  98.     coord_ = convex_hull(coord_);
  99.     size_ = coord_.size();
  100.     size_t ind = 0;
  101.     while(ind < world.obstaclesArray_.size()) {
  102.         if (point_in_polygon(world.obstaclesArray_[ind].coord_[0], *this)) {
  103.             swap(world.obstaclesArray_[ind], world.obstaclesArray_.back());
  104.             world.obstaclesArray_.pop_back();
  105.         } else {
  106.             ++ind;
  107.         }
  108.     }
  109.     world.obstaclesArray_.push_back(*this);
  110. }
  111.  
  112. void swap(Point &a, Point &b) {
  113.     std::swap(a.x_, b.x_);
  114.     std::swap(a.y_, b.y_);
  115. }
  116.  
  117. void swap(Vector &a, Vector &b) {
  118.     std::swap(a.x_, b.x_);
  119.     std::swap(a.y_, b.y_);
  120. }
  121.  
  122. } //namespace world
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement