]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/commitdiff
Add drive in slot method
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Thu, 15 Jul 2021 11:04:01 +0000 (13:04 +0200)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Thu, 15 Jul 2021 14:17:37 +0000 (16:17 +0200)
incl/pslot.hh
src/pslot.cc

index b9d4ff52dda6db1e55fe82312997f08d24ec04bb..b970cbacdbe21667fa8fc87fcfc604ed56772920 100644 (file)
@@ -3,6 +3,7 @@
 #define BCAR_PSLOT_H
 
 #include <ostream>
+#include <vector>
 #include "bcar.hh"
 
 namespace bcar {
@@ -80,6 +81,13 @@ public:
        /*! Return `true` if `c`'s car frame collide with `this` border. */
        bool collide(BicycleCar const& c) const;
 
+       /*! \brief Drive car `c` into the parking slot `this`.
+        *
+        * \param c Starting bicycle car.
+        * \param max Maximum number of backward-forward direction changes.
+        */
+       std::vector<BicycleCar> drive_in_slot(BicycleCar c, unsigned int& max);
+
        friend std::ostream& operator<<(std::ostream& o, ParkingSlot const& s);
 };
 
index 5c17a0c4e24b6fb4c4c78950bf87f5b86fdd4160..aff8850ded9ab3245b1da4607ee2813593b3f505 100644 (file)
@@ -140,6 +140,40 @@ ParkingSlot::collide(BicycleCar const& c) const
                && c.front().intersects_with(this->front_);
 }
 
+std::vector<BicycleCar>
+ParkingSlot::drive_in_slot(BicycleCar c, unsigned int& max)
+{
+       assert(this->parallel());
+       assert(this->right());
+       assert(c.len() < this->len());
+       assert(c.w() < this->w());
+       std::vector<BicycleCar> path;
+       path.reserve(max + 2);
+       path.push_back(c);
+       unsigned int cusp = 0;
+       while (cusp < max) {
+               if (c.h() < this->h()) {
+                       return std::vector<BicycleCar>();
+               }
+               if (this->parked(c)) {
+                       if (cusp < max) {
+                               max = cusp;
+                       }
+                       break;
+               }
+               c.next();
+               if (this->collide(c)) {
+                       c.sp(c.sp() * -1.0);
+                       c.next();
+                       path.push_back(c);
+                       c.st(c.st() * -1.0);
+                       cusp += 1;
+               }
+       }
+       path.push_back(c);
+       return path;
+}
+
 std::ostream&
 operator<<(std::ostream& o, ParkingSlot const& s)
 {