Find the Intersection points of 2 rectangles - java

we have two examples of rectangle:
public static Rectangle rect1 = new Rectangle(20, 300, 400, 160);
public static Rectangle rect2 = new Rectangle(150, 60, 230, 450);
The problem is to find an algorithm that finds all intersection points of these two rectangles

You can get the intersection points using inbuilt methods intersection
Rectangle rect1 = new Rectangle(20, 300, 400, 160);
Rectangle rect2 = new Rectangle(150, 60, 230, 450);
Rectangle intersection = rect1.intersection(rect2);
System.out.println(intersection);

For 2 rectangles, there would be four cases for intersection,
One is inside another or they are totally disjoint - No point of intersection.
They share a single point - 1 point of intersection.
They intersect at exactly four points.
They share part of one or more sides - infinite points of intersection.
These conditions can be used to write tests to find the solution.

You should do this:
public Area getRectanglesColisionArea(Rectangle rect1, Rectangle rect2){
Area shape1 = new Area(rect1);
Area shape2 = new Area(rect2);
return shape1.intersect(shape2);
}
Returning area shape is the
To call the function just:
Rectangle rect1 = new Rectangle(20, 300, 400, 160);
Rectangle rect2 = new Rectangle(150, 60, 230, 450);
Area result = getRectanglesColisionArea(rect1,rect2);
The Area result is the shape of the intersection, from there you can get the intersection points:
Rectangle inters = result.getBounds();
Double x1=inters.getX();
Double y1=inters.getY();
Double x2=inters.getX()+inters.getWidth();
Double y2=inters.getY()+inters.getHeight();

Related

Drawing Rectangles in Java

I am trying to draw rectangles in Java like this picture:
I visualize the coordinates as I do at math but I come up with the rectangles turned upside down which is like this:
I know Im missing just a few things.What should I do?
(Colors will be edited)
public class BlockTower
{
public static void main(String[] args)
{
Rectangle rect1 = new Rectangle(20, 70, 40, 30);
rect1.draw();
rect1.setColor(Color.BLUE);
rect1.fill();
Rectangle rect2 = new Rectangle(60, 70, 40, 30);
rect2.draw();
rect2.setColor(Color.MAGENTA);
rect2.fill();
Rectangle rect3 = new Rectangle(100, 70, 40, 30);
rect3.draw();
rect3.setColor(Color.CYAN);
rect3.fill();
Rectangle rect4 = new Rectangle(40, 100, 40, 30);
rect4.draw();
rect4.setColor(Color.RED);
rect4.fill();
Rectangle rect5 = new Rectangle(80, 100, 40, 30);
rect5.draw();
rect5.setColor(Color.PINK);
rect5.fill();
Rectangle rect6 = new Rectangle(60, 130, 40, 30);
rect6.draw();
rect6.setColor(Color.BLUE);
rect6.fill();
//TODO finish the draft to display the six blocks
}
}
Coordinates in Swing start from Top Left. That means you have to recalculate your y-coordinates. So the bottom of your panel is actually at the current height.
If you've calculated something to be at coordinates (x,y) it now has to be at coordinates (x, height - y) instead.

Calculating the Value of Pi in Processing

Processing is an environment that makes use of Java. I am trying to to use the Monte Carlo method to calculate the value of Pi. I am trying to create a dartboard (a circle within a square), and return "Yes" whenever the randomly selected point is selected within the circle.
Processing uses a coordinate system where the top left corner is the origin, rightwards is the positive x-axis, and downwards is the positive y-axis.
Here's my code:
float circleX;
float circleY;
float r;
void setup() {
size(360, 360);
circleX = 50;
circleY = 50;
frameRate(0.5);
}
void draw() {
background(50);
fill(255);
stroke(255);
fill(100);
ellipse(180, 180, 360, 360);
ellipse(circleX, circleY, 10, 10);
circleX = random(360);
circleY = random(360);
r = (circleX-180)*(circleX-180) + (180-circleY)*(180-circleY);
if (r < 32400) {
print("Yes! ");
}
}
However, on many instances, points inside the circle do not return "Yes," and points outside the circle do return "Yes." Any ideas on what is wrong?
You have to swap the lines generating the random coordinates and drawing it:
// Generate new random coordinates
circleX = random(360);
circleY = random(360);
// Draw circle at those coordinates
ellipse(circleX, circleY, 10, 10);
// Check whether the coordinates are withing the big circle
r = (circleX-180)*(circleX-180) + (180-circleY)*(180-circleY);
The way you do it, the circle is drawn before you generate new coordinates, which you then check.

Rotate two parallel lines to create an X

