Mehul Santoki
Mehul Santoki

Reputation: 1208

android line intersect with itself

enter image description here

Hello friends,

I draw a line using canvas.drawPath(mPath, mPaint); method. but having some problem, i want to get whether line intersect with itself or not. so please help.

thanks in advance

Upvotes: 1

Views: 852

Answers (3)

mpellegr
mpellegr

Reputation: 3182

I think you're best bet would be to:

1) Keep one path representing what's currently on the canvas, and every time you use canvas.drawPath(nextPath), also add it to your global path and do something like globalPath.addPath(nextPath)

2) Take a look at this post: Collision detection with bitmaps on SurfaceView's canvas in Android. It seems that you should be able to compare the globalPath to the nextPath and tell if they ever collide.

3) If instead you wanted to just know if a single path collides with itself and don't care about adding new paths or anything, we'd need more information on how you're drawing this path. With lines, arcs, circles??

Upvotes: 1

appsroxcom
appsroxcom

Reputation: 2821

For better performance and UI response create a separate Thread with a Handler and Looper. Also use a HashSet to store the path which tells you of an intersection when you add a position since add() returns a boolean.

Here is some code which should hopefully give better performance.

public class MainActivity extends Activity {

    private Handler mHandler;
    private String position, lastPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Tracker t = new Tracker();
        t.start();
    }

    @Override
    protected void onPause() {
        if (mHandler != null) 
            mHandler.getLooper().quit();
        super.onPause();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            position = event.getX()+","+event.getY();
            if (position.equals(lastPosition)) break; 

            if (mHandler != null) {
                Message msg = Message.obtain();
                msg.obj = position;
                mHandler.sendMessage(msg);
            }

            lastPosition = position;
            break;
        }
        return true;
    }

    private class Tracker extends Thread {
        HashSet<String> path = new HashSet<String>(); 

        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    String position = String.valueOf(msg.obj);
                    if (!path.add(position)) 
                        Log.w("Intersection", position);//Handle the intersection
                }
            };          
            Looper.loop();
        }
    }
}

Upvotes: 3

Akilan
Akilan

Reputation: 934

save the coordinates from events

    @Override

public boolean onTouchEvent(MotionEvent event) { event.getX();event.getY()`

}

to a Hashmap and compare

Upvotes: 2

Related Questions