📄 unit1.~cpp
字号:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;
typedef vector<int> typePATH;
#include"clsNODE.h"
#include"clsMAP.h"
#include"clsMAP4d.h"
#include"clsMAP8dB.h"
#include"clsPathFinder.h"
#include"clsPathFinder4d.h"
#include"clsPathFinder8dB.h"
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include<mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// 名称:ClearMapPath
// 功能:清楚地图上的路径痕迹
//---------------------------------------------------------------------------
void TfrmMain::ClearMapPath()
{
int x, y;
for(x = 0; x < 50; x++)
for(y = 0; y < 50; y++){
if(imgMAP->Canvas->Pixels[x * 10 + 5][y * 10 + 5] == clYellow){
setBrush(x * 10 + 5, y * 10 + 5, 2);
imgMAP->Canvas->Rectangle(rectBrush);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::imgMAPMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(MapPathShowExist){
ClearMapPath();
MapPathShowExist = 0;
}
mouse_down = 1;
//判断是否需要清楚原来的起点
if(mouse_state == 3){
if(imgMAP->Canvas->Pixels[start_point_old_x * 10 + 5][start_point_old_y * 10 + 5] == clGreen){
setBrush(start_point_old_x * 10 + 5, start_point_old_y * 10 + 5, 2);
imgMAP->Canvas->Rectangle(rectBrush);
}
start_point_old_x = X / 10;
start_point_old_y = Y / 10;
}
//判断是否需要清楚原来的终点
if(mouse_state == 4){
if(imgMAP->Canvas->Pixels[finish_point_old_x * 10 + 5][finish_point_old_y * 10 + 5] == clRed){
setBrush(finish_point_old_x * 10 + 5, finish_point_old_y * 10 + 5, 2);
imgMAP->Canvas->Rectangle(rectBrush);
}
finish_point_old_x = X / 10;
finish_point_old_y = Y / 10;
}
setBrush(X, Y, mouse_state);
imgMAP->Canvas->Rectangle(rectBrush);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::imgMAPMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
mouse_down = 0;;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::imgMAPMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
//刷新状态栏坐标
stbMAP->Panels[0][0]->Text = IntToStr(X / 10 + 1) + ","+ IntToStr(Y / 10 + 1);
//鼠标不为设置起点或设置终点才可以描绘
if((mouse_down) && (mouse_state != 3) && (mouse_state != 4)){
setBrush(X, Y, mouse_state);
imgMAP->Canvas->Rectangle(rectBrush);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnDRAWClick(TObject *Sender)
{
mouse_state = 1;
stbMAP->Panels[0][1]->Text = "障碍";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnERASERClick(TObject *Sender)
{
mouse_state = 2;
stbMAP->Panels[0][1]->Text = "擦除";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSETSTARTClick(TObject *Sender)
{
mouse_state = 3;
stbMAP->Panels[0][1]->Text = "起点";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSETFINISHClick(TObject *Sender)
{
mouse_state = 4;
stbMAP->Panels[0][1]->Text = "终点";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
//使用双缓冲,否则imgMAP会闪烁
imgMAP->Parent->DoubleBuffered = True;
ClearMap();
MapPathShowExist = 0;
//初始化起点终点和鼠标
mouse_state = 1;
start_point_old_x = 51;
start_point_old_y = 51;
finish_point_old_x = 51;
finish_point_old_y = 51;
//初始化状态栏
stbMAP->Panels[0][0]->Text = "0,0";
stbMAP->Panels[0][1]->Text = "障碍";
pgbMAP->Parent = stbMAP;
pgbMAP->Top = 2;
pgbMAP->Left = stbMAP->Width - pgbMAP->Width;
}
//---------------------------------------------------------------------------
// 名称:setBrush
// 功能:设置笔刷坐标和颜色;
//---------------------------------------------------------------------------
void TfrmMain::setBrush(int X, int Y, int color)
{
int new_x, new_y;
new_x = X / 10 * 10;
new_y = Y / 10 * 10;
rectBrush.Left = new_x;
rectBrush.Top = new_y;
rectBrush.Right = new_x + 10;
rectBrush.Bottom = new_y + 10;
imgMAP->Canvas->Brush->Style = bsSolid;
switch(color){
case 1 : //鼠标为画笔
imgMAP->Canvas->Pen->Color = clBlue;
imgMAP->Canvas->Brush->Color = clBlue;
break;
case 2 : //鼠标为擦除
imgMAP->Canvas->Pen->Color = clBlack;
imgMAP->Canvas->Brush->Color = clBlack;
break;
case 3 : //鼠标为设置起点
imgMAP->Canvas->Pen->Color = clGreen;
imgMAP->Canvas->Brush->Color = clGreen;
break;
case 4 : //鼠标为设置终点
imgMAP->Canvas->Pen->Color = clRed;
imgMAP->Canvas->Brush->Color = clRed;
break;
case 5 : //颜色为路径
imgMAP->Canvas->Pen->Color = clYellow;
imgMAP->Canvas->Brush->Color = clYellow;
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSaveMapClick(TObject *Sender)
{
if(dlgMapSave->Execute()){
AnsiString mapFILENAME = dlgMapSave->FileName;
SaveMAP(mapFILENAME);
pgbMAP->Position = 0;
//Application->MessageBoxA("保存成功",Application->Name.c_str(),MB_OK);
}
}
//---------------------------------------------------------------------------
// 名称:CheckMap
// 功能:检查地图是否可以
//---------------------------------------------------------------------------
int TfrmMain::CheckMap()
{
int start_point_exist = 0, finish_point_exist = 0;
//转换imgMAP中图像到字符串列表strList中
int i, j;
for(i = 0; i < 50; i++)
for(j = 0; j < 50; j++)
switch(imgMAP->Canvas->Pixels[j * 10 + 5][i * 10 + 5]){
case clGreen :
start_point_exist = 1;
break;
case clRed :
finish_point_exist = 1;
break;
}
if(start_point_exist && finish_point_exist)
return 1;
return 0;
}
//---------------------------------------------------------------------------
// 名称:SaveMAP
// 功能:保存地图用
//---------------------------------------------------------------------------
void TfrmMain::SaveMAP(AnsiString mapFILENAME)
{
TStringList *strList = new TStringList();
AnsiString strLine;
//转换imgMAP中图像到字符串列表strList中
int i, j;
for(i = 0; i < 50; i++){
strLine = "";
for(j = 0; j < 50; j++){
switch(imgMAP->Canvas->Pixels[j * 10 + 5][i * 10 + 5]){
case clBlack :
strLine += ".";
break;
case clBlue :
strLine += "X";
break;
case clGreen :
strLine += "s";
break;
case clRed :
strLine += "f";
break;
default :
strLine += ".";
}
pgbMAP->Position = i * 2 + j * (2 / 50);
}
strList->Add(strLine);
}
//保存地图文件
strList->SaveToFile(mapFILENAME);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 名称:SaveTempMAP
// 功能:保存临时地图用
//---------------------------------------------------------------------------
void TfrmMain::SaveTempMAP(AnsiString mapFILENAME)
{
TStringList *strList = new TStringList();
AnsiString strLine;
//转换imgMAP中图像到字符串列表strList中
int i, j;
for(i = 0; i < 50; i++){
strLine = "";
for(j = 0; j < 50; j++){
switch(imgMAP->Canvas->Pixels[j * 10 + 5][i * 10 + 5]){
case clBlack :
strLine += ".";
break;
case clBlue :
strLine += "X";
break;
case clGreen :
strLine += "s";
break;
case clRed :
strLine += "f";
break;
default :
strLine += ".";
}
}
strList->Add(strLine);
}
//保存地图文件
strList->SaveToFile(mapFILENAME);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnFindPathClick(TObject *Sender)
{
if(CheckMap() == 0){
Application->MessageBoxA("没有设置起点或终点", Application->Name.c_str(), MB_OK);
return;
}
pgbMAP->Position = 20;
unsigned long time_cost;
AnsiString mapFILENAME = "map.tmp";
SaveTempMAP(mapFILENAME);
pgbMAP->Position = 40;
int idxALG = rdgALG->ItemIndex;
typePATH path;
if(idxALG == 0){
clsMAP4d map;
map.InputMapFile(mapFILENAME.c_str());
clsPathFinder4d PathFinder;
pgbMAP->Position = 60;
//开启寻路
time_cost = (unsigned long)(timeGetTime());
path = PathFinder.FindPath(map);
time_cost = (unsigned long)(timeGetTime()) - time_cost;
pgbMAP->Position = 80;
stbMAP->Panels[0][2]->Text = IntToStr(time_cost) + "ms";
if(path.size()){
pgbMAP->Position = 100;
ShowMapPath(path, map);
MapPathShowExist = 1;
}
SetCost(path);
}
if(idxALG == 1){
clsMAP map;
map.InputMapFile(mapFILENAME.c_str());
clsPathFinder PathFinder;
pgbMAP->Position = 60;
//开启寻路
time_cost = (unsigned long)(timeGetTime());
path = PathFinder.FindPath(map);
time_cost = (unsigned long)(timeGetTime()) - time_cost;
pgbMAP->Position = 80;
stbMAP->Panels[0][2]->Text = IntToStr(time_cost) + "ms";
if(path.size()){
pgbMAP->Position = 100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -