# colllision rectangle-polygon

• 01-27-2009, 08:02 PM
marina
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():
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?
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:
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));       }```
• 01-27-2009, 09:00 PM
hardwired
The Polygon class api shows a method
Code:

`boolean intersects(Rectangle2D r)`
• 01-27-2009, 09:24 PM
marina
```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);         addMouseListener(ma);         addMouseMotionListener(ma);     }     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;             JRadioButton rb = new JRadioButton(ids[i], b);             rb.setActionCommand(ids[i]);             rb.addActionListener(al);             group.add(rb);             panel.add(rb);         }         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.add(test);         f.add(test.getLastPanel(), "Last");         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();             }         }     }; }```