Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!eru!luth!sunic!lth.se!newsuser From: sund@tde.lth.se (Lars Sundstr|m) Newsgroups: comp.sys.mac.programmer Subject: Re: How do you grab lines like MacDraw? Message-ID: <1990Aug10.085010.18302@lth.se> Date: 10 Aug 90 08:50:10 GMT References: <4176@dogie.macc.wisc.edu> Sender: newsuser@lth.se (LTH network news server) Organization: Dept. of Applied Electronics, Lund University, Sweden Lines: 46 In article <4176@dogie.macc.wisc.edu> yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) writes: >In programming a MacDraw style environment where the >user can click-n-drag objects around, it's ez enuf to grab >objects with mass like squares and rectangles. You just see if >the clicked on pixel is in any of the regions defined by the >objects, i.e., you do a "PtInRgn()" call. > >But what if the object is a line? Can a region be defined to >be a line, w/no thickness? I'm posting this for a friend who >said he tried creating a region w/only a line in it and >subsequent PtInRgn calls would fail... any ideas? > >>>> yahnke@macc.wisc.edu <<< It's possible to calculate the distance between the line and the click point by using the vector product of the line and the vector between the click point and a endpoint of the line. Define A=(Ax,Ay) as a vector between the startpoint and endpoint of the line and B=(Bx,By) as a vector between the startpoint of the line and the clickpoint. Now, there is a nice relationship that can be used: |A x B| = |A| |B| sin(alfa) The angle alfa is the angle between the vectors and therefore, the distance between the line and the clickpoint is |B| sin(alfa). But according to the relationship above |B| sin(alfa) is equal to |A x B| / |A| which is pretty simple to calculate because |A x B| = abs(Ax By + Ay Bx) and |A| = sqrt(sqr(Ax) + sqr(Ay)) Because you get the distance to the line you can define your own click margin. With the formulas above you get away with 4 multiplications, 1 division, 2 additions and and square root. If you think the square root eats to much computer power then square both the distance and the click margin. I think that this approach saves both computer power and memory compared to the Point-In-Region-approach. Lars