I am trying to make an app which involves user drawing on the canvas.
I want to identify if the user draws a bounded shape ( like, “O”) or an unbounded shape (like, “U”).
This is how the drawing is implemented.
local function myTouchListener( event ) local tempGroup = display.newGroup() if ( event.phase == "began" ) then coorX = event.x coorY = event.y print( "object touched = "..tostring(event.target) ) elseif ( event.phase == "moved" ) then local paint = { 1, 0, 0.5 } s = s+1 --initially -1 strokeCircle[s] = display.newCircle(event.x,event.y,3) --circle shape used as brush stroke strokeCircle[s].fill = paint tempGroup:insert(strokeCircle[s]) --Finding the circumference, aka the length of outline of shape. distanceNew = math.sqrt(math.pow((event.x - coorX),2) + math.pow((event.y - coorY),2) ) print("distanceOld,distanceNew,distance"..distanceOld..","..distanceNew..","..distance) if((distanceOld) \<= (distanceNew)) then distance = distanceNew farthestPtX = event.x farthestPtY = event.y distanceOld = distanceNew end print( "touch location in content coordinates = "..event.x..","..event.y ) elseif ( event.phase == "ended" ) then print("farthestPtX , farthestPtY"..farthestPtX..","..farthestPtY) print("coorX , coorY"..coorX..","..coorY) centerX = (farthestPtX + coorX) /2 centerY = (farthestPtY + coorY) /2 print("Center is "..centerX..","..centerY) --finds the centre point of shape drawn by user. for x = 1,s do for y = 1, s do if ( strokeCircle[y] and hasCollidedCircle( strokeCircle[x],strokeCircle[y]) ) then print("It is a Circle") end end end radius = math.sqrt(math.pow((farthestPtX - coorX)/2,2) + math.pow((farthestPtY - coorY)/2,2) ) local paint = {0,0,1} local centerCircle = display.newCircle(centerX,centerY,5) centerCircle.fill = paint circle\_autoComplete(centerX,centerY,radius) --replaces the outline drawn by user as a circle print( "touch ended on object "..tostring(event.target) ) --Remove the user drawn outline for x =0,table.maxn(strokeCircle ) do display.remove(strokeCircle[x]) end s = -1 end return true --prevents touch propagation to underlying objects end
My goal is to replace the outline drawn by user with a circle (the circle_autocomplete function) only if the outline is a bounded shape(like O, D).
Right now, the code calls the circle_autocomplete() function even if the user draws an unbounded shape (like I, U, C) etc.
I tried to use a collision function, like if the distance between two circles eg, Distance(strokeCircle[x], strokeCircle[x+1]) < circleSize, then there is an overlap or collision. But this did not work, as if the user draws slowly nearby circles overlap even while drawing. (See image).
How do I detect if the user has drawn a bounded or unbounded shape from their stroke ?