Results 1 to 5 of 5
Thread: colllision rectangle-polygon
- 01-27-2009, 07:02 PM #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():
Am i able to do the same for the polygon (use getbounds) and then use the intersects method?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); }
getBounds for the polygon: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);
Thanks in advance!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)); }
- 01-27-2009, 08:00 PM #2
The Polygon class api shows a method
that might be helpful.Java Code:boolean intersects(Rectangle2D r)
- 01-27-2009, 08:24 PM #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 08:46 PM.
- 01-28-2009, 01:29 AM #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); 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(); } } }; }
- 01-28-2009, 09:14 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
polygon-shaped JComponent
By zenMarko in forum New To JavaReplies: 2Last Post: 11-04-2008, 06:06 PM -
Random Points inside a Polygon
By nidhirastogi in forum Advanced JavaReplies: 1Last Post: 09-23-2008, 03:28 AM -
How to Fill a Polygon in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:10 PM -
How to Draw a Polygon in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM -
help with rectangle class
By darkgt in forum New To JavaReplies: 7Last Post: 11-14-2007, 06:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks