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/car.cpp b/car.cpp new file mode 100644 index 0000000..6f198a4 --- /dev/null +++ b/car.cpp @@ -0,0 +1,86 @@ +#include "car.h" + + +Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : QGraphicsPixmapItem(parent) +{ + m_pos = this->pos(); + this->setRotation(dir); + if(color==RANDOM) + color = Car::Color(rand()%3); //车身颜色一定是确定的 + m_color = color; + switch (m_color) { + case Pink: + m_pic = QPixmap(":/cars/pink"); + break; + case Red: + m_pic = QPixmap(":/cars/red"); + break; + case Yellow: + m_pic = QPixmap(":/cars/yellow"); + break; + default: + m_pic = QPixmap(":/cars/red"); + break; + } + m_pic = m_pic.scaled(M_WID, M_LEN); + this->setPixmap(m_pic); + setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心 +} + +void Car::Forward(qreal vel) +{ + qreal ang = qDegreesToRadians(rotation()); + this->moveBy(+vel*qSin(ang), -vel*qCos(ang)); +} + +void Car::Backward(qreal vel) +{ + qreal ang = qDegreesToRadians(rotation()); + this->moveBy(-vel*qSin(ang), +vel*qCos(ang)); +} + +void Car::moveLeft(qreal vel) +{ + qreal ang = qDegreesToRadians(rotation()); + moveBy(-vel*qCos(ang), -vel*qSin(ang)); +} + +void Car::moveRight(qreal vel) +{ + qreal ang = qDegreesToRadians(rotation()); + moveBy(vel*qCos(ang), vel*qSin(ang)); +} + +//ang为旋转角度(弧度),正数为顺时针,负数逆时针 +void Car::Rotate(qreal ang) +{ + setRotation(rotation() + ang); +} + +//r为弯道半径,ang为转过的角度 +void Car::turnLeft(int r, int ang) +{ + qreal rad = qDegreesToRadians(ang); + Forward(r*qSin(rad)); + moveLeft(r*(1-qCos(rad))); + Rotate(-ang); +} + +//r为弯道半径,ang为转过的角度 +void Car::turnRight(int r, int ang) +{ + qreal rad = qDegreesToRadians(ang); + Forward(r*qSin(rad)); + moveRight(r*(1-qCos(rad))); + Rotate(ang); +} + +Car::Color Car::getColor() +{ + return this->m_color; +} + +qreal Car::getDir() +{ + return this->rotation(); +} diff --git a/car.h b/car.h new file mode 100644 index 0000000..69c023a --- /dev/null +++ b/car.h @@ -0,0 +1,38 @@ +#ifndef CAR_H +#define CAR_H + +#include +#include +#include +#include +#include + +//车身长宽 +#define M_LEN (200) +#define M_WID (130) + +class Car : public QGraphicsPixmapItem +{ +public: + enum Color { Pink, Red, Yellow, 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); //向左平移 + void moveRight(qreal vel = 3); //向右平移 + void Rotate(qreal ang = 30); //原地旋转 + void turnLeft(int r, int ang); //左转 + void turnRight(int r, int ang); //右转 + + Color getColor(); + qreal getDir(); + +private: + Color m_color; //车身颜色 N为0, 顺时针递增 + QPixmap m_pic; + QString m_number; + QPointF m_pos; + +}; + +#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/cars.cpp b/cars.cpp deleted file mode 100644 index 7fceb60..0000000 --- a/cars.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "cars.h" -#include - -Cars::Cars(QWidget *parent, int dir, Cars::Color color) : QWidget(parent) -{ - if(color==RANDOM) - color = Cars::Color(rand()%3); //车身颜色一定是确定的 - m_color = color; - m_dir = dir; - m_pos = this->pos(); - resize(M_WID, M_LEN); - switch (m_color) { - case Pink: - m_pixmap = QPixmap(":/cars/pink"); - break; - case Red: - m_pixmap = QPixmap(":/cars/red"); - break; - case Yellow: - m_pixmap = QPixmap(":/cars/yellow"); - break; - default: - m_pixmap = QPixmap(":/cars/red"); - break; - } -} - -void Cars::paintEvent(QPaintEvent *event) { - QPainter painter(this); - m_pos = this->pos(); - painter.translate(M_WID/2, M_LEN/2); //转移远点,设置旋转中心 - painter.rotate(m_dir); //旋转 - painter.translate(-M_WID/2, -M_LEN/2); //移回远点 - painter.drawPixmap(0, 0, M_WID, M_LEN, m_pixmap); //绘制小车 - painter.setPen(QPen(Qt::red, 5)); - painter.drawRect(0, 0, M_WID, M_LEN); - event->ignore(); -} - -void Cars::Forward(int vel) -{ - float ang = m_dir*PI/180; - this->move(m_pos.x()+vel*sin(ang), m_pos.y()-vel*cos(ang)); -} - -void Cars::Backward(int vel) -{ - float ang = m_dir*PI/180; - this->move(m_pos.x()-vel*sin(ang), m_pos.y()+vel*cos(ang)); -} diff --git a/cars.h b/cars.h deleted file mode 100644 index 68f7fc9..0000000 --- a/cars.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef CARS_H -#define CARS_H - -#include -#include -#include -#include -#include - -//车身长宽 -#define M_LEN (200) -#define M_WID (130) -#define PI (3.1415926f) - -class Cars : public QWidget -{ - Q_OBJECT -public: - enum Color { Pink, Red, Yellow, RANDOM }; - explicit Cars(QWidget *parent = nullptr, int dir = 0, Cars::Color color = RANDOM); - void paintEvent(QPaintEvent *event); - void Forward(int vel); - void Backward(int vel); -private: - Color m_color; //车身颜色 - int m_dir; //车头朝向 方向角 N为0, 顺时针递增 - QPixmap m_pixmap; - QString m_number; - QPoint m_pos; - -signals: - -public slots: -}; - -#endif // CARS_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..4d331fe --- /dev/null +++ b/path.cpp @@ -0,0 +1,39 @@ +#include "path.h" + +Path::Path() +{ + this->path = QList(); +} + +//用PathPoint数组初始化Path +Path::Path(PathPoint points[], int i) +{ + path = QList(); + 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