The following code is the code that I am using to rotate two rectangles is below
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);
//r1
Rectangle2D r1 = new Rectangle2D.Double(0, 0, 50, 4);
g2d.rotate(Math.toRadians(45));
g2d.fill(r1);
//r3
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90));
g2d.fill(r3);
This create something which looks like this
Whereas I am trying to create something which looks like this
This occurs since when the rectangles are rotated, they are both rotated around the point 0,0. To fix that I tried using rotate(double theta, double x, double y). However I am having trouble using that correctly. For example when I have tried
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 25, 25);
or
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 0, 25);
I get similar undesired results when both the rectangles were being rotated around the point 0,0. I would appreciate any help if fixing my problem.
If you are wondering why I have done it like this, it is because I am hoping to make a effect similar to when you click on the 3 parallel lines seen here by the time I finish coding the graphic
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class Cross extends JPanel {
private Rectangle2D rectangle;
Cross() {
rectangle = new Rectangle2D.Double(0, 0, 50, 4);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.white);
AffineTransform at = g2.getTransform();
g2.translate(5, 5);
g2.rotate(Math.toRadians(45));
g2.fill(rectangle);
g2.setTransform(at);
g2.translate(5, 5 + Math.sqrt(2) * 25);
g2.rotate(Math.toRadians(-45));
g2.fill(rectangle);
g2.setTransform(at);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Cross");
frame.add(new Cross());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(128, 128);
frame.setVisible(true);
}
}
Although I think I might have done mistake somewhere with maths (it looks somewhat odd), this should give you an idea.
So it turns out that this can be done with some relatively simple maths. As the shape I am trying to make is a perfect X.
To work out the position for the rectangle we can use Pythagorean theorem.
The image above shows two steps.
Translation [2√2, 0] from the point [0, 0]
Rotate 45deg from the point [2√2, 0]
Next we need to work out the minimum point of this rectangle. Again we can use Pythagorean theorem.
This tells us where the top point of the second rectangle will be
Difference in height: 4 - 2√2
Bottom of line when straight: [0, 27√2 + (4 - 2√2)] = [0, 4 + 25√2]
Top of line when straight: [0, 25√2]
Finally we can put in the last rectangle starting at [0, 0]
Translation [0, 25√2] from the point [0, 0]
Rotate -45deg from the point [0, 25√2]
Now that the theory is out of the way, what does this look like in code? It looks similar to the code below
//Values
final static double[] r1Points = {2.828427125, 0}; //Equivilant 2√2
final static double[] r3Points = {0, 35.35533906}; //Equivilant 25√2
final static int[] widthNHeight = {50, 4}; //Width then height
final static double angle = 45.0; //Angle to rotate lines
//Declaring the rectangles
Rectangle2D r1 = new Rectangle2D.Double(r1Points[0], r1Points[1], widthNHeight[0], widthNHeight[1]);
Rectangle2D r3 = new Rectangle2D.Double(r3Points[0], r3Points[1], widthNHeight[0], widthNHeight[1]);
//r1
g2d.rotate(Math.toRadians(angle), r1Points[0], r1Points[1]); //Rotates graphic for first rectangle
g2d.fill(r1);
//r3
g2d.rotate(Math.toRadians(-angle), r1Points[0], r1Points[1]); //Rotates the graphic back to straight
g2d.rotate(Math.toRadians(-angle), r3Points[0], r3Points[1]); //Rotates graphic for second rectangle
g2d.fill(r3);

Java - Color Rectangle

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
int height = getHeight();
int width = getWidth();
g.drawRect(350, 510, 110, 170);
g.drawRect(470, 510, 110, 170);
g.drawRect(590, 510, 110, 170);
g.drawRect(710, 510, 110, 170);
g.drawRect(830, 510, 110, 170);
g.drawRect(350, 30, 110, 170);
g.drawRect(470, 30, 110, 170);
g.drawRect(590, 30, 110, 170);
g.drawRect(710, 30, 110, 170);
g.drawRect(830, 30, 110, 170);
g.setColor(Color.RED);
g.drawRect(110, 450, 110, 170);
g.drawRect(110, 60, 110, 170);
}
}
I need to color red every Rectangle ( i mean inside the Rectangle ), but with this g.setColor ( Color.RED ) ; i can only color the exterior part of Rectanlge
drawRect() from the JavaDocs
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.
That's why you need to use fillRect:
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
From your last comment: And what about if i want to have the half rectangle blue and the rest red? What should i do then ?
Draw 2 rectangles, one ends where the other one starts, something like:
g.setColor(Color.BLUE);
g.fillRect(50, 50, 50, 50);
g.setColor(Color.RED);
g.fillRect(100, 50, 50, 50);
I haven't tested the above code, but you get the idea :)
g.drawRect() only draws rectangle's border. You probably should use g.fillRect() which fills your rectangle with solid color. JavaDoc
Use fillRect() to fill a rectangular area rather than just drawing a rectangle.
Try this:
g.fillRect(x, y, width, height)
Description here!

Java 2D Graphics rotating objects keeping original coords system?

I've already searched this topic everywhere on the Internet, but nothing.
I'm programming with JAVA and I'm painting in a JPanel some shapes like yellow stars with a sort of spaceship in the center of the screen. My purpose is to make the entire scene, apart from the spaceship which remains stationary at the center, rotate around the center (and of course around the spaceship) when I press a certain button.
Now, when I rotate the stars I make them rotate around the center point wich changes as the spaceship moves, but when I do a rotation, the coordinates system changes and the ship moves diagonally, while I want to rotate the scene keeping possible to move the spaceship vertically and horizontally.
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
// RIEMPIMENTO SFONDO
g2.setColor(new Color(10, 10, 10));
g2.fillRect(0, 0, 1366, 768);
// DEBUGGER
g2.setColor(Color.WHITE);
g2.drawString("|" + posx + "|" + posy + "| " + scale, 1250, 758);
// TRASFORMAZIONE NAVICELLA
AffineTransform at = new AffineTransform();
at.translate(683, 384);
at.scale(scale, scale);
g2.setTransform(at);
AffineTransform backup = g2.getTransform();
// DISEGNO NAVICELLA
g2.setColor(Color.LIGHT_GRAY);
int[] xp = {0, -25, -25, 0, 25, 25};
int[] yp = {-25, 0, 25, 0, 25, 0};
g2.drawPolygon(xp, yp, 6);
// TRASFORMAZIONE SPAZIO
at.translate(posx, posy);
at.rotate(Math.toRadians(rotation), -posx, -posy);
g2.setTransform(at);
// DISEGNO SPAZIO
STAR(g2, 300 + posx, 100 + posy, 200, 200, Color.YELLOW);
STAR(g2, -100 + posx, -200 + posy, 200, 200, Color.YELLOW);
g2.dispose();
}

Categories