📄 mapseg.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace WindowsApplication2
{
/// <summary>
/// One of the foundations of the CityMap, each MapSeg represents a city block
/// </summary>
[Serializable]
public class MapSeg
{
public const string DEF_ST_NAME = "None Supplied";
private int FX, FY, TX, TY,inter_From,inter_To,carsEntered;
private string streetName;
private double length;
private int[] lanes = new int [2];
private int speedLimit;
private ArrayList CarsFromTo;
private ArrayList CarsToFrom;
public MapSeg (int From_X, int From_Y, int To_X, int To_Y)
{
FX = From_X;
FY = From_Y;
TX = To_X;
TY = To_Y;
streetName = DEF_ST_NAME;
double xdiff = FX - TX;
double ydiff = FY - TY;
length = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
lanes[0] = 1;
lanes[1] = 1;
speedLimit = 25;
CarsFromTo = new ArrayList();
CarsToFrom = new ArrayList();
carsEntered = 0;
}
#region Simple Properties
public int LaneCount
{
get {return lanes[0];}
set {lanes[0] = value;}
}
public int visits
{
get {return carsEntered;}
set {carsEntered = value;}
}
public int LaneCount_Reversed
{
get {return lanes[1];}
set {lanes[1] = value;}
}
public string StreetName
{
get {return streetName;}
set {streetName = value;}
}
public int SpeedLimit
{
get {return speedLimit;}
set {speedLimit = value;}
}
public int From_Intersection
{
get {return inter_From;}
set {inter_From = value;}
}
public int To_Intersection
{
get {return inter_To;}
set {inter_To = value;}
}
public int min_X()
{
if (FX < TX) return FX;
else return TX;
}
public int max_X()
{
if (FX > TX) return FX;
else return TX;
}
public int min_Y()
{
if (FY < TY) return FY;
else return TY;
}
public int max_Y()
{
if (FY > TY) return FY;
else return TY;
}
public Point from
{
get { return new Point(FX,FY);}
}
public Point to
{
get { return new Point(TX,TY);}
}
#endregion
public void PushCar(Car theCar, bool FromTo)
{
if (FromTo)
CarsFromTo.Add(theCar);
else
CarsToFrom.Add(theCar);
carsEntered++;
}
public Point getCarPosition (int CarID, bool FromTo)
{//Returns the furthest point which a car can travel to or (0,0) if there is no limitation
ArrayList toUse = (FromTo)? CarsFromTo : CarsToFrom;
int lanecount = (FromTo)? lanes[0] : lanes[1];
int i=0;
for (; i<toUse.Count; i++)
if (((Car)toUse[i]).getID == CarID)
{
break;
}
if (i >= lanecount)
return ((Car)toUse[i-lanecount]).getLocation;
else
return new Point (0,0);
}
public int RemoveCar(int CarID, bool FromTo)
{
ArrayList toUse = (FromTo)? CarsFromTo : CarsToFrom;
for (int i=0; i<toUse.Count; i++)
{
if (((Car)toUse[i]).getID == CarID)
{
toUse.RemoveAt(i);
return 1;
}
}
return -1;
}
public void drivePreviousCars(int CarID, int count, bool FromTo, CityMap myMap)
{//Called from Car.Drive, this function ensures that all cars infront of CarID
// have already been driven
ArrayList toUse = (FromTo)? CarsFromTo : CarsToFrom;
for (int i=0; i<toUse.Count; i++)
{
if (((Car)toUse[i]).getID == CarID)
return;
else if (((Car)toUse[i]).getDriveCount < count)
((Car)toUse[i]).Drive(myMap);
}
}
public string Report_Info()
{
string toReturn = "Starting point: ("+FX+","+FY+")"+Environment.NewLine;
toReturn += "Starting Intersection: "+inter_From+Environment.NewLine;
toReturn += "Ending point: ("+TX+","+TY+")"+Environment.NewLine;
toReturn += "Ending Intersection: "+inter_To+Environment.NewLine;
toReturn += "Length: "+length+Environment.NewLine;
toReturn += "Street Name: "+streetName+Environment.NewLine;
toReturn += "Speed Limit: "+speedLimit+Environment.NewLine;
toReturn += "Number of Lanes (F->T): "+lanes[0]+Environment.NewLine;
toReturn += "Number of Lanes (T->F): "+lanes[1]+Environment.NewLine;
return toReturn;
}
public void SortCars()
{//Called before simulation starts to ensure that initial ordering is correct
if (CarsFromTo.Count > 1)
CarsFromTo = putInOrder (CarsFromTo, new Point(TX,TY));
if (CarsToFrom.Count > 1)
CarsToFrom = putInOrder (CarsToFrom, new Point(FX,FY));
}
public ArrayList putInOrder(ArrayList toSort, Point target)
{//Because the maximum simulation size is roughly one car per map segment,
//and the function is only called once during simulation setup, the low overhead
//O(n^2) algorithm is acceptable in this situation
for (int i=0; i<toSort.Count; i++)
for (int j=0; j<toSort.Count-1; j++)
{
if (Distance(((Car)toSort[j]).getLocation,target) >
Distance(((Car)toSort[j+1]).getLocation,target))
{
Car temp = (Car)toSort[j];
toSort[j] = (Car)toSort[j+1];
toSort[j+1] = temp;
}
}
return toSort;
}
public Point getRandomPoint()
{//Called when creating cars, allows cars to start (and end) in the middle of blocks
Random r = new Random();
int dist = r.Next((int)length);
Point newLoc = new Point();
double Z = dist/Math.Sqrt((to.X - from.X)*(to.X - from.X)
+ (to.Y - from.Y)*(to.Y - from.Y));
newLoc.X = (int)(from.X + Z * (to.X - from.X));
newLoc.Y = (int)(from.Y + Z * (to.Y - from.Y));
if (Distance(newLoc,to) > Distance(from,to))
{
newLoc.X = (int)(from.X - Z * (to.X - from.X));
newLoc.Y = (int)(from.Y - Z * (to.Y - from.Y));
}
return newLoc;
}
public void clearCars()
{//Initialize lists at the beginning of each simulation
CarsToFrom.Clear();
CarsFromTo.Clear();
}
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 + -