This POTD can be used as a component in your Joust game - all the methods here c
ID: 649331 • Letter: T
Question
This POTD can be used as a component in your Joust game - all the methods here can be useful for detecting and resolving collisions between moving objects, though you might not need to use them all in Joust.
Java has a built in rectangle class that has methods that tell you if two rectangles are intersecting, and can give you the intersecting rectangle of those two rectangles, shown below:
In the image above, A. intersects(B) is true and A.intersection(B) is the green rectangle.
Please implement the following API in your class:
+ CollisionBox(Rectangle rect)
+ CollisionBox(x : int, y : int, width : int, height : int)
+ getRectangle() : Rectangle
+ collidesWith(other : CollisionBox) : boolean
+ moveTo(x : int, y : int) : void
+ moveCenterTo(x : int, y : int) : void
+ isHigherThan(other : CollisionBox) : boolean
+ isLeftOf(other : CollisionBox) : boolean
+ verticalDifference(other : CollisionBox) : int
+ horizontalDifference(other : CollisionBox) : int
+ isSmallerOverlapVertical(other : CollisionBox) : boolean
(+ = public, - = private)
Constructors
Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in.
collidesWith(CollisionBox other)
This method should return a boolean indicating whether or not this CollisionBox
Collision Box ? rect : Rectangle+ CollisionBox(Rectangle rect)
+ CollisionBox(x : int, y : int, width : int, height : int)
+ getRectangle() : Rectangle
+ collidesWith(other : CollisionBox) : boolean
+ moveTo(x : int, y : int) : void
+ moveCenterTo(x : int, y : int) : void
+ isHigherThan(other : CollisionBox) : boolean
+ isLeftOf(other : CollisionBox) : boolean
+ verticalDifference(other : CollisionBox) : int
+ horizontalDifference(other : CollisionBox) : int
+ isSmallerOverlapVertical(other : CollisionBox) : boolean
(+ = public, - = private)
Constructors
Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in.
collidesWith(CollisionBox other)
This method should return a boolean indicating whether or not this CollisionBox
This POTD can be used as a component in your Joust game - all the methods here can be useful for detecting and resolving collisions between moving objects, though you might not need to use them all in Joust. Java has a built in rectangle class that has methods that tell you if two rectangles are intersecting, and can give you the intersecting rectangle of those two rectangles, shown below: In the image above, A. intersects(B) is true and A.intersection(B) is the green rectangle. Please implement the following API in your class: Collision Box ? rect : Rectangle + CollisionBox(Rectangle rect) + CollisionBox(x : int, y : int, width : int, height : int) + getRectangle() : Rectangle + collidesWith(other : CollisionBox) : boolean + moveTo(x : int, y : int) : void + moveCenterTo(x : int, y : int) : void + isHigherThan(other : CollisionBox) : boolean + isLeftOf(other : CollisionBox) : boolean + verticalDifference(other : CollisionBox) : int + horizontalDifference(other : CollisionBox) : int + isSmallerOverlapVertical(other : CollisionBox) : boolean (+ = public, - = private) Constructors Both constructors should initialize the rect field to an appropriate rectangle - either the parameter passed in, or a rectangle created from the parameters passed in. collidesWith(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox?s rectangle intersects with other?s rectangle. moveTo(int x, int y) This method should move the cornder of CollisionBox to the given parameters. moveCenterTo(int x, int y) This method should move the center of CollisionBox to the given parameters. If the CollisionBox has odd-number width or height, you may round either way. isHigherThan(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox is higher on the screen (smaller y) than other. Compare the center of the rectangles, not a corner. isLeftOf(CollisionBox other) This method should return a boolean indicating whether or not this CollisionBox is to the left of other on the screen (smaller x). Compare the center of the rectangles, not a corner. verticalDifference(CollisionBox other) This method should return the minimal y distance one of the two boxes would need to move in order for them not to overlap anymore. That will be the difference between the minimal y of one box and the maximal y of the other. You may assume this method is only called on boxes that do overlap. horizontalDifference(CollisionBox other) This method should return the minimal x distance one of the two boxes would need to move in order for them not to overlap anymore. That will be the difference between the minimal x of one box and the maximal x of the other. You may assume this method is only called on boxes that do overlap. isSmallerOverlapVertical(CollisionBox other) This method should return a boolean indicating whether or not the vertical difference between these two CollisionBox objects is smaller than the horizontal difference. You may assume this method is only called on boxes that do overlap. A simple solution includes invoking the horizontalDifference and verticalDifference methods. Example CollisionBox box1 = new CollisionBox(new Rectangle(1, 1, 3, 3)); CollisionBox box2 = new CollisionBox(2, 2, 1, 3); CollisionBox box3 = new CollisionBox(0, 0, 5, 2); box1.collidesWith(box2);// should return true box1.collidesWith(box3);// should return true. box2.collidesWith(box3);// should return false. box1.isSmallerOverlapVertical(box2); // should return false. box1.isSmallerOverlapVertical(box3);// should return true. box2.moveTo(3, 2); box1.isHigherThan(box2);// should return true. box1.verticalDifference(box2); // should return 2. box2.verticalDifference(box1); // should return 2. box2.isLeftOf(box1); // should return false. box1.horizontalDifference(box2); // should return 1. box2.horizontalDifference(box1); // should return 1.Explanation / Answer
hope this may helps you
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class intersection extends JFrame {
Rectangle r = new Rectangle(50, 50, 100, 100);
Rectangle r1 = new Rectangle(100, 100, 75, 75);
intersection() {
super("Intersection");
setSize(250, 250);
}
public void paint(Graphics g) {
g.drawRect(r.x, r.y, r.width, r.height);
g.drawRect(r1.x, r1.y, r1.width, r1.height);
Rectangle r2 = r.intersection(r1);
System.out.println(r2);
g.fillRect(r2.x, r2.y, r2.width, r2.height);
}
public static void main(String[] args) {
JFrame f = new intersection();
f.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.