package inheritance;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.shape.Polygon;
public class BaseSpaceObject extends Polygon {
/**
* Adds the point given by x,y to this Polygon
* @param x the x-coordinate
* @param y the y-coordinate
*/
protected void addPoint(double x, double y) {
getPoints().add(x);
getPoints().add(y);
}
/**
* Adds the pointspoint given by thex,y sequenceto of x- and y-coordinatesthis Polygon
* @param xysx the x- and y-coordinatescoordinate
*/
protected void addPoints(double... xys) {
@param y the y-coordinate
for (int*/
i = 0; iprotected < xys.length; i += 2void addPoint(double x, double y) {
getPoints().add(x);
addPoint(xys[i], xys[i + 1] getPoints().add(y);
}
}/**
}
/*** Adds the points given by the sequence of x- and y-coordinates
* @param Addsxys the point given by angle, length in polar coordinates
* @param angle the angle
* @param length the length
*/x- and y-coordinates
*/
protected void addPoints(double... xys) {
for (int i = 0; i < xys.length; i += 2) {
protected void addPolarPoint(double angle, double length) {
addPoint(xys[i], xys[i + 1]);
addPoint(Math.cos(angle) * length, Math.sin(angle) * length);}
}
/**
* ReturnsAdds the positionpoint ofgiven thisby Polygonangle, aslength ain Point2Dpolar objectcoordinates
* @param @returnangle the position as a Point2D object angle
* @param length the length
*/
protected void addPolarPoint(double publicangle, Point2Ddouble getPosition(length) {
return new Point2D(getTranslateX(), getTranslateY()addPoint(Math.cos(angle) * length, Math.sin(angle) * length);
}
/**
* Moves (displaces) this Polygon by Returns the givenposition dx,of dy
* @param dx the x displacementthis Polygon as a Point2D object
* @param dy the y displacement@return the position as a Point2D object
*/
public voidPoint2D translate(double dx, double dygetPosition() {
setTranslateXreturn new Point2D(getTranslateX() + dx);
setTranslateY(, getTranslateY() + dy);
}
/**
* Returns the position of the center of Moves (displaces) this Polygon.
* The asParentCoordinates determines if by the positiongiven is transformed into the parent coordinate system.dx, dy
* @param asParentCoordinates determines if the position is transformed intodx the parent coordinatex systemdisplacement
* @return@param dy the centery positiondisplacement
*/
public Point2Dvoid getCenter(boolean asParentCoordinatestranslate(double dx, double dy) {
Bounds bounds = getBoundsInLocal(setTranslateX(getTranslateX() + dx);
Point2D center = new Point2D((bounds.getMaxX() + bounds.getMinX()) / 2, (bounds.getMaxY setTranslateY(getTranslateY() + bounds.getMinY()) / 2);dy);
}
/**
* Returns the ifposition (asParentCoordinates) {
of the center of this Polygon.
* The asParentCoordinates centerdetermines = localToParent(center);
}
if the position is transformed into the parent coordinate system.
* @param asParentCoordinates determines returnif center;
the position is transformed }
into the parent coordinate /**system
* Returns@return the number of points in this Polygoncenter position
*/
@return the number ofpublic pointsPoint2D in this PolygongetCenter(boolean asParentCoordinates) {
*/
Bounds publicbounds int= getPointCountgetBoundsInLocal();
{
Point2D center = return getPointsnew Point2D((bounds.getMaxX() + bounds.sizegetMinX()) / 2;
}
/**
* Determines of a specific point of another Polygon is inside this Polygon, (bounds.getMaxY() + bounds.getMinY()) / 2);
if (asParentCoordinates) {
* @param other the other Polygon
center = localToParent(center);
* @param pointNum the number of the point to check}
* @return
*/
return center;
public boolean contains(BaseSpaceObject other, int pointNum) {
}
/**
* Returns the number ObservableList<Double>of points = other.getPoints();in this Polygon
* @return the doublenumber of xpoints = points.get(pointNum * 2), y = points.get(pointNum * 2 + 1);in this Polygon
*/
public int getPointCount() {
return this.contains(parentToLocal(other.localToParent(x, y)));
}
} |