Trying to get some help on this concept

Hey Guys… I’m working on an app for a client that wants a system in place where the user can draw a line and check it against some coordinates already hard coded into the scene. My question is this: how can I compare the coordinates in a table of the drawn line to the hard coded ones if they are varying quantities and placements. For example, if I draw the line faster I will end up with less coordinates versus if I draw slower. How do I compare when there is this disparity?

Ideas?

Thanks [import]uid: 10361 topic_id: 12099 reply_id: 312099[/import]

Look at the code exchange sample, this should help you.

http://developer.anscamobile.com/code/draw-line

i just downloaded it myself, its nice.

then compare your .x and .y positions and you should be well on your way.
[import]uid: 11860 topic_id: 12099 reply_id: 44107[/import]

I’m sorry i forgot to mention that it’s not just a “line.” It’s a series of lines. It would have been more accurately spelled out as “Points.” I’ve sought for help on this 3 times in the past week on the forums here and haven’t really gotten any responses.

I do have the line code in place. I’ve been looking/working with the point reduction code and the two main line drawing game samples.

I just don’t know how to handle/compare the differing numbers of points in the table from what the user draws to what is hard coded into each shape on screen. [import]uid: 10361 topic_id: 12099 reply_id: 44130[/import]

Ok… So this is my start. It’s a little clunky, but i’m left at the end of the “drawing” with my hard-coded points table telling me if they were “matched” or not.

Is there a better way to accomplish this?

Also, the end result is to try and give the user visual feedback that they are tracing the line properly by increasing the alpha level of letter (in this case it’s a “c”) they are tracing.

I’ve tried a number of ways to do this, but it seems like the most accurate is to just do it upon completion of the “trace” or when the user lifts their finger. (in the code below I don’t have this implemented)

Any ideas on how to make this work/better?

[code]
local function main()

– Constants-

local FALSE = 0
local TRUE = 1
local moved = FALSE;
local cLi – CURRENT LETTER IMAGE
local accuThresh = 15
local matches
local distanceBetween1

local function distanceBetween( obj1, obj2 )
local xfactor = obj2.x - obj1.x
local yfactor = obj2.y - obj1.y
distanceBetween1 = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
–print(“Distance =” … distanceBetween1)
return distanceBetween1

end


– Display Group

local background = display.newGroup()
local drawDotsGroup = display.newGroup() ; drawDotsGroup.alpha = .2 --DISPLAY ALPHA
local startEndPoints = display.newGroup() ; startEndPoints.alpha = .2


– Display Objects

local backdrop = display.newImageRect(“assets/green_chalk_bkg.png”, 1024,768);
backdrop.x = _W/2 ; backdrop.y = _H/2
background:insert(backdrop)

local upperCimage = display.newImageRect(“test_c.png”, 256,256)
upperCimage.x = _W/2 ; upperCimage.y = _H/2 ; upperCimage.alpha = .2
cLi = upperCimage


– LETTER TABLES

local cTable = {

{ x = cLi.x+70, y = cLi.y-90, match = 0 },
{ x = cLi.x+44, y = cLi.y-105, match = 0 },
{ x = cLi.x, y = cLi.y-110, match = 0 },
{ x = cLi.x-55, y = cLi.y-90, match = 0 },
{ x = cLi.x-82, y = cLi.y-25, match = 0 },
{ x = cLi.x-78, y = cLi.y+40, match = 0 },
{ x = cLi.x-55, y = cLi.y+85, match = 0 },
{ x = cLi.x-15, y = cLi.y+108, match = 0 },
{ x = cLi.x+35, y = cLi.y+94, match = 0 },
{ x = cLi.x+60, y = cLi.y+70, match = 0 },

}

for i,v in ipairs(cTable) do print(i,v.y) end

local function drawCinvisibles()
for i=1, #cTable do
cZoneStart = display.newCircle(cTable[i].x, cTable[i].y ,10) ;

end
end

–drawCinvisibles() --< Uncomment to draw match points


