Merge branch 'master' of github.com:w-mj/DataStructureProject

This commit is contained in:
wmj committed 2017-12-20 10:05:59 +08:00
commit 3b68cc00c4
10 files changed
+208 -95

No files matched your search

+6 -4
View File
@@ -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 \
+86
View File
@@ -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();
}
+38
View File
@@ -0,0 +1,38 @@
#ifndef CAR_H
#define CAR_H
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QString>
#include <QPoint>
#include <QtMath>
//车身长宽
#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
+4 -1
View File
@@ -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
#endif // CARMANANGER_H
-50
View File
@@ -1,50 +0,0 @@
#include "cars.h"
#include <QPainter>
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));
}
-36
View File
@@ -1,36 +0,0 @@
#ifndef CARS_H
#define CARS_H
#include <QWidget>
#include <QPixmap>
#include <QString>
#include <QPaintEvent>
#include <QPoint>
//车身长宽
#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
+1 -1
View File
@@ -1,6 +1,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cars.h"
#include "car.h"
#include <QDialog>
#include <QLabel>
#include <QScrollArea>
+3 -3
View File
@@ -5,7 +5,7 @@
#include <QPixmap>
#include <QVector>
#include <QQueue>
#include "cars.h"
#include "car.h"
class ParkingLotItem: public QGraphicsPixmapItem
{
@@ -16,8 +16,8 @@ private:
QVector<QPixmap*> m_pixmap; // 每一层的pixmap
QVector<int> m_max_capacity; // 每一层的容量
QVector<int> m_num_of_cars; // 每一层当前停的车数量
QVector<QVector<Cars*> > m_cars; // 停在每一层的车
QVector<QQueue<Cars*> >m_waitting; // 在每个入口等待的车
QVector<QVector<Car*> > m_cars; // 停在每一层的车
QVector<QQueue<Car*> >m_waitting; // 在每个入口等待的车
QStringList m_xml_path; // 每一层xml文件地址
QStringList m_name; // 每一层的名字
const uint m_num_of_layer = 2; // 总层数
+39
View File
@@ -0,0 +1,39 @@
#include "path.h"
Path::Path()
{
this->path = QList<PathPoint>();
}
//用PathPoint数组初始化Path
Path::Path(PathPoint points[], int i)
{
path = QList<PathPoint>();
for(int j=0; j<i; j++)
addPoint(points[j]);
}
PathPoint Path::getPoint(int i)
{
return path.value(i);
}
void Path::addPoint(int i, const PathPoint &point)
{
path.insert(i, point);
}
void Path::addPoint(const PathPoint &point)
{
path.append(point);
}
bool Path::isEmpty()
{
return path.isEmpty();
}
int Path::pointsCount()
{
return path.length();
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef PATH_H
#define PATH_H
#include<QList>
#include<QPoint>
//路径上的点和车在该点的角度
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<PathPoint> path;
int len;
};
#endif // PATH_H