[comp.sys.mac.programmer] Selecting objects in a drawing program

housen@ssc-vax.UUCP (Kevin Housen) (06/10/88)

Im writing a program which will have certain aspects of
an object-oriented drawing program, like MacDraw or
MacDraft.  The user will be able to draw some primitives
like lines, arcs, circles, rects etc. and will be
able to select objects and edit them. There could be lots
of objects (>1000?).  My question is - does anyone know of
an efficient algorithm for detecting which object, if any,
was selected when a mouse-down occurs?  How do programs such
as MacDraw, MacDraft, SuperPaint, etc do this?

Also, what criteria do these programs use to determine when
a polyline or polygon is selected?  MacDraw and SuperPaint
highlite the object when you click within a certain distance
of one of the lines comprising an object.  MacDrafts criterion
isnt obvious.

Any help or suggestions are appreciated.

Thanks heaps...

Kevin Housen

mikem@uhccux.uhcc.hawaii.edu (Mike Morton) (06/18/88)

About a week back (sorry, I'm behind), Kevin Housen asked:
 >does anyone know of
 >an efficient algorithm for detecting which object, if any,
 >was selected when a mouse-down occurs?  How do programs such
 >as MacDraw, MacDraft, SuperPaint, etc do this?

As I understand it, MacDraw uses the following nifty trick:
(1) Change your screen-redraw code to optionally draw everything in black.
    You can do this by wrapping an if-then-else around each QuickDraw call,
    or more elegantly by replacing the low-level drawing routines which
    draw black.
(2) When seeking which object was hit by a mousedown, create an offscreen
    bitmap of any size and clip the drawing to a 1-by-1 rectangle which
    is where the mousedown occurred.  Then do a SetPortBits to this bitmap
    and call your screen-redraw code.  Draw your objects front-to-back and,
    after drawing each one, check if the pixel got blackened.  When it the
    check sees that it blackened, the most recently drawn object is the
    select one.

This is quite fast since QuickDraw will clip nearly all the drawing operations
to nothing.  I haven't used this method, but a former co-worker said it
worked very well.

 >Also, what criteria do these programs use to determine when
 >a polyline or polygon is selected?  MacDraw and SuperPaint
 > highlite the object when you click within a certain distance
 >of one of the lines comprising an object.  MacDrafts criterion
 >isnt obvious.

I suspect you can add a tolerance by changing your bitmap and clipped area
to be larger than one pixel, and watching for anything NEAR the mousedown
to turn black.  In this case, you won't want to stop for the first object;
other objects may fall even closer, so you have to keep a running minimum.

Anyone know an easy way to use the above method for text?  Ie, how can I
change the standard text-drawing procedure to draw solid black characters
instead of the actual letters from the font?

 -- Mike Morton // P.O. Box 11378, Honolulu, HI  96828, (808) 456-8455 HST
       for now: mikem@uhccux.uhcc.hawaii.edu
  someday soon: msm@ceta.ics.hawaii.edu
    (anagrams): Mr. Machine Tool; Ethical Mormon; Chosen Immortal; etc.