Klassenmethode zur Berechnung beliebiger Ankerpunkte

This commit is contained in:
ngb
2022-06-29 22:34:31 +02:00
parent 010a37fc0e
commit a01878286b

View File

@@ -117,6 +117,18 @@ public abstract class Shape extends FilledShape {
}
}
protected static Point2D.Double getAnchorPoint( double width, double height, Options.Direction anchor ) {
double wHalf = width * .5, hHalf = height * .5;
// anchor == CENTER
Point2D.Double anchorPoint = new Point2D.Double(
wHalf + wHalf * anchor.x,
hHalf + hHalf * anchor.y
);
return anchorPoint;
}
/**
* Bestimmt den Ankerpunkt der Form relativ zum gesetzten
* {@link #setAnchor(Options.Direction) Ankerpunkt}.
@@ -144,6 +156,7 @@ public abstract class Shape extends FilledShape {
* @return Der absolute Ankerpunkt.
*/
public Point2D.Double getAbsAnchorPoint( Options.Direction anchor ) {
// TODO: Die absoluten Anker müssten eigentlich die Rotation berücksichtigen.
Point2D.Double ap = getAnchorPoint(anchor);
ap.x += getX();
ap.y += getY();
@@ -219,6 +232,24 @@ public abstract class Shape extends FilledShape {
this.y = ap.getY() + dir.y * buff;
}
/**
* Richtet die Form entlang der angegebenen Richtung am Rand der
* Zeichenfläche aus.
*
* @param dir Die Richtung der Ausrichtung.
*/
public void alignTo( Options.Direction dir ) {
alignTo(dir, 0.0);
}
public void alignTo( Options.Direction dir, double buff ) {
Point2D anchorShape = Shape.getAnchorPoint(width, height, dir);
Point2D anchorThis = this.getAbsAnchorPoint(dir);
this.x += Math.abs(dir.x) * (anchorShape.getX() - anchorThis.getX()) + dir.x * buff;
this.y += Math.abs(dir.y) * (anchorShape.getY() - anchorThis.getY()) + dir.y * buff;
}
public void alignTo( Shape shape, Options.Direction dir ) {
alignTo(shape, dir, 0.0);
}