0

I have a view where I have drawn a path on the canvas. The path drawn contains the x and y coordinates to draw the shape. The shape has 2 diagonal sides. What I need to do is to detect when a user touches either one of these diagonal sides. I am able to detect when a user touches a vertical and horizontal side of the shape but I am having trouble finding out how to do the same for the diagonal lines in a path.

I was able to determine whether a horizontal or vertical line was touched just by simply knowing the x and y values of each end of the line and checking if the user touched the screen within these values. However, the same approach cannot be done for a diagonal line so I am wondering how I would approach this.

Here is an image of the shape I have drawn with its coordinates:

Image of Shape

onTouchEvent Method:

@Override
public boolean onTouchEvent(MotionEvent ev) {
                //The Top is touched
                if ((ev.getX() >= x1 && ev.getX() <= x2 && ev.getY() >= y1 && ev.getY() <= y1)) {
                    Toast.makeText(this.getContext(), "Top Touched", Toast.LENGTH_SHORT).show();
                }
                //The Bottom is touched
                else if ((ev.getX() >= x4 && ev.getX() <= x3 && ev.getY() >= y3 && ev.getY() <= y3)) {
                    Toast.makeText(this.getContext(), "Bottom Touched", Toast.LENGTH_SHORT).show();
                }
                //The Left Side is touched
                else if ((ev.getY() >= y2 && ev.getY() <= y3 && ev.getX() >= x4 && ev.getX() <= x4)) {
                    Toast.makeText(this.getContext(), "Left Side Touched", Toast.LENGTH_SHORT).show();                       
                }
                //The Right Side is touched
                else if ((ev.getY() >= y2 && ev.getY() <= y3 && ev.getX() >= x3 && ev.getX() <= x3)) {
                    Toast.makeText(this.getContext(), "Right Side Touched", Toast.LENGTH_SHORT).show();
                    }
3
  • the diagonal is inside a square or could be the diagonal of a rectangle too?
    – MatPag
    Jan 13, 2018 at 15:57
  • Check the image I posted. I basically need to know if the user touches either of the 2 diagonal sides of the shape. Jan 13, 2018 at 16:03
  • I had seen it, but a diagonal is a line which connects 2 vertices of a polygon with 4 or more sides. Even if you don't see them, they are there
    – MatPag
    Jan 13, 2018 at 16:09

1 Answer 1

1
} else if ((ev.getX() < x1 && ev.getY() < y2) && 
    (ev.getX() - x4 = (ev.getY - y1) * ((x1-x4)/(y2-y1))) {
    Toast.makeText(this.getContext(), "Left Top 
        Touched",Toast.LENGTH_SHORT).show();
} else if ((ev.getX() > x2 && ev.getY() < y2) &&
    (ev.getX() - x2 = (ev.getY - y1) * ((x3-x2)/(y2-y1)) {
    Toast.makeText(this.getContext(), "Right Top Touched", 
        Toast.LENGTH_SHORT).show();
}
1
  • Thanks for the answer. Didn't directly solve my problem, but it helped me find a solution to solve it. Jan 14, 2018 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.