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

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.

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.

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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();

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.

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.

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.

@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.

@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)

views:1544 update:2011/9/28 9:01:40
corona forums © 2003-2011