I created a digraph using the jgrapht library, and it takes as vertices Point
objects I created. These objects take as parameters two coordinates and a type. I made a simple and short example:
public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);
public static Point firstPoint = new Point(2, 7, "A");
public static Point secondPoint = new Point(2, 8, "B");
public static Point thirdPoint = new Point(2, 9, "B");
public static Point fourthPoint = new Point(2, 4, "C");
void setup () {
directedGraph.addVertex(firstPoint);
directedGraph.addVertex(secondPoint);
directedGraph.addVertex(thirdPoint);
directedGraph.addVertex(fourthPoint);
directedGraph.addEdge(firstPoint, secondPoint);
directedGraph.addEdge(secondPoint, thirdPoint);
directedGraph.addEdge(secondPoint, fourthPoint);
int degree = directedGraph.outDegreeOf(secondPoint);
if (degree >= 2) {
for (Point successor : Graphs.successorListOf (directedGraph, secondPoint)) {
if (/*the iD is equal to B*/){
for (Point predecessor : Graphs.predecessorListOf (directedGraph, secondPoint )) {
directedGraph.addEdge(predecessor, successor);
}
}
}
}
// --------------------------------------------------------------
public static ArrayList<Point> pointList = new ArrayList<Point>();
public static class Point {
public int x;
public int y;
public String iD;
public Point(int x, int y, String iD)
{
this.x = x;
this.y = y;
this.iD= iD;
}
@Override
public String toString() {
return ("[x="+x+" y="+y+" iD="+iD+ "]");
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + this.x;
hash = 71 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object other)
{
if (this == other)
return true;
if (!(other instanceof Point))
return false;
Point otherPoint = (Point) other;
return otherPoint.x == x && otherPoint.y == y;
}
}
I'd like to add a condition in the if statement on the iD of the vertices, but not on the two other parameters of my Point objects. Is there a way to do this ?
Aucun commentaire:
Enregistrer un commentaire