添加Path类

This commit is contained in:
TurnMeOn committed 2017-12-19 23:09:10 +08:00
1 parent dd4edc5643
commit ff6b9bb3bf
8 files changed
+100 -26

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 \
+12 -12
View File
@@ -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();
}
+5 -5
View File
@@ -1,5 +1,5 @@
#ifndef CARS_H
#define CARS_H
#ifndef CAR_H
#define CAR_H
#include <QGraphicsPixmapItem>
#include <QPixmap>
@@ -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
+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
+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; // 总层数
+38
View File
@@ -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<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