Class Shape

java.lang.Object
org.openpatch.scratch.Shape
Direct Known Subclasses:
Circle, Ellipse, Polygon, Rectangle, Triangle

public class Shape extends Object
A shape on the stage: a polygon, rectangle, circle, ellipse or triangle. Shapes serve as hitboxes (sprite.setHitbox(...)) and can be tested against each other. Circles and ellipses are stored as polygons that stay within a quarter pixel of the true outline.

Shapes that only touch, along an edge or at a corner, do not intersect.

  • Method Details

    • getPoints

      public double[] getPoints()
      Returns:
      the outline's points x0, y0, x1, y1, … (a copy)
    • contains

      public boolean contains(double x, double y)
      Parameters:
      x - x of a point
      y - y of a point
      Returns:
      whether the point lies inside the shape
    • intersects

      public boolean intersects(Shape other)
      Parameters:
      other - another shape
      Returns:
      whether the two shapes overlap with some area; shapes that only touch do not
    • scale

      public Shape scale(double scaleX, double scaleY)
      Parameters:
      scaleX - factor along x (negative mirrors)
      scaleY - factor along y
      Returns:
      the shape scaled about the origin
    • translate

      public Shape translate(double dx, double dy)
      Parameters:
      dx - distance along x
      dy - distance along y
      Returns:
      the shape moved
    • rotate

      public Shape rotate(double degrees, double anchorX, double anchorY)
      Turns the shape about a point, from the x axis towards the y axis (anticlockwise when y points up).
      Parameters:
      degrees - the angle
      anchorX - x of the point to turn about
      anchorY - y of the point to turn about
      Returns:
      the shape turned
    • getBounds

      public Bounds getBounds()
      Returns:
      the smallest rectangle around the shape