我有所有的点来画一条线并在画布上正确显示这些点。但是现在我想在用户触摸时在该现有线上画另一条线。我的问题是,如何找到用户触摸该线的内部或线外?
1 回答
I have a suggestion.
This is a thought based on Google Maps and Polylines but could should easily be adapted to a Canvas 2DLine situation too, I think.
Use this method to calculate the distance between two LatLng points:
/** distance in meters **/
private float distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
return (float) (dist * meterConversion);
}
When user clicks the canvas (for simplicity just imagine it has only one line):
- lineLength = Compute distance between the points (full length of the line)
- endToClickLength1 = Calculate distance from the click position to endpoint 1 of the line
- endToClickLength2 = Calculate distance from the click position to endpoint 2 of the line
Now, if endToClickLength1 + endToClickLength2 = lineLength (+/- acceptedError), then the user has clicked the line.
The acceptedError is needed to widen the boundary for when a lines is clicked a bit.
Do this for all the drawn lines to tell if one of the lines have been clicked.