如果两个手势笔划相交,如何识别点

时间:2012-06-15 07:24:23

标签: android android-gesture line-intersection

我正在开发一款游戏。在我的游戏中,当两条手势线相交时,游戏将结束。如何识别两条手势线的点交点?

我的代码在

下面
public class GameActivity extends Activity implements OnTouchListener 
{
    float temp=0,temp1=0;
    GestureOverlayView gstr;
    ArrayList<String> arr,prmt;
    View view;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gestureoverlay);


        prmt=new ArrayList<String>();
        arr=new ArrayList<String>();
        gstr=(GestureOverlayView) findViewById(R.id.GestureView);


        gstr.setOnTouchListener(this);


    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(MotionEvent.ACTION_MOVE==event.getAction())
        {
        Log.i(" "+event.getX()+""+event.getY(),"state");
        if(temp!=0 && temp1!=0)
        {
            if(temp<event.getX() && temp1<event.getY())
            {
                if(event.getX()>=event.getY())
                {
                    for(float i=temp;i<=event.getX();i++)
                    {
                        if(temp1<=event.getY())
                        {
                            temp1++;
                        }
                        if(!arr.contains(i+"  "+temp1))
                        {
                            arr.add(i+"  "+temp1);
                        }
                    }
                }
                else
                {
                    for(float i=temp1;i<event.getY();i++)
                    {
                        if(temp<=event.getX())
                        {
                            temp++;
                        }
                    if(!arr.contains(temp+"  "+i))
                    {
                            arr.add(temp+"  "+i);
                        }
                        Toast.makeText(getApplicationContext(), "y is high",50).show(); 
                    }               }
            }
            else if(temp>event.getX() && temp1>event.getY())
            {
                if(event.getX()<=event.getY())
                {
                    for(float i=temp;i>=event.getX();i--)
                    {
                        if(temp1>=event.getY())
                        {
                            temp1--;
                        }
                        if(!arr.contains(i+"  "+temp1))
                        {
                            arr.add(i+"  "+temp1);
                        }
                    }
                }
                else
                {
                    for(float i=temp1;i>event.getY();i--)
                    {
                        if(temp>=event.getX())
                        {
                            temp--;
                        }
                        if(!arr.contains(temp+"  "+i))
                        {
                            arr.add(temp+"  "+i);
                        }
                        Toast.makeText(getApplicationContext(), "y is high",50).show(); 
                    }
                }
            }

            else
            {
                if(!arr.contains(event.getX()+"  "+event.getY()))
                {
                        arr.add(event.getX()+"  "+event.getY());
                    if(!arr.contains((event.getX()-1)+"  "+event.getY()))
                        arr.add((event.getX()-1)+"  "+(event.getY()));              
                    if(!arr.contains((event.getX()+1)+"  "+(event.getY())))
                        arr.add(((event.getX()+1))+"  "+(event.getY()));
                }
            }
        }

        temp=event.getX();
        temp1=event.getY();
        tv.setText("x="+event.getX()+"  "+event.getY()+"\n");
        Log.i("x and y="+event.getX()+"  "+event.getY()+" "+arr, " ");
        //Toast.makeText(getApplicationContext(),event.getX()+"  "+(event.getX()+1), 40).show();
        }
        if(MotionEvent.ACTION_UP==event.getAction())
        {

            for(int i=0;i<arr.size();i++)
            {
                if(!prmt.contains(arr.get(i)))
                {
                    prmt.add(arr.get(i));
                    //arr.remove(i);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Game over",30).show();
                    Log.i("x and y=", "else");
                    Intent in=new Intent(getApplicationContext(),mainmenu.class);
                    startActivity(in);
                    finish();
                }
            }
            arr.clear();
            //Toast.makeText(getApplicationContext(), "action up",50).show();   
        }
        return false;
    }



}

但我无法获得所有像素(xy坐标)。因此,每当两条手势线相互交叉时,我就无法识别交点 请给我任何解决方案或任何其他方式来识别两条画线的共同点

0 个答案:

没有答案