I have a program that find coordinates between two point with a predefined interval:
ArrayList<Point> genPoints(double smallDist, Point a, Point b)
{
ArrayList<Point> outputPoints = new ArrayList<Point>();
double distAB = dist2Points(a, b);
if (smallDist > distAB)
return null;
int numGeneratedPoints = (int)(distAB / smallDist);
Vector vectorBA = b - a;
vectorBA.Normalize();
Point currPoint = a;
for (int i = 0; i < numGeneratedPoints; i++)
{
currPoint = currPoint + vectorBA * smallDist;
if (dist2Points(currPoint, b) != 0)
outputPoints.Add(currPoint);
}
return outputPoints;
}
now I called that method using the following code, where I am passing two points P1, P2 and predefined distance.
gp = genPoints(1, p1, p2)
when I want to show the values, it gives me the following:
4.94974746830583,4.94974746830583
5.65685424949238,5.65685424949238
6.36396103067893,6.36396103067893
7.07106781186548,7.07106781186548
7.77817459305202,7.77817459305202
for (int i = 0; i < gp.Count; i++)
System.Console.WriteLine(" " + gp[i]);
I don’t know how to access those values individually. I couldn’t even use gp[i].x or gp[i].y. but, somehow I need to access those values separately.
Any help would be greatly appreciated.
The problem is that you’re using a non-generic collection,
ArrayList. The indexer is just returningobject, so you have to cast:If you’re using .NET 2 or higher, it would be better to use a generic collection such as
List<T>– make your method return aList<Point>and you’ll be able to write:… no cast is required.
There are a number of benefits to generics – unless you’re forced to use non-generic collections (e.g. you’re writing code for .NET 1.1) you should pretty much avoid them and always use the generic collections.
As an aside, methods conventionally start with a capital letter in .NET – so I would name this method
GeneratePointsinstead.