Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android tracking x,y coordinates draw from Path

I am trying to develop an android application (targeting versions 2.1/2.2) which requires the tracking of Path x,y coordinate points and am having a hard time. The basics of the app are similar to FingerPaint, except first a separate thread draws some lines using a Path drawable onto a canvas, then the user tries to draw over those lines, covering them completely with a different Paint and Path object. The user will have a larger Paint brush than the computer. The first auto drawn lines use a separate thread similar to LunarLander example app. Since the lines are not just straight lines I'm struggling finding an approach to do the following: 1. Track the auto points draw. I realize I'm providing the basic points but using path.lineTo and path.arcTo and path.quadTo to really figure out all the points.

  1. Once I know all the points the auto path drew, monitor and detect when user has drawn over all those points and "wins" that round.

My ideal method which obviously doesn't exist would be something like autoPath.getDrawnPoints().coveredBy(userPath.getDrawnPoints()) == true ? Any help with this would be greatly appreciated, thank you. -Ben

like image 214
Benjamin Avatar asked Nov 06 '22 07:11

Benjamin


1 Answers

All I can say is: Path relies on GeneralPath of Java which relies on Path2dFloat.

If I look at the sourcecode it has a packageReadable transient float floatCoords[]; So the only way I see to get the coordinates you pass in back is more a hack than a solution. Anyway, here it is:

public float[] getPointsFromPath(GeneralPath path){
    Class<?> clazz = path.getClass();
    while(true){
        clazz = clazz .getSuperclass();
        if (clazz ==  Path2D.Float.class){
            break;
        }
    }
    try {
        Field f = clazz.getDeclaredField("floatCoords");
        if (f != null){
            if (!f.isAccessible()){
                f.setAccessible(true);
            }
            Object o = f.get(path);
            if (o != null && o instanceof float[]){
                float[] drawnPoints = (float[])o;
                Log.d("TAG", drawnPoints);
                return drawnPoints;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
}
like image 88
Rafael T Avatar answered Nov 11 '22 13:11

Rafael T