Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ATG

macrumors regular
Original poster
Aug 7, 2005
187
0
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. ;)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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....
 

ATG

macrumors regular
Original poster
Aug 7, 2005
187
0
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!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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!

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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
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!

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.