New topic: More CurveShape Math :) [Solved...kind of]
<http://forums.realsoftware.com/viewtopic.php?t=47792> Page 1 of 1 [ 11 posts ] Previous topic | Next topic Author Message DaveS Post subject: More CurveShape Math :) [Solved...kind of]Posted: Sat May 04, 2013 7:47 pm Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA Ok..... assume I have a 2nd Order Curveshape (Order=1... ie a Quad Bezier Curve) There are Three Points P0, P1 and P2 where P0 and P1 are the end points, and P2 is the Control Point Now Given a 4th Point XY.... return the DISTANCE from XY to *ANY* point on the curve (I don't care which point, but if it is part of the solution I'll take it) What I am trying to do is with such a curve drawn on a Canvas (and knowing the Points that made it)... If I click the mouse near the curve am I "close enough" to consider having clicked on the curve (ie. +/- a few pixels) Selecting Circles, Squares etc are easy as they are 2D and you can apply a bounding rectangle... .but a Curve or a Line (yeah I need that too) are only 1D so its a bit trickier _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Last edited by DaveS on Sun May 05, 2013 10:22 am, edited 1 time in total. Top timhare Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 12:05 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 12295 Location: Portland, OR USA Take the chicken way out. Draw the curve into a picture and then test the pixel values near the XY to see if it's close enough to the line. It's probably faster even than the math involved. Certainly much easier on the brain. Top DaveS Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 12:13 am Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA Yeah.... Thought about that too..... but here is the problem (solvable.. but still a problem) The data points are stored at 100% scale.... but the CANVAS where the user clicks might be zoomed in 400% So there is the issue of translating from one scale to another.... and the Curve could be HUGE even at 100%... as there is nothing to stop them from creating a curve that covers the entire workarea .... So I'd have to figure out what "piece" was visible, what the scale factor was, and transform the mouse coordinates from the screen to the "test area" graphic. I can't do it on the screen, because it might be possible that they have draw a white curve over a white section, so the test area would have to be only black and white (as the viewport also has a grid etc) I actually used this technique in this same program to determine if the mouse click was inside a complex shape... but the shapes can be scaled to a known value as they are contrained to a rectangle of 100px per side. _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top timhare Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 12:25 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 12295 Location: Portland, OR USA You really only have to translate the mouse location to the test area. Apply the x/y offset to the upper/left corner of the visible area, then un-zoom back to 100%. Top DaveS Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 12:47 am Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA it might be faster to manually recalulate the points on the curve because it could be very possible as I mentioned before to have a curve defined as P0=0,1500, P1=1500,0, PC=0,0 at 100% scale that covers a rectangle 1500 x 1500 so my "test area" is as big as my work area in that case ... and while I am assuming your intent was to scan the RGBSURFACE looking for a black pixel, thats a lot of real-estate to cover Also another thing to consider... at 100% scale, I want to know if I'm like +/- 3 pixels from the line, so I guess I don't have to scan the WHOLE image. a horizontal vector at the MOUSEY , and the vertical vector at MOUSEX (tranformed values of course) Just wish I could do it mathematically, given the 3pts of the Curve, and the Mouse Point (all in the same scale, and offset) Another consideration is overall speed. The canvas could be covered in lines, curves and Group2D shapes, and when the user clicks the mouse, it needs to scan though the list of objects and find the one the is most fore-front and closest to the mouse click Think CAD type drawing... or more precisely Microsoft Visio _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top timhare Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 2:01 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 12295 Location: Portland, OR USA You don't have to scan the entire surface. Just the area in the rectangle that represents "close enough". Eg., 20 pixels on either side of the mouse. I've written CAD applications. What we did was divide up the work area into quadrants (and those quadrants into smaller quadrants, etc.) You assign each drawn object to the smallest quadrant that contains the entire shape. Any given point is in a small number of quadrants, so you only have to look at the shapes in those quadrants. You automatically exclude anything that is "far away", especially the pesky little shapes that proliferate in a drawing. At any given point, you only have to examine a small fraction of the number of objects. Edit: to clarify the concept of "quadrants" a bit more. With 3 levels of division, you have 21 "quadrants" Quadrant 0 = the whole screen Quadrants 1 - 4 = 1/4 the screen Quadrants 5 - 8 = 1/4 Quadrant 1 Quadrants 9 - 12 = 1/4 Quadrant 2 etc. A point inside quadrant 8, say, would be in 3 quadrants (one per level): Quadrant 8, Quadrant 1, and Quadrant 0. You only have to examine the objects in those 3 quadrants. A line that was in both quadrant 7 and 8 would be assigned to the next level up: Quadrant 1. A line that crossed the entire screen would be in Quadrant 0. Top kermit Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 3:51 am Joined: Mon May 30, 2011 12:56 am Posts: 679 Quote:The data points are stored at 100% scale.... but the CANVAS where the user clicks might be zoomed in 400% Cherry picking your information supplied here.. Canvas shows all current shapes, in a variety of colours, but since its also zoomable, it shows only a section of the available design area? A brute force answer: Get the x,y co-ords of the clicked pixel on screen. Create an off screen picture same size as the canvas. For each object create a new object2d in black zoom, scale, offset the same way as the onscreen canvas draw the object to the offscreen picture examine the pixel under the x,y co-ordinate if black, its a hit, exit for next and i'm sure that could be optimised. For example, if the object count was less than 255, you might draw them all at once, using the object index as the red value of the colour it gets drawn with Then look at the red value of the pixel at x,y, and that's the index of your selected object Top DaveS Post subject: Re: More CurveShape Math :)Posted: Sun May 05, 2013 9:03 am Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA My concern with all of these solutions is TIME... Kermit.... for 2D shapes I am doing precisely what you said... For a 2D (2 point) LINE. Calculating an intercept is fairly easy. It is a Quad Bezier curve that is thwarting me... Tim. Since I know the 3 point of the Curve. I can easily find a Rectangle that contains the curve (Min/Max of the X,Y of each point) and if the Mouse is NOT in the rectangle, I need go no further (I am going on the premise, that the user isn't going to choose the curve by clicking so close to an end point) I was hoping I could do "Given X (mouse) solve for Y value on curve)... but its a 2nd order equations.... meaning I could get TWO answers... And the fact is.... I only need to know if I'm +/- 3 pixels (regardless the scale) _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top DaveS Post subject: Re: More CurveShape Math :) [Solved...kind of]Posted: Sun May 05, 2013 10:26 am Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA A bit brute force.... but it seems fast enough The Array Xpos(),Ypos() already contain the points for the Curve Given a point in space (x1,y1) is it close enough to the curve (ie. +/- tolerace value) Note : This works ONLY for QUAD Bezier curves (ie. curveshape with an order of 1) FUNCTION Point_Close_to_Curve(x1 as integer,y1 as integer) as boolean Const tolerance=2 Dim xx As Integer Dim yy As Integer Dim t As Single Dim t2 As Single Dim tm As Single Dim tm2 As Single Dim t3 As Single Dim tt As Integer Dim flag As Boolean flag=False ' points 0/1 are end points , #2 is control point For tt=0 To 500 t=tt/500 t2=t*t tm=(1-t) tm2=tm*tm t3=tm*t*2 xx = tm2 * xpos(0) + t3 * ypos(2) + t2 * xpos(1) yy = tm2 * ypos(0) + t3 * ypos(2) + t2 * ypos(1) // stop if point on curve is within tolerance of mouse point If Abs(xx-x1)<=tolerance And Abs(yy-y1)<=tolerance Then flag=True Exit For End If Next tt Return flag END FUNCTION _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top kermit Post subject: Re: More CurveShape Math :) [Solved...kind of]Posted: Sun May 05, 2013 4:11 pm Joined: Mon May 30, 2011 12:56 am Posts: 679 Impressed Top DaveS Post subject: Re: More CurveShape Math :) [Solved...kind of]Posted: Sun May 05, 2013 4:18 pm Joined: Sun Aug 05, 2007 10:46 am Posts: 4845 Location: San Diego, CA kermit wrote:Impressed Thanks It is brute force.... but I'm not even noticing any real lag time (but only a few shapes on the screen) I even went so far as to use this exact algotrithm for "Close to a Line".... just make it a Curve Shape, with the control point at the bisection point of the line.. works perfectly _________________ Dave Sisemore iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 11 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
