Problem with Polygon intersects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpatchak
    New Member
    • Oct 2006
    • 76

    Problem with Polygon intersects

    Hello,

    I am having a problem using intersects method of a class I made that extends Polygon. Basically, I am trying to write code that allows the user to drag these polygons. I create a 1x1 rectangle on a mouse press and look through an array to see if the rectangle intersects the polygon. If it does, then I call the polygon's move class, passing the x and y coordinates while the user is dragging. Problem is that it works the first time, but the each time after that, the intersects method is more and more likely to return false. I even drew the rectangle and I can see that the rectangle is sitting right on top of these shapes. Here is the move method of the polygon:

    Code:
                public void Move(int x_pos, int y_pos)
                {
                	
                	for (int myY = 0; myY<this.ypoints.length; myY++)
                		this.ypoints[myY] = this.ypoints[myY] + (y_pos-this.y_position);
                	for (int myX=0; myX<this.xpoints.length; myX++)
                		this.xpoints[myX] = this.xpoints[myX] + (x_pos-this.x_position);
                	this.x_position = x_pos;
                	this.y_position = y_pos;
                }
    Here is the code in the mouse press that sets the reference to the polygon I want to move:
    Code:
                        for (PenroseShape p : pShapes)
                        {
                        	System.out.println(p.x_position + "," + p.y_position);
    
                        	if (p.intersects(r))
                                {
                                    Mover = p;
                            		OriginalX = p.x_position;
                                    OriginalY = p.y_position;
                                    dragging=true;
                                    System.out.println("Intersection Found");
                                }
                        }
    And here is the code during the drag that actually moves the polygon (works fine if p.intersects(r) above returns true:
    Code:
                    	if(dragging)
               				{
                  				Mover.Move(OriginalX + (e.getX()-StartDragX),OriginalY+(e.getY()-StartDragY));          			
                  				System.out.println(Mover.x_position + "," + Mover.y_position);
                  				repaint();
                       		}
    I am relatively new to Java and am trying to teach myself, so if I've left out anything that might be critical to anyone being able to help me, please let me know. This has been driving me nuts all night.

    Thanks,
    Josh
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What happens when you test it the other way around? i.e. r.intersects(p) . Also have a look at the contains() method.

    kind regards,

    Jos

    Comment

    • jpatchak
      New Member
      • Oct 2006
      • 76

      #3
      r.intersects(p) will not compile - tells me that the intersects method of a rectangular shape can't take p as an argument. I tried contains - created a Point2D at cursor on mouse press and then tested to see if p contains the point and got the same results as with p.intersects(r) .

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        What is a PenroseShape? Does it extend a Shape or is it another class? If so how is its intersects() method implemented?

        kind regards,

        Jos

        Comment

        • jpatchak
          New Member
          • Oct 2006
          • 76

          #5
          The class PenroseShape extends Polygon and has 2 subclasses: kites and dart. Here is the PenroseShape class with its subclasses:
          Code:
                  class PenroseShape extends Polygon
                  {
                      public int rotation, x_position, y_position;
                      public Color pColor;
                      public PenroseShape(){}
                      public PenroseShape (int x_pos, int y_pos, int rotation)
                      {
                      	this.x_position=x_pos;
                      	this.y_position=y_pos;
                      	this.rotation=rotation;
                      }
                      public void Move(int x_pos, int y_pos)
                      {
                      	
                      	for (int myY = 0; myY<this.ypoints.length; myY++)
                      		this.ypoints[myY] = this.ypoints[myY] + (y_pos-this.y_position);
                      	for (int myX=0; myX<this.xpoints.length; myX++)
                      		this.xpoints[myX] = this.xpoints[myX] + (x_pos-this.x_position);
                      	this.x_position = x_pos;
                      	this.y_position = y_pos;
                      }
          
                  }
                  class Kite extends PenroseShape
                  {
                      public int rotation, x_position, y_position;
                      public Color pColor;
          
                      public Kite(int x_pos, int y_pos, int rotation)
                      {
                          pColor = Color.WHITE;
                      	this.x_position=x_pos;
                          this.y_position=y_pos;
                      	super.addPoint(x_pos+0, y_pos+42);
                          super.addPoint(x_pos+125, y_pos+1);
                          super.addPoint(x_pos+250, y_pos+42);
                          super.addPoint(x_pos+125,  y_pos+214);
                      }
          
                  }
                  class Dart extends PenroseShape
                  {
                      public int rotation, x_position, y_position;
                      public Color pColor;
                      public Dart(int x_pos, int y_pos, int rotation)
                      {
                          pColor = Color.BLACK;
                      	this.x_position=x_pos;
                          this.y_position=y_pos;
                          super.addPoint(x_pos+0, y_pos + 173);
                          super.addPoint(x_pos+125, y_pos + 1);
                          super.addPoint(x_pos+250, y_pos+173);
                          super.addPoint(x_pos+125, y_pos+132);
                      }
                  }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by jpatchak
            The class PenroseShape extends Polygon
            I don't understand why r.intersects(p) fails to compile; both classes implement the Shape interface so that should work. Can you check that (see my previous replies)? There's something fishy going on ...

            kind regards,

            Jos

            ps. you should also upate the 'bounds' member of the parent class Polygon.

            psps. also see the translate() method in the Polygon parent class.

            Comment

            • jpatchak
              New Member
              • Oct 2006
              • 76

              #7
              I think that the reason that r.intersects(p) won't compile is that the intersects method of Rectangle2D needs type Rectangle2D for the argument - since p is not a Rectangle2D, it won't compile.

              Updated bounds in the move method of PenroseShape and no luck.

              I'm not getting what I should be doing with the translate method of Polygon here, so if you could give a little bigger clue, that would be helpful.

              Comment

              • jpatchak
                New Member
                • Oct 2006
                • 76

                #8
                Never mind - I was updating the bounds incorrectly. Once I updated bounds correctly it worked! Thank you for your patience and your expertise!

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by jpatchak
                  Never mind - I was updating the bounds incorrectly. Once I updated bounds correctly it worked! Thank you for your patience and your expertise!
                  Good; just in case you missed it: there's no need to do all those calculations yourself; the parent class implements the translate() method and the getBounds() method. The fist one translates the entire polygon while the second method produces an updated bounding box.

                  kind regards,

                  Jos

                  Comment

                  Working...