– Fade In Effect for current letter

local function isCorrect(obj)
transition.to(obj, {time = 1000, alpha = 1})
end

– Try Again

local function tryAgain()
letterFail = display.newText(“Try Again”, _W/2, _H/2, “Helvetica”, 76)
end


– Draw Points by User

local linePoints = {} – TABLE TO HOLD DRAWN LINE DOTS

local function createPoints(event)
local phase = event.phase
if “began” == phase then
matches = 0
local pt = {}
pt.x = event.x;
pt.y = event.y;
table.insert(linePoints,pt);
elseif “moved” == phase then
local pt = {}
pt.x = event.x;
pt.y = event.y;
table.insert(linePoints,pt);
moved = TRUE;

for i = 1, #linePoints, 1 do --Iterate through drawn points

vpt = display.newRect(linePoints[i].x,linePoints[i].y,6,6)

for h = 1, #cTable, 1 do

if(linePoints[i] and cTable[h] ~= nil) then --Find Distance
distanceBetween(linePoints[i], cTable[h])
end

if (distanceBetween1 < accuThresh) then
cTable[h].match = 1 --Matched per Hard-coded Point
matches = matches + 1 --Total matches

end

–[[if(distanceBetween1 < 30) then
print(distanceBetween1)
end]]–
end

end

elseif “ended” == phase or “cancelled” == phase then
moved = FALSE;

–print("total matches " … matches)

for i,v in ipairs(cTable) do print(i,v.match ) end --Show which points hardcoded were matched

–[[local hardCodeMatches = 0
for i = 1, #cTable, 1 do
if cTable[i].match == 1 then
hardCodeMatches = hardCodeMatches + 1
end
end
print("cTable matches " … hardCodeMatches)

if hardCodeMatches == 10 then
isCorrect(cLi)
elseif hardCodeMatches < 10 then
tryAgain()
end]]–

end

return true

end

Runtime:addEventListener(“touch”,createPoints);
end

main();
[/code] [import]uid: 10361 topic_id: 12099 reply_id: 45044[/import]

For the visual feedback portion of your question, have you considered using image masks? As each section is “matched”, you could fade in a mask to obscure that portion of the “dark” image, revealing the “lit” image below. [import]uid: 27119 topic_id: 12099 reply_id: 45072[/import]

Hey thanks for dropping in!
I don’t really need to break up each portion of the letter visually. They just want it to slowly fade in a percentage as the user completes more of the trace.

Masking might be a bit overkill. [import]uid: 10361 topic_id: 12099 reply_id: 45074[/import]

For your comparison question, I would identify key parts of each line such as the start, the end, and possibly the middle, then do the comparison with only these. [import]uid: 6787 topic_id: 12099 reply_id: 45122[/import]

@Snarla, Since these aren’t merely lines but actual letters of the alphabet, they need multiple hard-coded points. This was actually a suggestion by Ansca Staff to begin with. I was merely looking for a little help on better implementing this concept. It functions now as it is; I was hoping for some optimization tips. Plus the factor that the realtime visual feedback is something that I am not yet sure how to implement. [import]uid: 10361 topic_id: 12099 reply_id: 45231[/import]

@Snarla or anyone

This is what was suggested to me via Ansca Staff:

What I would do is have a set number of “matches”, and also a threshold so that if a point is close enough, it would be recognized as a match. Then, I’d loop through all the drawn points (since there are more), and check and see how many matches there are. Once it reaches the set number of matches, you could then break the loop and assume the user had traced their letter correctly.

The problem is, I need to perform this not just at the end of the user “trace.” I need it to happen while the user is dragging their finger so that I can fade the alpha in on the letter. I have the fade in code working but it’s clunky and slow on the device. I would sure appreciate some assistance making this function better.

BTW: I have hardcoded 10 points for the letter C which is my test subject for the entire code here. (cTable contains those coords)
[import]uid: 10361 topic_id: 12099 reply_id: 45460[/import]