kopia lustrzana https://github.com/jameshball/osci-render
Created constructor for graph and made Line immutable.
rodzic
bdbb1a56c2
commit
0a479d82e7
|
@ -1,4 +0,0 @@
|
|||
package doubleset;
|
||||
|
||||
public class DoubleComparator {
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package doubleset;
|
||||
|
||||
public class DoubleSet {
|
||||
|
||||
}
|
|
@ -3,21 +3,31 @@ package graphs;
|
|||
import shapes.Line;
|
||||
import shapes.Point;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class Graph {
|
||||
private List<Node> nodes;
|
||||
private List<Point> locations;
|
||||
private Map<Point, Node> nodes;
|
||||
|
||||
public Graph(List<Line> lines) {
|
||||
this.nodes = new ArrayList<>();
|
||||
this.locations = new ArrayList<>();
|
||||
this.nodes = new HashMap<>();
|
||||
|
||||
for (Line line : lines) {
|
||||
nodes.add(new Node(line.getA()));
|
||||
nodes.add(new Node(line.getB()));
|
||||
if (!nodes.containsKey(line.getA())) {
|
||||
nodes.put(line.getA(), new Node(line.getA()));
|
||||
}
|
||||
|
||||
if (!nodes.containsKey(line.getB())) {
|
||||
nodes.put(line.getB(), new Node(line.getB()));
|
||||
}
|
||||
|
||||
Node nodeA = nodes.get(line.getA());
|
||||
Node nodeB = nodes.get(line.getB());
|
||||
|
||||
nodeA.addAdjacent(nodeB);
|
||||
nodeB.addAdjacent(nodeA);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,26 @@ package graphs;
|
|||
|
||||
import shapes.Point;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Node {
|
||||
private Point location;
|
||||
private List<Node> adjacentNodes;
|
||||
|
||||
public Node(Point location) {
|
||||
this.location = location;
|
||||
this.adjacentNodes = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Point getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void addAdjacent(Node node) {
|
||||
adjacentNodes.add(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package shapes;
|
||||
|
||||
public class Line {
|
||||
private Point a;
|
||||
private Point b;
|
||||
private double weight;
|
||||
private final Point a;
|
||||
private final Point b;
|
||||
private final double weight;
|
||||
|
||||
public static final double DEFAULT_WEIGHT = 100;
|
||||
|
||||
|
@ -61,28 +61,28 @@ public class Line {
|
|||
return b.getY();
|
||||
}
|
||||
|
||||
public void setX1(double x1) {
|
||||
a.setX(x1);
|
||||
public Line setX1(double x1) {
|
||||
return new Line(x1, getY1(), getX2(), getY2());
|
||||
}
|
||||
|
||||
public void setY1(double y1) {
|
||||
a.setY(y1);
|
||||
public Line setY1(double y1) {
|
||||
return new Line(getX1(), y1, getX2(), getY2());
|
||||
}
|
||||
|
||||
public void setX2(double x2) {
|
||||
b.setX(x2);
|
||||
public Line setX2(double x2) {
|
||||
return new Line(getX1(), getY1(), x2, getY2());
|
||||
}
|
||||
|
||||
public void setY2(double y2) {
|
||||
b.setY(y2);
|
||||
public Line setY2(double y2) {
|
||||
return new Line(getX1(), getY1(), getX2(), y2);
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(double weight) {
|
||||
this.weight = weight;
|
||||
public Line setWeight(double weight) {
|
||||
return new Line(getX1(), getY1(), getX2(), getY2(), weight);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,7 +6,7 @@ public class Point {
|
|||
private double x;
|
||||
private double y;
|
||||
|
||||
private static final double EPSILON = 0.0001;
|
||||
private static final double EPSILON = 0.001;
|
||||
|
||||
public Point(double x, double y) {
|
||||
this.x = x;
|
||||
|
@ -42,6 +42,15 @@ public class Point {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(round(getX()), round(getY()));
|
||||
}
|
||||
|
||||
private double round(double d) {
|
||||
return Math.round(d * 1000) / 1000d;
|
||||
}
|
||||
|
||||
public Point copy() {
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue