I am working on an application that draws letters on a canvas and detect if user traces the letter in the correct form. I tried different techniques and can't seem to find the best approach to cover all letters. Letters are in the comic sans font and due to formations in f and s it is pretty difficult. It's lowercase letters
Asked
Active
Viewed 1,162 times
2
-
1It describes how you can do it with gestures here. Maybe you will want to use the solution they are advertising: http://www.gesturekit.com/developing-gesture-different-mobile-platforms-cumbersome-gesturekit/ – geokavel Oct 30 '15 at 23:49
-
Thanks but gestures cannot work, simply because gestures cannot be measured against a path. Which is what I am trying to accomplish – B Days Oct 31 '15 at 02:45
1 Answers
0
There was a number of attempts to finally get the answer I needed thanks to this answer posted here: Path Measure example
This is what I did to accomplish the task:
Get the starting point of the path
Then I created a separate path that started at that point when the user touch was close to the start point (lets call this the trail path)
Finally I went on to calculate the point of the path that was slightly ahead of the user trail path and see if the difference in the touch and expected point was fairly close.
Here is a bit of code
Path Measure pm = new PathMeasure(myPath, false);
pm.getPosTan(0, floatPoints, null);
//This functions says whether the offset from path was too great to accept
private boolean isMajorOffset(float point1, float point2, int tolerance){
float difference = Math.abs(point1 - point2);
return tolerance < Math.floor(difference) && tolerance < Math.ceil(difference);
}
private void touch_start(float x, float y){
//get point as float
float floatPoints[] = {0f, 0f};
//get current start point
pm.getPosTan(0, floatPoints, null);
Point startPoint = new Point((int)floatPoints[0], (int)floatPoints[1]);
//if startPoint is selected then set path as started
if((startPoint.x >= x - TOUCH_TOLERANCE && startPoint.x <= x + TOUCH_TOLERANCE)
&& (startPoint.y >= y - TOUCH_TOLERANCE && startPoint.y <= y + TOUCH_TOLERANCE))
{
PathStarted = true;
Toast.makeText(this.getContext(), "Started", Toast.LENGTH_SHORT).show();
//move trail path to this point
trailPath.moveTo(startPoint.x, startPoint.y);
}
}
private void touch_move(float x, float y){
if(PathStarted==false){
return;
}
//get lenght of trail path
PathMeasure tm = new PathMeasure(trailPath, false);
//get point as float
float floatPoints[] = {0f, 0f};
//get current start point
pm.getPosTan(tm.getLength() + 1, floatPoints, null);
Point point = new Point((int)floatPoints[0], (int)floatPoints[1]);
//if offset is ok continue with trail path
if(!isMajorOffset(point.x, x, TOUCH_TOLERANCE) && !isMajorOffset(point.y, y, TOUCH_TOLERANCE))
{
//move current path to this point
trailPath.lineTo(point.x, point.y);
}
else {
PathStarted = false;
Toast.makeText(this.getContext(), "Ended", Toast.LENGTH_SHORT).show();
}
}
Hope this helps someone!