# Thread: colllision rectangle-polygon

1. Member
Join Date
Jan 2009
Posts
3
Rep Power
0

## colllision rectangle-polygon

Hello!!
Well,I have some problems with my program( I am new to java..).I want to know if there is a collision between a polygon and a rectangle.How am I going to do that??

for the rectangle I used the method getBounds():
Java Code:
```public Rectangle r;
Graphics g;

public Obstacle(int x,int y,int w,int h)
{
r=new Rectangle(x,y,w,h);
}

public Rectangle getBounds() {
return new Rectangle(r.x,r.y,r.width,r.height);
}

public void draw(Graphics gr)
{
g=gr;
g.setColor(Color.red);
g.fillRect(r.x,r.y,r.width,r.height);
}```
Am i able to do the same for the polygon (use getbounds) and then use the intersects method?
Java Code:
```int[] xs = { (int)((Diameter/2)*1.4), -(int)((Diameter/2)*0.6), -(int)((Diameter/2)*0.6) };
int[] ys = { 0, (int)((Diameter/2)*0.6), -(int)((Diameter/2)*0.6) };
int[] xpoints = {0,0,0};
int[] ypoints = {0,0,0};

double sin = Math.sin(direction);
double cos = Math.cos(direction);

for(int tripoint = 0;tripoint < 3;tripoint++){
xpoints[tripoint] = (int)x + (int)(cos * xs[tripoint] + -sin * ys[tripoint]);
ypoints[tripoint] = (int)y + (int)(sin * xs[tripoint] + cos * ys[tripoint]);
}

g.setColor(color);
g.fillPolygon(xpoints,ypoints,3);
g.drawPolygon(xpoints,ypoints,3);```
getBounds for the polygon:
Java Code:
```public Rectangle getBounds() {
return new Rectangle(x+(int)((Diameter)*1.4),y+(int)((Diameter)*0.6),(int)((Diameter)*1.4),(int)((Diameter)*0.6));
}```

2. The Polygon class api shows a method
Java Code:
`boolean intersects(Rectangle2D r)`
that might be helpful.

3. Member
Join Date
Jan 2009
Posts
3
Rep Power
0
thanks for the reply!
My problem is how to get the bounds of the polygon..
I know about intersects method and i used it in my code but sometimes it doesn't "see" that there is a collision between the rectangle and the polygon.So I assumed there's something wrong with the polygon's getbounds method..
Last edited by marina; 01-27-2009 at 09:46 PM.

4. Using the bounds of a polygon to test for intersection is using the Rectangle intersects method. Bounds => Rectangle. Using the Polygon intersects method is a different thing. To demonstrate the difference:
Java Code:
```import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PolygonIntersection extends JPanel {
Polygon polygon;
Rectangle rect;
boolean selected = false;
boolean useBounds = false;

public PolygonIntersection() {
int[][] pts = {
{ 125, 176, 73 }, { 40, 130, 130 }
};
polygon = new Polygon(pts[0], pts[1], 3);
rect = new Rectangle(100, 150, 175, 75);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Color color = selected ? Color.red : Color.blue;
g2.setPaint(color);
g2.draw(polygon);
g2.draw(rect);
g2.setPaint(Color.pink);
g2.draw(polygon.getBounds());
}

private void checkForIntersection() {
selected = useBounds ? polygon.getBounds().intersects(rect)
: polygon.intersects(rect);
repaint();
}

private JPanel getLastPanel() {
String[] ids = { "bounds", "polygon" };
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = e.getActionCommand();
boolean selected = ((JRadioButton)e.getSource()).isSelected();
useBounds = id.equals("bounds") && selected;
checkForIntersection();
}
};
ButtonGroup group = new ButtonGroup();
JPanel panel = new JPanel();
for(int i = 0; i < ids.length; i++) {
boolean b = useBounds ? i == 0 : i > 0;
rb.setActionCommand(ids[i]);
}
return panel;
}

public static void main(String[] args) {
PolygonIntersection test = new PolygonIntersection();
JFrame f = new JFrame("drag the polygon");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}

private MouseAdapter ma = new MouseAdapter() {
boolean dragging = false;
Point start;

public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if(polygon.contains(p)) {
start = p;
dragging = true;
}
}

public void mouseReleased(MouseEvent e) {
dragging = false;
}

public void mouseDragged(MouseEvent e) {
if(dragging) {
Point p = e.getPoint();
int x = p.x - start.x;
int y = p.y - start.y;
polygon.translate(x, y);
start.setLocation(p);
checkForIntersection();
}
}
};
}```

5. Member
Join Date
Jan 2009
Posts
3
Rep Power
0
thanks very much for the reply!!I 'll study that code and hope to solve my problem..

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•