Skip to content

Commit 0e928c1

Browse files
committed
Bound position of newly created vertex.
1 parent 18bc022 commit 0e928c1

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,12 +634,11 @@ The opposite vertex exists in the (di)graph, but we have not yet
634634
x = mx;
635635
y = my;
636636
} else {
637-
/* TODO: fix -- the placing point can be set out of bounds*/
638637
Point2D p = UtilitiesPoint2D.rotate(existing.getPosition().add(50.0, 50.0),
639638
existing.getPosition(), Math.random() * 360);
640639

641-
x = p.getX();
642-
y = p.getY();
640+
x = boundValue(p.getX(), 0, getWidth());
641+
y = boundValue(p.getY(), 0, getHeight());
643642
}
644643
}
645644

@@ -1132,6 +1131,19 @@ private void enableDoubleClickListener() {
11321131
});
11331132
}
11341133

1134+
private static double boundValue(double value, double min, double max) {
1135+
1136+
if (Double.compare(value, min) < 0) {
1137+
return min;
1138+
}
1139+
1140+
if (Double.compare(value, max) > 0) {
1141+
return max;
1142+
}
1143+
1144+
return value;
1145+
}
1146+
11351147
/**
11361148
* Represents a tuple in Java.
11371149
*

0 commit comments

Comments
 (0)