you can construct a rectangle just contains the line. So rect has same length with line, and height/weight is 3 pixels. ........ ...
.------. .|.
........ .|.
...Polygon rect=new Polygon(int[] xpoints,int[] ypoints);
rect.contains(x,y) to determine if the line has been selected.int[] xpoints and int[] ypoints should be created carefully, as the line can has different directions.
.------. .|.
........ .|.
...Polygon rect=new Polygon(int[] xpoints,int[] ypoints);
rect.contains(x,y) to determine if the line has been selected.int[] xpoints and int[] ypoints should be created carefully, as the line can has different directions.
// Line2D.Double templine = line;
// if (line.x1 == line.x2) {
// templine = new Line2D.Double(line.x1 + 3, line.y1, line.x2 - 3,
// line.y2);
// }
// if (line.y1 == line.y2) {
// templine = new Line2D.Double(line.x1, line.y1 + 3, line.x2,
// line.y2 - 3);
// }
return getBounds(templine.getBounds());
}注释部分为在直线的时候,返回偏移量!
这个点中的范围大,且简单!但是还是不购准确!
* 判断一个点是否在某个链接线上
* @see inforway.topo.NetNode#isPoint(java.awt.Point)
*/
public boolean isPoint(Point point) {
//得到两点之间的距离的一半
double a=(Math.sqrt((pointf.x-pointl.x)*(pointf.x-pointl.x)+
(pointf.y-pointl.y)*(pointf.y-pointl.y)))/2;
double c=Math.sqrt((pointf.x-point.x)*(pointf.x-point.x)+
(pointf.y-point.y)*(pointf.y-point.y));
double b=Math.sqrt(c*c-a*a);
if(b<=2){ //这个值2可以选择点与直线的距离
return true;
}
else{
return false;
}
}
过一会结贴给分,谢谢大家!public boolean lineHit(int x,int y)
{
for(int i=0;i<numOfNodes;i++)
for(int j=0;j<numOfNodes;j++)
{
double x1,x2,y1,y2,k;
x1=node[i].x;
x2=node[j].x;
y1=node[i].y;
y2=node[j].y; double a=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double c=Math.abs((y2-y1)*x-(x2-x1)*y+(x2-x1)*y1-(y2-y1)*x1);
double b=c/a; if((weight[i][j]>0)&&(b<=3))
{
return true;
} }
return false;
}