Answer the question
In order to leave comments, you need to log in
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);
public class Edge {
Vertex v1;
Vertex v2;
}
public class Vertex {
float x;
float y;
}
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");
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question