RotatablePolygon - CoolPotato31F/Java-Graphics GitHub Wiki
RotatablePolygon Class
Overview
The RotatablePolygon class extends the Polygon class, adding the ability to rotate the polygon around its centroid. It maintains the original points of the polygon and applies rotation transformations when needed.
Attributes
-
private Point[] originalPoints
A deep copy of the original points of the polygon, used for rotation calculations. -
private Point center
The centroid of the polygon, which serves as the rotation pivot. -
private double rotation = 0
The current rotation angle of the polygon in degrees.
Constructors
RotatablePolygon(Point[] p)
Constructs a RotatablePolygon from an array of Point objects.
Parameters:
p- An array ofPointobjects defining the polygon vertices.
Methods
Point getCenter()
Gets the centroid of the polygon.
Returns:
Point- The center point of the polygon.
void rotate(double degree)
Rotates the polygon by the specified degree around its centroid.
Parameters:
degree- The angle in degrees by which to rotate the polygon.
Point[] getOriginalPoints()
Gets the original, untransformed points of the polygon.
Returns:
Point[]- An array of the originalPointobjects.
String toString()
Returns a string representation of the RotatablePolygon, including its points.
Returns:
String- A string representation of the rotated polygon.
void drawPanel(Graphics2D graphics)
Renders the RotatablePolygon onto a Graphics2D panel.
Parameters:
graphics- TheGraphics2Dobject used for rendering the polygon.