From ff6b9bb3bf164b2474e72b35ea02ae87e48aa226 Mon Sep 17 00:00:00 2001 From: TurnMeOn Date: Tue, 19 Dec 2017 23:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Path=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureProject.pro | 10 ++++++---- cars.cpp => car.cpp | 24 ++++++++++++------------ cars.h => car.h | 10 +++++----- carmananger.h | 5 ++++- mainwindow.cpp | 2 +- parkingLotItem.h | 6 +++--- path.cpp | 38 ++++++++++++++++++++++++++++++++++++++ path.h | 31 +++++++++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 26 deletions(-) rename cars.cpp => car.cpp (73%) rename cars.h => car.h (77%) create mode 100644 path.cpp create mode 100644 path.h diff --git a/DataStructureProject.pro b/DataStructureProject.pro index dfb149b..489678b 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -27,25 +27,27 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ - cars.cpp \ ParkingLotWidget.cpp \ Road.cpp \ ParkingSpaceWidget.cpp \ monitor.cpp \ boundary.cpp \ carmananger.cpp \ - parkingLotItem.cpp + parkingLotItem.cpp \ + car.cpp \ + path.cpp HEADERS += \ mainwindow.h \ - cars.h \ ParkingLotWidget.h \ Road.h \ ParkingSpaceWidget.h \ monitor.h \ boundary.h \ carmananger.h \ - parkingLotItem.h + parkingLotItem.h \ + car.h \ + path.h FORMS += \ mainwindow.ui \ diff --git a/cars.cpp b/car.cpp similarity index 73% rename from cars.cpp rename to car.cpp index eb7893d..a09ab2d 100644 --- a/cars.cpp +++ b/car.cpp @@ -1,12 +1,12 @@ -#include "cars.h" +#include "car.h" -Cars::Cars(QGraphicsItem *parent, int dir, Cars::Color color) : QGraphicsPixmapItem(parent) +Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : QGraphicsPixmapItem(parent) { m_pos = this->pos(); this->setRotation(dir); if(color==RANDOM) - color = Cars::Color(rand()%3); //车身颜色一定是确定的 + color = Car::Color(rand()%3); //车身颜色一定是确定的 m_color = color; switch (m_color) { case Pink: @@ -27,56 +27,56 @@ Cars::Cars(QGraphicsItem *parent, int dir, Cars::Color color) : QGraphicsPixmapI setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心 } -void Cars::Forward(qreal vel) +void Car::Forward(qreal vel) { qreal ang = qDegreesToRadians(rotation()); this->moveBy(+vel*qSin(ang), -vel*qCos(ang)); } -void Cars::Backward(qreal vel) +void Car::Backward(qreal vel) { qreal ang = qDegreesToRadians(rotation()); this->moveBy(-vel*qSin(ang), +vel*qCos(ang)); } -void Cars::moveLeft(qreal vel) +void Car::moveLeft(qreal vel) { qreal ang = qDegreesToRadians(rotation()); moveBy(-vel*qCos(ang), -vel*qSin(ang)); } -void Cars::moveRight(qreal vel) +void Car::moveRight(qreal vel) { qreal ang = qDegreesToRadians(rotation()); moveBy(vel*qCos(ang), vel*qSin(ang)); } //ang为旋转角度(弧度),正数为顺时针,负数逆时针 -void Cars::Rotate(qreal ang) +void Car::Rotate(qreal ang) { setRotation(rotation() + ang); } -void Cars::turnLeft(int r, int ang) +void Car::turnLeft(int r, int ang) { Forward(r*qSin(ang)); Rotate(-ang); moveLeft(r*(1-qCos(ang))); } -void Cars::turnRight(int r, int ang) +void Car::turnRight(int r, int ang) { Forward(r*qSin(ang)); Rotate(ang); moveRight(r*(1-qCos(ang))); } -Cars::Color Cars::getColor() +Car::Color Car::getColor() { return this->m_color; } -qreal Cars::getDir() +qreal Car::getDir() { return this->rotation(); } diff --git a/cars.h b/car.h similarity index 77% rename from cars.h rename to car.h index b861d90..d5bc5b6 100644 --- a/cars.h +++ b/car.h @@ -1,5 +1,5 @@ -#ifndef CARS_H -#define CARS_H +#ifndef CAR_H +#define CAR_H #include #include @@ -11,11 +11,11 @@ #define M_LEN (200) #define M_WID (130) -class Cars : public QGraphicsPixmapItem +class Car : public QGraphicsPixmapItem { public: enum Color { Pink, Red, Yellow, RANDOM }; - explicit Cars(QGraphicsItem *parent = Q_NULLPTR, int dir = 0, Cars::Color color = RANDOM); + explicit Car(QGraphicsItem *parent = Q_NULLPTR, int dir = 0, Car::Color color = RANDOM); void Forward(qreal vel = 3); void Backward(qreal vel = 3); void moveLeft(qreal vel = 3); @@ -35,4 +35,4 @@ private: }; -#endif // CARS_H +#endif // CAR_H diff --git a/carmananger.h b/carmananger.h index 57c8cd1..54da76b 100644 --- a/carmananger.h +++ b/carmananger.h @@ -1,6 +1,9 @@ #ifndef CARMANANGER_H #define CARMANANGER_H +#include "car.h" +#include "path.h" + class CarMananger { @@ -8,4 +11,4 @@ public: CarMananger(); }; -#endif // CARMANANGER_H \ No newline at end of file +#endif // CARMANANGER_H diff --git a/mainwindow.cpp b/mainwindow.cpp index ba56f98..d568e56 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,6 +1,6 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "cars.h" +#include "car.h" #include #include #include diff --git a/parkingLotItem.h b/parkingLotItem.h index a51bbe9..4d45cf7 100644 --- a/parkingLotItem.h +++ b/parkingLotItem.h @@ -5,7 +5,7 @@ #include #include #include -#include "cars.h" +#include "car.h" class ParkingLotItem: public QGraphicsPixmapItem { @@ -16,8 +16,8 @@ private: QVector m_pixmap; // 每一层的pixmap QVector m_max_capacity; // 每一层的容量 QVector m_num_of_cars; // 每一层当前停的车数量 - QVector > m_cars; // 停在每一层的车 - QVector >m_waitting; // 在每个入口等待的车 + QVector > m_cars; // 停在每一层的车 + QVector >m_waitting; // 在每个入口等待的车 QStringList m_xml_path; // 每一层xml文件地址 QStringList m_name; // 每一层的名字 const uint m_num_of_layer = 2; // 总层数 diff --git a/path.cpp b/path.cpp new file mode 100644 index 0000000..6115d61 --- /dev/null +++ b/path.cpp @@ -0,0 +1,38 @@ +#include "path.h" + +Path::Path() +{ + this->path = QList(); +} + +//用PathPoint数组初始化Path +Path::Path(PathPoint points[], int i) +{ + for(int j=0; j +#include + +//路径上的点和车在该点的角度 +struct PathPoint +{ + QPointF point; + qreal dir; +}; + +class Path +{ +public: + + Path(); + Path(PathPoint points[], int i); + PathPoint getPoint(int i); + void addPoint(int i, const PathPoint &point); + void addPoint(const PathPoint &point); + bool isEmpty(); + int pointsCount(); + +private: + QList path; + int len; +}; + +#endif // PATH_H