A Star Search Pen Plotter moving from two unconnected nodes
I'm having trouble with a programming assignment, its a penplotter, there have been quite a few questions about it already here.
Here's a summary:
The paper sheet is understood to be laid out on a grid with both the x-axis and y-axis running from 0 up to infinity (notionally). All drawings are fully contained in this quadrant and can be assumed to fit on the page. Lines can always be assumed not to be contained in other lines. The pen always starts at the origin (0, 0) but can finish anywhere. Lines are specified by the (x, y) coordinates (in integers) of their end points on the grid. The pen can either draw a line between any two points or move (without drawing anything) in a straight line between any two points. As should be obvious, a line can be drawn in either direction. Since we want to minimize the total time to draw a figure, assume the pen moves at constant speed so that an optimal drawing is one that minimizes the total distance moved by the pen whilst not drawing.
All input will be in a file with a sequence of lines of the following form:
Line between x1 y2 and x2 y2
I'm using this for my input: Line between 0 0 and 2 2
Line between 4 1 and 4 4
Line between 4 4 and 4 7
Line between 2 6 and 4 4
Line between 4 4 and 6 2
Line between 6 6 and 4 4
Line between 2 2 and 4 4
and the edges are being stored into the arraylist. I'm not sure why its returning an NullPointerException
I'm almost done, except the following for loop is returning an exception and I'm not sure why. It aims to get the list of开发者_JAVA百科 all connected edges in the graph
The exception says: Exception in thread "main" java.lang.NullPointerException at Nodes.getConnectedEdges(Nodes.java:55) which is the for loop line "for (Edges a : lines"
public ArrayList<Edges> getConnectedEdges(ArrayList<Edges> lines) {
ArrayList<Edges> returnData = new ArrayList<Edges>();
for (Edges a : lines) {
if(a.getFromNode() == this ){ // if this node is the from node of that edge
returnData.add(a);
}
// if this node is the to node of that edge
if(a.getToNode() == this){
returnData.add(a);
}
}
return returnData;
}
The on flowing problem would be how to get from say the origin (0,0) to an unconnected point say (2,2)?
Thanks in advance
精彩评论