📄 form1.cs
字号:
{
Car theCar = (Car)myCars[i];
if (theCar.getDriveCount >= ticks)
continue;
int code = theCar.Drive(myMap);
if (code == Car.STUCK_AT_INTERSECTION) //code = 0
{//Add car to the correct intersection's list, but only if it isn't already there
ArrayList al = congestedInters[theCar.getNextIntersection];
if (al == null)
al = new ArrayList();
bool toAdd = true;
for (int j=0; j<al.Count; j++)
{
if (((Car)al[j]).getID == theCar.getID)
{
toAdd = false;
break;
}
}
if (toAdd)
al.Add(theCar);
congestedInters[theCar.getNextIntersection] = al;
}
else if (code > Car.STUCK_AT_INTERSECTION) //Good event
{
if (code == Car.GOAL_REACHED)
{
Sim_Events += theCar.getID+ ": Arrived at Goal"+Environment.NewLine;
myCars.RemoveAt(i);
i--;
carCount.Text = "Cars: "+myCars.Count;
}
}
else if (code < 0) //Error Handling - Not in use
{
MessageBox.Show ("Error Happened");
}
}
//Advance one car through each intersection where there is backup
for (int i=0; i<congestedInters.Length; i++)
{
if (!(congestedInters[i]==null) && congestedInters[i].Count >= 1)
{
((Car)congestedInters[i][0]).passThruIntersection(myMap);
congestedInters[i].RemoveAt(0);
}
if (!(congestedInters[i]==null) && congestedInters[i].Count == 0)
congestedInters[i] = null;
}
Invalidate(true);
}
#region File Menu
private void menuItem2_Click(object sender, System.EventArgs e)
{
OpenDialog.ShowDialog();
}
private void OpenDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
myMap = new CityMap();
myMap.LoadMap(OpenDialog.FileName);
Map_Loaded = true;
ticks = 0;
this.Refresh();
}
private void menuItem13_Click(object sender, System.EventArgs e)
{
Map_Loaded = false;
Invalidate(true);
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
SaveDialog.ShowDialog();
}
private void SaveDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
myMap.SaveMap(SaveDialog.FileName);
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
ImportDialog.ShowDialog();
}
private void ImportDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
myMap = new CityMap();
myMap.ImportMap(File.OpenText(ImportDialog.FileName));
Map_Loaded = true;
ticks = 0;
this.Refresh();
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Not yet implemented");
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
#region Simulation Menu
private void menuItem9_Click(object sender, System.EventArgs e)
{//Starts Simulation, invokes the Pathfinder object
if (Map_Loaded)
{
Simulation_Setup setupForm = new Simulation_Setup(2 * myMap.getIntersectionList.Count);
setupForm.ShowDialog();
if (setupForm.Value < 0)
return;
myCars.Clear();
myCars.Capacity = setupForm.Value;
myMap.PrepareSim();
ProgressBar theForm = new ProgressBar(myCars.Capacity,10);
theForm.Show();
for (int i=0; i<myCars.Capacity;i++)
{
if ((i+1)%10 == 0)
theForm.increment();
Car toAdd = new Car(myMap,i);
int startIntersection = toAdd.getStartSeg.To_Intersection;
int toIntersection = toAdd.getGoalSeg.To_Intersection;
if (Distance(toAdd.getStartSeg.from,toAdd.getGoalPoint) < Distance (toAdd.getStartSeg.to,toAdd.getGoalPoint))
startIntersection = toAdd.getStartSeg.From_Intersection;
if (Distance(toAdd.getGoalSeg.from,toAdd.getStartPoint) < Distance (toAdd.getGoalSeg.to,toAdd.getStartPoint))
toIntersection = toAdd.getGoalSeg.From_Intersection;
Pathfinder aStar = new Pathfinder(startIntersection,toIntersection,myMap);
toAdd.path = aStar.getPath();
if (toAdd.path == null)
i--;
else
myCars.Add(toAdd);
}
theForm.Hide();
ticks = 0;
myMap.SortCars();
congestedInters = new ArrayList[myMap.getIntersectionList.Count];
timer1.Start();
Sim_Started = true;
Sim_Events = "";
carCount.Text = "Cars: "+myCars.Capacity;
Invalidate(true);
}
else
MessageBox.Show("You must load a map to begin a simulation");
}
private void menuItem10_Click(object sender, System.EventArgs e)
{
if (menuItem10.Checked)
{
menuItem10.Checked = false;
timer1.Start();
}
else
{
menuItem10.Checked = true;
timer1.Stop();
}
}
private void menuItem21_Click(object sender, System.EventArgs e)
{
myCars.Clear();
}
private void menuItem14_Click(object sender, System.EventArgs e)
{
MessageBox.Show("The Following Events have been reported:"+Environment.NewLine+
Sim_Events,"Events Logged");
}
private void SimGraphics_Click(object sender, System.EventArgs e)
{
SimGraphics.Checked = !SimGraphics.Checked;
}
private void menuItem17_Click(object sender, System.EventArgs e)
{
myMap.reRandomize();
}
private void menuItem20_Click(object sender, System.EventArgs e)
{
ticks = 0;
countLbl.Text = "###";
carCount.Text = "No Sim";
Invalidate(true);
}
#endregion
#region Tools Menu
#region Setup Selection Variables
private void CB_Box_Click(object sender, System.EventArgs e)
{
to_Select=CITY_BLOCK;
select_Type = SELECT_BOX;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void CB_Line_Click(object sender, System.EventArgs e)
{
to_Select=CITY_BLOCK;
select_Type = SELECT_LINE;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void I_Box_Click(object sender, System.EventArgs e)
{
to_Select=INTERSECTION;
select_Type = SELECT_BOX;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void I_Line_Click(object sender, System.EventArgs e)
{
to_Select=INTERSECTION;
select_Type = SELECT_LINE;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void I_Point_Click(object sender, System.EventArgs e)
{
to_Select=INTERSECTION;
select_Type = SELECT_POINT;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
to_Select = NONE;
select_Type = NONE;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Arrow;
}
private void menuItem18_Click(object sender, System.EventArgs e)
{
to_Select = THRU_STREETS;
select_Type = SELECT_BOX;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void menuItem19_Click(object sender, System.EventArgs e)
{
to_Select = THRU_STREETS;
select_Type = SELECT_LINE;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
private void menuItem16_Click(object sender, System.EventArgs e)
{
to_Select = THRU_STREETS;
select_Type = SELECT_POINT;
this.mapBox.Cursor = System.Windows.Forms.Cursors.Cross;
}
#endregion
private void mapBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (to_Select != NONE)
{
select_Point_1 = new PointF(e.X,e.Y);
}
}
private void mapBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch (to_Select)
{
case CITY_BLOCK:
ArrayList blocksFound = myMap.getBlocks(select_Point_1, new PointF(e.X,e.Y),select_Type);
if (blocksFound.Count > 0)
{
City_Blocks formInstance = new City_Blocks(blocksFound,myMap);
formInstance.ShowDialog();
myMap = formInstance.getMap;
formInstance.Dispose();
}
break;
case INTERSECTION:
ArrayList intersFound = myMap.getIntersections(select_Point_1, new PointF(e.X,e.Y),select_Type);
if (intersFound.Count > 0)
{
Intersection_Edit interForm = new Intersection_Edit(intersFound,myMap);
interForm.ShowDialog();
myMap = interForm.getMap;
interForm.Dispose();
}
break;
case THRU_STREETS:
ArrayList interFound = myMap.getIntersections(select_Point_1, new PointF(e.X,e.Y),select_Type);
if (interFound.Count > 0)
{
for (int i=0;i<interFound.Count;i++)
{
ThruStreetFinder interForm = new ThruStreetFinder((int)interFound[i],myMap);
interForm.ShowDialog();
myMap = interForm.getMap;
interForm.Dispose();
}
}
break;
}
}
private void menuItem12_Click(object sender, System.EventArgs e)
{
Invalidate(true);
}
#endregion
private double Distance(Point a, Point b)
{
double xdiff = a.X - b.X;
double ydiff = a.Y - b.Y;
return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -