473,394 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.             public void Move(int x_pos, int y_pos)
  2.             {
  3.  
  4.                 for (int myY = 0; myY<this.ypoints.length; myY++)
  5.                     this.ypoints[myY] = this.ypoints[myY] + (y_pos-this.y_position);
  6.                 for (int myX=0; myX<this.xpoints.length; myX++)
  7.                     this.xpoints[myX] = this.xpoints[myX] + (x_pos-this.x_position);
  8.                 this.x_position = x_pos;
  9.                 this.y_position = y_pos;
  10.             }
  11.  
Here is the code in the mouse press that sets the reference to the polygon I want to move:
Expand|Select|Wrap|Line Numbers
  1.                     for (PenroseShape p : pShapes)
  2.                     {
  3.                         System.out.println(p.x_position + "," + p.y_position);
  4.  
  5.                         if (p.intersects(r))
  6.                             {
  7.                                 Mover = p;
  8.                                 OriginalX = p.x_position;
  9.                                 OriginalY = p.y_position;
  10.                                 dragging=true;
  11.                                 System.out.println("Intersection Found");
  12.                             }
  13.                     }
  14.  
And here is the code during the drag that actually moves the polygon (works fine if p.intersects(r) above returns true:
Expand|Select|Wrap|Line Numbers
  1.                     if(dragging)
  2.                            {
  3.                               Mover.Move(OriginalX + (e.getX()-StartDragX),OriginalY+(e.getY()-StartDragY));                      
  4.                               System.out.println(Mover.x_position + "," + Mover.y_position);
  5.                               repaint();
  6.                            }
  7.  
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
Jan 25 '09 #1
8 2961
JosAH
11,448 Expert 8TB
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
Jan 25 '09 #2
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).
Jan 25 '09 #3
JosAH
11,448 Expert 8TB
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
Jan 25 '09 #4
The class PenroseShape extends Polygon and has 2 subclasses: kites and dart. Here is the PenroseShape class with its subclasses:
Expand|Select|Wrap|Line Numbers
  1.         class PenroseShape extends Polygon
  2.         {
  3.             public int rotation, x_position, y_position;
  4.             public Color pColor;
  5.             public PenroseShape(){}
  6.             public PenroseShape (int x_pos, int y_pos, int rotation)
  7.             {
  8.                 this.x_position=x_pos;
  9.                 this.y_position=y_pos;
  10.                 this.rotation=rotation;
  11.             }
  12.             public void Move(int x_pos, int y_pos)
  13.             {
  14.  
  15.                 for (int myY = 0; myY<this.ypoints.length; myY++)
  16.                     this.ypoints[myY] = this.ypoints[myY] + (y_pos-this.y_position);
  17.                 for (int myX=0; myX<this.xpoints.length; myX++)
  18.                     this.xpoints[myX] = this.xpoints[myX] + (x_pos-this.x_position);
  19.                 this.x_position = x_pos;
  20.                 this.y_position = y_pos;
  21.             }
  22.  
  23.         }
  24.         class Kite extends PenroseShape
  25.         {
  26.             public int rotation, x_position, y_position;
  27.             public Color pColor;
  28.  
  29.             public Kite(int x_pos, int y_pos, int rotation)
  30.             {
  31.                 pColor = Color.WHITE;
  32.                 this.x_position=x_pos;
  33.                 this.y_position=y_pos;
  34.                 super.addPoint(x_pos+0, y_pos+42);
  35.                 super.addPoint(x_pos+125, y_pos+1);
  36.                 super.addPoint(x_pos+250, y_pos+42);
  37.                 super.addPoint(x_pos+125,  y_pos+214);
  38.             }
  39.  
  40.         }
  41.         class Dart extends PenroseShape
  42.         {
  43.             public int rotation, x_position, y_position;
  44.             public Color pColor;
  45.             public Dart(int x_pos, int y_pos, int rotation)
  46.             {
  47.                 pColor = Color.BLACK;
  48.                 this.x_position=x_pos;
  49.                 this.y_position=y_pos;
  50.                 super.addPoint(x_pos+0, y_pos + 173);
  51.                 super.addPoint(x_pos+125, y_pos + 1);
  52.                 super.addPoint(x_pos+250, y_pos+173);
  53.                 super.addPoint(x_pos+125, y_pos+132);
  54.             }
  55.         }
  56.  
Jan 25 '09 #5
JosAH
11,448 Expert 8TB
@jpatchak
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.
Jan 25 '09 #6
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.
Jan 25 '09 #7
Never mind - I was updating the bounds incorrectly. Once I updated bounds correctly it worked! Thank you for your patience and your expertise!
Jan 25 '09 #8
JosAH
11,448 Expert 8TB
@jpatchak
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
Jan 26 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Abhishek | last post by:
My objective is quite simple. I want to display a JPG image and then on it overlay a polygon. This polygon can be moved by a user. The even is trapped on the mouse down event of the control. I can...
3
by: Merlin | last post by:
Design Problem =============== Would appreciate any help or suggestion on this design issue. I have spent a great deal of time and effort for an elegant solution but it seems I am not getting...
1
by: Jaime | last post by:
When I send a graph (polygon) to printer, I receive the next message: Object referente not set to an instante of an object.--- at System.Drawing.SafeNativeMethods.GdipFillPolygonI(HandleRef...
6
BSOB
by: BSOB | last post by:
im gone for a long weekend so plenty of time for anyone to answer. if i have 4 points representing a polygon and each point is represented by an x and a y coordinate, is there an easy (or slightly...
6
by: John Smith | last post by:
//Shrinks the specified polygon by one pixel (in any relevant direction) //so that the shrunken polygon sits exactly within the original (specifed) polygon. public void Point ShrinkPolygon(Point...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
4
by: canteyn | last post by:
Ok here is my problem. I am coding a program that uses 3 different functions, and the end result is that the program reads in (x,y) points from a file, and then outputs those points to another file,...
14
by: David Abrahams | last post by:
Here's an implementation of the functionality I propose, as a free-standing function: def intersects(s1,s2): if len(s1) < len(s2): for x in s1: if x in s2: return True else: for x in s2: if...
3
by: jojo41300000 | last post by:
Hi, Is anyone know that how to get the x and y points inside the polygon using C++ program? I have the given polygon data to draw the polygon, but i don't know how to get all the points...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.