Java 如何检查点是否在对角线上?

Java 如何检查点是否在对角线上?,java,gwt,java-canvas,Java,Gwt,Java Canvas,我有一个画布,上面有行。在上单击我想检查单击是否在我的行上以突出显示它 我还有一些矩形,只需使用正方形的开始点和结束点就可以了。 但是对于一条对角线我不能使用与当然直线不能填充矩形相同的技术 但我还能怎样做到这一点呢? 此外,我还希望有一些“偏移”,这样,如果点击距离线足够近,它也会被标记,否则细线可能很难点击 可能我错过了正确的关键字,因为我肯定不是第一个想这么做的人。希望你能帮上忙。写出这行的方程式: a*x + b*y + c = 0 然后将单击的坐标放入此方程式: distance =

我有一个
画布
,上面有
。在
上单击
我想检查单击是否在我的行上以突出显示它

我还有一些
矩形
,只需使用正方形的
开始点
结束点
就可以了。 但是对于一条
对角线
我不能使用与当然直线不能填充矩形相同的技术

但我还能怎样做到这一点呢? 此外,我还希望有一些“偏移”,这样,如果点击距离线足够近,它也会被标记,否则细线可能很难点击


可能我错过了正确的关键字,因为我肯定不是第一个想这么做的人。希望你能帮上忙。

写出这行的方程式:

a*x + b*y + c = 0
然后将单击的坐标放入此方程式:

distance = a*x1 + b*y1 + c 

其中,
(x1,y1)
是您单击的点。如果您在该行上单击了
distance

Gabor是正确的,那么计算两点之间的距离并使用它是非常容易的。根据Roger建议的链接,下面是从AWT源代码中提取的一些代码,用于测量两点之间的距离

所以,你的代码应该是这样的

if (ptLineDist(lineX1,lineY1,lineX2,lineY2,clickX,clickY) < someLimit) 
   clicked=true; 
else clicked=false;

有什么好处吗?gwt中没有awt,罗杰,但是你可以提取代码。+1,感谢你挖掘代码,我太懒了,没法用线坐标写出所有的方程。实际上,您可以用示例代码中所示的1个端点的x和y坐标差来表示
a
b
。我有一条线在两端终止,而不是无限长。这难道不会给我一个积极的结果,点击的线路将继续吗?
 521:   /**
 522:    * Measures the square of the shortest distance from the reference point
 523:    * to a point on the infinite line extended from the segment. If the point
 524:    * is on the segment, the result will be 0. If the segment is length 0,
 525:    * the distance is to the common endpoint.
 526:    *
 527:    * @param x1 the first x coordinate of the segment
 528:    * @param y1 the first y coordinate of the segment
 529:    * @param x2 the second x coordinate of the segment
 530:    * @param y2 the second y coordinate of the segment
 531:    * @param px the x coordinate of the point
 532:    * @param py the y coordinate of the point
 533:    * @return the square of the distance from the point to the extended line
 534:    * @see #ptLineDist(double, double, double, double, double, double)
 535:    * @see #ptSegDistSq(double, double, double, double, double, double)
 536:    */
 537:   public static double ptLineDistSq(double x1, double y1, double x2, double y2,
 538:                                     double px, double py)
 539:   {
 540:     double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
 541: 
 542:     double x, y;
 543:     if (pd2 == 0)
 544:       {
 545:         // Points are coincident.
 546:         x = x1;
 547:         y = y2;
 548:       }
 549:     else
 550:       {
 551:         double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2;
 552:         x = x1 + u * (x2 - x1);
 553:         y = y1 + u * (y2 - y1);
 554:       }
 555: 
 556:     return (x - px) * (x - px) + (y - py) * (y - py);
 557:   }
 558: 
 559:   /**
 560:    * Measures the shortest distance from the reference point to a point on
 561:    * the infinite line extended from the segment. If the point is on the
 562:    * segment, the result will be 0. If the segment is length 0, the distance
 563:    * is to the common endpoint.
 564:    *
 565:    * @param x1 the first x coordinate of the segment
 566:    * @param y1 the first y coordinate of the segment
 567:    * @param x2 the second x coordinate of the segment
 568:    * @param y2 the second y coordinate of the segment
 569:    * @param px the x coordinate of the point
 570:    * @param py the y coordinate of the point
 571:    * @return the distance from the point to the extended line
 572:    * @see #ptLineDistSq(double, double, double, double, double, double)
 573:    * @see #ptSegDist(double, double, double, double, double, double)
 574:    */
 575:   public static double ptLineDist(double x1, double y1,
 576:                                    double x2, double y2,
 577:                                    double px, double py)
 578:   {
 579:     return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py));
 580:   }
 581: