T
T
terminator-light2019-07-10 10:55:02
Java
terminator-light, 2019-07-10 10:55:02

How to detect tap by line drawn in Canvas Android?

I draw a line like this:

ePaint = new Paint();
ePaint.setTextSize(50);
canvas.drawLine(edge.v1.x, edge.v1.y, edge.v2.x, edge.v2.y, ePaint);

edge
public class Edge {
    Vertex v1;
    Vertex v2;
}

Vertex
public class Vertex {
    float x;
    float y;
}

So I'm trying to determine:
public static final int E = 20;

public boolean isPointOnLine(Edge edge, float px, float py) {
       if(px > edge.v1.x-E && px < edge.v2.x+E && py > edge.v1.y-E && py < edge.v2.y+E){
           return true;
       }else{
           Vertex v;
           v = edge.v1;
           edge.v1 = edge.v2;
           edge.v2 = v;
           return (px > edge.v1.x-E && px < edge.v2.x+E && py > edge.v1.y-E && py < edge.v2.y+E);
       }
    }

public void addWeight(float px, float py) {
        Log.d(TAG, "Point("+(int)px+","+(int)py+")");
        for(Edge edge: edges){
            if(isPointOnLine(edge, px, py) || isPointOnLine(edge, py, px)){
                Log.d(TAG, "found point: "+edge.toString());
                callback.onWeightChange(edge.weight);
                currentEditingEdge = edge;
                break;
            }else{
                Log.d(TAG, "not found");
            }
        }
    }

I tried to determine by building a rectangle, because the line has a thickness of 50.
The equation of a straight line by 2 points did not fit

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question