a simple 2d question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dominik Kaspar

    a simple 2d question

    Hello

    I want to catch mouse clicks on drawn lines and was dumbfound by the
    Line2D.intersec ts method.

    My code looks like this:
    Line2D.Double l = new Line2D.Double(0 , 0, 100, 100);
    if(l.intersects (49, 49, 51, 51)) System.out.prin tln("1");
    if(l.intersects (35, 35, 37, 37)) System.out.prin tln("2");

    The line (0,0)-(100,100) definitely doesn't intersect with the
    rectangle (35,35)-(37,37). But why the hell does the above code give
    the output "1" and "2"?!

    I hope somebody can eliminate my confusion.
    Dominik
  • Dominik Kaspar

    #2
    Re: a simple 2d question

    Well, the last example were quite bad...
    How about the following one:

    Line2D.Double l = new Line2D.Double(1 20, 120, 240, 240);
    if(l.intersects (140, 190, 150, 200)) System.out.prin tln("1");

    Why does this result in the output "1"? The line l definitely doesn't
    intersects the square.

    Comment

    • Raymond DeCampo

      #3
      Re: a simple 2d question

      Dominik Kaspar wrote:[color=blue]
      > Well, the last example were quite bad...
      > How about the following one:
      >
      > Line2D.Double l = new Line2D.Double(1 20, 120, 240, 240);
      > if(l.intersects (140, 190, 150, 200)) System.out.prin tln("1");
      >
      > Why does this result in the output "1"? The line l definitely doesn't
      > intersects the square.[/color]

      Dominik,

      Are you reading the javadoc correctly? Note that Line2D.intersec t()
      expects the x-coord, y-coord, the width and the height of the rectangle
      (contrast with the Line2D.Double constructor). So the point (240,240)
      is inside your rectangle as:

      140 <= 240 <= 290 = 140+150 and
      190 <= 240 <= 390 = 190+200

      Ray

      Comment

      Working...