Class Rectangle
- All Implemented Interfaces:
Shape
public class Rectangle extends java.lang.Object implements Shape
Just as a quick notice: this code is really old. Like, really old. As in at least 6 months, which is quite old for a new project. Anyways. If you find yourself looking at this comment right here and you'd like to make this code better, be my guest.
The four lines; top, right, bottom, and left, are used for collision detection. Mostly everything else in this class is fairly useless.
The following words are used frequently throughout this class. Some of those words have the same meaning.
- TOP (or FRONT)
- RIGHT
- BOTTOM (or BACK)
- LEFT
- Since:
- 0.1.0
- Author:
- Colin Robertson
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Rectangle.Corners
A list of different positions on a rectangle. -
Field Summary
Fields Modifier and Type Field Description Point
backLeft
The back-left corner of the rectangle.Point
backRight
The back-right corner of the rectangle.Line
bottom
The bottom of the rectangle.Point
center
The center of the rectangle.Rectangle.Corners
drawCorner
Which corner the rectangle should be drawn from.Point
frontLeft
The front-left corner of the rectangle.Point
frontRight
The front-right corner of the rectangle.Line
left
The left of the triangle.Line
right
The right of the rectangle.Rectangle.Corners
rotateCorner
The corner which the rectangle will be rotated from.double
rotationalAngle
The angle at which the entire rectangle should be rotate from.Point
startingPoint
The Point where the shape will be drawn from.Line
top
The top of the rectangle.double
xDraw
How far, in the X dimension, the rectangle should be drawn.double
yDraw
How far, in the Y dimension, the rectangle should be drawn. -
Constructor Summary
Constructors Constructor Description Rectangle(Rectangle.Corners drawCorner, Rectangle.Corners rotateCorner, Point startingPoint, double xDraw, double yDraw, double rotationalAngle)
The one and only... -
Method Summary
Modifier and Type Method Description boolean
doesLineEnterShape(Line line)
Check whether or not a given line at any point enters our rectangle.boolean
doesRectangleIntersect(Rectangle rectangle)
Check whether or not a given rectangle enters this rectangle.Point
getCenterPoint()
Get the very center of the rectangle, as a point.int
getComponentCount()
Get a count of how many components (lines) are in the rectangle.int
getComponents()
Get how many components are in the shape.boolean
isLineInShape(Line line)
Check whether or not a given line is inside the shape.boolean
isPointInShape(Point point)
Used to determine whether a given point is inside of the rectangle.private Point[]
rotateAllPointsAroundFirstPoint(Point[] points, double rotationFactor)
A privately and internally used method for rotating three points around a fourth.private Point[]
rotateArray(Point[] arr, int reps)
Rotate an array.Point
rotatePoint(Point center, Point point, double angle)
Rotate a point around another point by a specified angle, notated in radians.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
frontRight
The front-right corner of the rectangle. This is relative to the origin + rotation. -
backRight
The back-right corner of the rectangle. This is relative to the origin + rotation. -
frontLeft
The front-left corner of the rectangle. This is relative to the origin + rotation. -
backLeft
The back-left corner of the rectangle. This is relative to the origin + rotation. -
center
The center of the rectangle. This is relative to the origin + rotation. -
top
The top of the rectangle. This is relative to the origin + rotation. -
right
The right of the rectangle. This is relative to the origin + rotation. -
bottom
The bottom of the rectangle. This is relative to the origin + rotation. -
left
The left of the triangle. This is relative to the origin + rotation. -
drawCorner
Which corner the rectangle should be drawn from. This is NOT always the same corner which the rectangle will be rotated from, however - just the corner it'll be drawn from. X and Y are relative to this corner, meaning top right's Y draw would have a different impact than bottom left's Y draw - one (top right) would be negative, and the other (bottom left) would be positive. -
rotateCorner
The corner which the rectangle will be rotated from. You don't need to rotate the rectangle, by the way - it's an entirely optional step. If you don't want to rotate the rectangle, you can use any corner (CENTER or maybe your drawCorner) as the corner of rotation, and set the rotation angle to 0, representing a net change of zero rotation. -
startingPoint
The Point where the shape will be drawn from. This point is directly correlative to drawCorner - having a drawCorner of top right means that this code will interpret the starting point as the top right corner of the rectangle, and thus draw the rectangle as so. Make sure that this is the correct corner. Assuming you're familiar with something such as JavaScript's "canvas" functionality, you will (most often) want to use the top left corner as a starting point and figure out the Point of said corner. -
xDraw
public final double xDrawHow far, in the X dimension, the rectangle should be drawn. Note that this is relative to which corner the rectangle is being drawn from. Having a draw corner of top left means that both X and Y draws are negative. -
yDraw
public final double yDrawHow far, in the Y dimension, the rectangle should be drawn. Note that this is relative to which corner the rectangle is being drawn from. Having a draw corner of the top left means that both X and Y draws are negative. -
rotationalAngle
public final double rotationalAngleThe angle at which the entire rectangle should be rotate from. I believe that this angle is in radians, and any code you write using this angle should reflect that. If you have an angle which is in degrees, Java's native math class should include a function for converting degrees to radians.
-
-
Constructor Details
-
Rectangle
public Rectangle(Rectangle.Corners drawCorner, Rectangle.Corners rotateCorner, Point startingPoint, double xDraw, double yDraw, double rotationalAngle)The one and only... rectangle constructor!Rectangle construction and calculation is rather expensive compared to other operations related to shapes. This code is pretty old and pretty shitty - if you have the time to revise it, and have the kindness in your cold, cold, cold heart to do so, that would be greatly appreciated.
- Parameters:
drawCorner
- which corner the rectangle should be drawn from. This is NOT always the same corner which the rectangle will be rotated from, however - just the corner it'll be drawn from. X and Y are relative to this corner, meaning top right's Y draw would have a different impact than bottom left's Y draw - one (top right) would be negative, and the other (bottom left) would be positive.rotateCorner
- the corner which the rectangle will be rotated from. You don't need to rotate the rectangle, by the way - it's an entirely optional step. If you don't want to rotate the rectangle, you can use any corner (CENTER or maybe your drawCorner) as the corner of rotation, and set the rotation angle to 0, representing a net change of zero rotation.startingPoint
- the Point where the shape will be drawn from. This point is directly correlative to drawCorner - having a drawCorner of top right means that this code will interpret the starting point as the top right corner of the rectangle, and thus draw the rectangle as so. Make sure that this is the correct corner. Assuming you're familiar with something such as JavaScript's "canvas" functionality, you will (most often) want to use the top left corner as a starting point and figure out the Point of said corner.xDraw
- how far, in the X dimension, the rectangle should be drawn. Note that this is relative to which corner the rectangle is being drawn from. Having a draw corner of top left means that both X and Y draws are negative.yDraw
- how far, in the Y dimension, the rectangle should be drawn. Note that this is relative to which corner the rectangle is being drawn from. Having a draw corner of the top left means that both X and Y draws are negative.rotationalAngle
- the angle at which the entire rectangle should be rotate from. I believe that this angle is in radians, and any code you write using this angle should reflect that. If you have an angle which is in degrees, Java's native math class should include a function for converting degrees to radians.
-
-
Method Details
-
rotatePoint
Rotate a point around another point by a specified angle, notated in radians. (Or at least I think so.)- Parameters:
center
- the center point to rotate around - in other words, this is the "axis" or "origin."point
- the point which should be rotated around the center.angle
- the angle, in radians, depicting how far the point should be rotated around the center point.- Returns:
- the freshly rotated point.
-
rotateAllPointsAroundFirstPoint
A privately and internally used method for rotating three points around a fourth.- Parameters:
points
- the array of points to use.rotationFactor
- how far the points should be rotated.- Returns:
- a freshly rotated array.
-
rotateArray
Rotate an array. This doesn't mean rotate them geometrically, or relative to another point on a cartesian Point plane. Rather, this literally just means to rotate the array as if it's an infinitely scrolling array, which Java obviously doesn't have.- Parameters:
arr
- the arrayreps
- how many times it should be rotated- Returns:
- a rotated array
-
isPointInShape
Used to determine whether a given point is inside of the rectangle.This functions by finding the maximums of all of the X and Y positions of our rectangle and ensuring that the given point fits in between the range of the aforementioned maximums and the later-calculated minimums.
- Specified by:
isPointInShape
in interfaceShape
- Parameters:
point
- the point to check- Returns:
- whether or not the point is inside the rectangle
-
doesRectangleIntersect
Check whether or not a given rectangle enters this rectangle.This might need optimization in the future. I'm not really sure how powerful the control hub is or how many calculations per second it can do or whatever metric you'd like to use, but for now I'm just going to leave this as it is and hope it works.
- Parameters:
rectangle
- the rectangle to check.- Returns:
- whether the two rectangles intersect.
-
doesLineEnterShape
Check whether or not a given line at any point enters our rectangle.- Parameters:
line
- the line to check.- Returns:
- whether that line enters the rectangle.
-
getComponentCount
public int getComponentCount()Get a count of how many components (lines) are in the rectangle.This will obviously always be four - rectangles can only have four lines.
- Returns:
- how many lines there are.
-
getComponents
public int getComponents()Get how many components are in the shape.- Specified by:
getComponents
in interfaceShape
- Returns:
- a list of all the components.
-
isLineInShape
Description copied from interface:Shape
Check whether or not a given line is inside the shape.Line-checks are the most expensive operation for a shape to perform, aside, of course, from initialization. Unless you have a very strong reason to use this method, the
Shape.isPointInShape(Point)
method is significantly more effective.- Specified by:
isLineInShape
in interfaceShape
- Parameters:
line
- the line to check.- Returns:
- whether or not the line intersects with the shape.
-
getCenterPoint
Get the very center of the rectangle, as a point.- Returns:
- the center point of the shape
-