Clicking lines

Discussion in 'Mac Programming' started by ATG, Nov 29, 2006.

  1. ATG macrumors regular

    Joined:
    Aug 7, 2005
    #1
    How is the best way to know when the user clicks on a line. For instance, in a drawing app the user might draw a line and click on it to select it.

    Sorry if I'm not making myself clear, it's been a long day. ;)
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    How are you drawing the lines? Straight or curved? If this is in Cocoa you can implement event handlers to get mouse click events, convert the coordinates from window to view and then use some maths to check the coordinates against the line coordinates....
     
  3. ATG thread starter macrumors regular

    Joined:
    Aug 7, 2005
    #3
    Cocoa, and they are straight.

    However, my lines can have multiple (straight) segments so how would I handle that best? Do I have to loop through every segment of every line? That would take ages!
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes, although the normal way to speed this up would be to use a bounding box calculation to quickly check if the pointer is even close to the line: basically work out a rectangle that will contain the line then check if the pointer is in that. This should be fast enough to be close to "free". If you have a concept of z so you want to select the topmost line it should be reasonably easy to build a small array of lines that the pointer could have clicked and iterate down them in z order. Unless you have millions of lines to check you should be able to do this quickly.
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    How many line segments are you anticipating? Looping through a couple hundreds segments would be instantaneous on any computer made in the last ten years.
     

Share This Page