📄 intersectionlist.cs
字号:
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication2
{
/// <summary>
/// An overloaded array list that allows for faster identification of points which
/// have already been recognized as intersections.
///
/// Otherwise it is simply an ArrayList with the ability to draw itself.
/// </summary>
[Serializable]
public class IntersectionList : ArrayList
{
int Max_X, Min_X;
ArrayList speedTable;
public IntersectionList(int max_x, int min_x)
{
// Variables allow for faster data access
Max_X = max_x;
Min_X = min_x;
speedTable = new ArrayList(max_x-min_x);
for (int i=0;i<max_x-min_x+1;i++)
speedTable.Add(new ArrayList());
}
public override int Add(object value)
{
Intersection toAdd = null;
try {toAdd = (Intersection)value;}
catch
{
MessageBox.Show("You have attempted to add "+value+" to this Intersection List."+Environment.NewLine+
"You may only add Intersections.");
}
ArrayList hashes = (ArrayList)speedTable[toAdd.getPoint.X-Min_X];
hashes.Add(this.Count);
speedTable[toAdd.getPoint.X-Min_X]=hashes;
return base.Add (value);
}
public int pointSearch(Point p)
{
ArrayList hashes = null;
try {hashes = (ArrayList)speedTable[p.X-Min_X];}
catch (Exception e)
{
MessageBox.Show("Index: "+(p.X-Min_X)+" Px: "+p.X+" Min: "+Min_X+Environment.NewLine+
"Msg: "+e.Message);
}
for (int i=0; i<hashes.Count;i++)
{
int index = (int)hashes[i];
Intersection toTest = (Intersection)this[index];
if (toTest.getPoint.Y == p.Y)
{
if (toTest.getPoint.X != p.X)
MessageBox.Show("Algorithm Failed");
return index;
}
}
return -1;
}
public double shortestDist()
{
double theDist = Int32.MaxValue;
Point point1, point2;
for (int i=0; i < this.Count; i++)
{
for (int j=0; j<this.Count;j++)
{
if (i != j)
{
point1=((Intersection)this[i]).getPoint;
point2=((Intersection)this[j]).getPoint;
if (Distance(point1,point2) < theDist)
theDist = Distance(point1,point2);
}
}
}
return theDist;
}
public void draw (Graphics g, Scaler scale, int ticks)
{
Pen stop = new Pen(Color.HotPink,2);
Pen deadEnd = new Pen(Color.Gray,2);
Pen uncontrolled = new Pen (Color.Yellow,2);
Pen light1 = new Pen (Color.Red,2);
Pen light2 = new Pen (Color.Green,2);
for(int i=0;i<this.Count;i++)
{
Intersection needToDraw = (Intersection)this[i];
float x = scale.translate(needToDraw.getPoint).X-1.0f;
float y = scale.translate(needToDraw.getPoint).Y-1.0f;
if (needToDraw.getState(0,ticks) == Intersection.IS_STOP)
g.DrawEllipse(stop,x,y,2.0f,2.0f);
else if (needToDraw.getState(0,ticks) == Intersection.UNCONTROLLED)
g.DrawEllipse(uncontrolled,x,y,2.0f,2.0f);
else if (needToDraw.getState(0,ticks) == Intersection.DEAD_END)
g.DrawEllipse(deadEnd,x,y,2.0f,2.0f);
else if (needToDraw.getState(0,ticks) == Intersection.LIGHT_RED)
g.DrawEllipse(light1,x,y,2.0f,2.0f);
else
g.DrawEllipse(light2,x,y,2.0f,2.0f);
}
}
internal 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 + -