public class Graph
extends java.lang.Object
Constructor and Description |
---|
Graph()
Create a graph with an initial capacity of 16 nodes.
|
Graph(int nbrNodes)
Create a graph with an initial capacity based on an estimate of
the number of nodes to be added.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addEdge(int fromID,
int toID,
double cost)
Add a unidirectional edge to the graph.
|
boolean |
addEdge(int fromID,
int toID,
double costOutward,
double costInward)
Add bidirectional link with the costs indicated.
|
void |
addNode(GraphNode node)
Add a node to the list.
|
void |
compact()
Clear out all remaining floating edges.
|
double |
distance(GraphNode nodeFrom,
GraphNode nodeTo)
Gets the world distance between 2 nodes.
|
double |
distance(int nodeFromID,
int nodeToID)
Get the distance between two nodes.
|
GraphEdge |
getEdge(int fromID,
int toID)
Get the edge between 2 nodes.
|
GraphEdge[] |
getEdgeArray()
Will return an array of all the GraphEdges in the graph.
|
GraphEdge[] |
getEdgeArray(int from)
Will return an array of all the GraphEdges that start from the node.
|
<T extends GraphEdge> |
getEdgeArray(int from,
T[] array)
Will return an array of all the GraphEdges that start from the node.
|
<T extends GraphEdge> |
getEdgeArray(T[] array)
Will return an array of all the GraphEdges in the graph.
|
double |
getEdgeCost(int fromID,
int toID)
Get the cost of traversing an edge between 2 nodes.
|
java.util.LinkedList<GraphEdge> |
getEdgeList(GraphNode node)
Gets a list of GraphEdges leaving (from) this node.
|
java.util.LinkedList<GraphEdge> |
getEdgeList(int nodeID)
Gets a list of GraphEdges leaving (from) this node.
|
int |
getNbrEdges() |
int |
getNbrNodes()
get the number of nodes in the graph
|
GraphNode |
getNode(int id)
Get a node with a given id.
|
GraphNode[] |
getNodeArray()
Will return an array of all the GraphNodes in the graph.
|
<T extends GraphNode> |
getNodeArray(T[] array)
Will return an array of all the GraphNodes in the graph.
|
GraphNode |
getNodeNear(double x,
double y,
double maxDistance)
Locate and return the first node encountered that is within a
stated distance of a position at [x,y,0]
|
GraphNode |
getNodeNear(double x,
double y,
double z,
double maxDistance)
Locate and return the first node encountered that is within a
stated distance of a position at [x,y,z]
|
GraphNode |
getNodeNearest(double x,
double y)
Locate and return the node nearest a given position irrespective of the distance
apart in the x/y plane (ignores z).
|
GraphNode |
getNodeNearest(double x,
double y,
double z)
Locate and return the node nearest a given position irrespective of the distance apart.
|
GraphNode |
getNodeNearest(GraphNode a_node)
Find the node nearest the node specified based on Euclidean distance between them.
|
GraphNode |
getNodeNearest(int id)
Find the node nearest the node with the specified ID number based on Euclidean distance between them.
|
boolean |
hasEdge(int from,
int to)
Sees whether the graph has this edge
|
boolean |
hasNode(int id)
Does a node with a given id exist?
|
static Graph |
makeFromXML(java.io.File xmlFile) |
static Graph |
makeFromXML(processing.core.PApplet app,
java.lang.String xmlFilename) |
static Graph |
makeFromXML(java.lang.String xmlFilename) |
boolean |
removeEdge(int fromID,
int toID)
Remove an edge between 2 nodes.
|
boolean |
removeNode(int nodeID)
If the node exists remove it and all edges that start
or end at this node.
|
java.lang.String |
toString()
Used for debugging only.
|
public Graph()
public Graph(int nbrNodes)
nbrNodes
- public static Graph makeFromXML(processing.core.PApplet app, java.lang.String xmlFilename)
public static Graph makeFromXML(java.lang.String xmlFilename)
public static Graph makeFromXML(java.io.File xmlFile)
public double distance(GraphNode nodeFrom, GraphNode nodeTo)
nodeFrom
- first nodenodeTo
- second nodepublic double distance(int nodeFromID, int nodeToID)
nodeFromID
- ID of first nodenodeToID
- ID of second nodepublic void addNode(GraphNode node)
node
- public boolean removeNode(int nodeID)
nodeID
- id of the node to removepublic GraphNode getNode(int id)
id
- public boolean hasNode(int id)
id
- public GraphNode getNodeNear(double x, double y, double z, double maxDistance)
x
- y
- z
- maxDistance
- only consider a node that is with this distance of [x,y,z]public GraphNode getNodeNear(double x, double y, double maxDistance)
x
- y
- maxDistance
- only consider a node that is with this distance of [x,y,z]public GraphNode getNodeNearest(GraphNode a_node)
a_node
- the node to use as the origin.public GraphNode getNodeNearest(int id)
id
- the ID number of the node to use as the originpublic GraphNode getNodeNearest(double x, double y, double z)
x
- y
- z
- public GraphNode getNodeNearest(double x, double y)
x
- y
- public int getNbrNodes()
public boolean addEdge(int fromID, int toID, double cost)
fromID
- the ID number of the from nodetoID
- the ID number of the to nodecost
- cost from > topublic boolean addEdge(int fromID, int toID, double costOutward, double costInward)
fromID
- the ID number of the from nodetoID
- the ID number of the to nodecostOutward
- cost from > tocostInward
- cost to > frompublic void compact()
public GraphEdge getEdge(int fromID, int toID)
fromID
- ID for the from nodetoID
- ID for the to nodepublic double getEdgeCost(int fromID, int toID)
fromID
- ID for the from nodetoID
- ID for the to nodepublic boolean removeEdge(int fromID, int toID)
graph.removeEdge(22, 33);
graph.removeEdge(33, 22);
fromID
- ID for the from nodetoID
- ID for the to nodepublic boolean hasEdge(int from, int to)
from
- node id of from-nodeto
- node if of to-nodepublic java.util.LinkedList<GraphEdge> getEdgeList(int nodeID)
nodeID
- id of the node where the edges start frompublic java.util.LinkedList<GraphEdge> getEdgeList(GraphNode node)
node
- the node where the edges start frompublic GraphEdge[] getEdgeArray()
public <T extends GraphEdge> T[] getEdgeArray(T[] array)
T
- array
- a zero length array of GraphNode or any derived class.public GraphEdge[] getEdgeArray(int from)
from
- the node where the edges start frompublic <T extends GraphEdge> T[] getEdgeArray(int from, T[] array)
T
- from
- the node where the edges start fromarray
- a zero length array of GraphNode or any derived class.public int getNbrEdges()
public GraphNode[] getNodeArray()
public <T extends GraphNode> T[] getNodeArray(T[] array)
T
- array
- a zero length array of GraphNode or any derived class.public java.lang.String toString()
toString
in class java.lang.Object
Processing library AI_for_2D_Games by Peter Lager. (C) 2013