我有一个canvas
和lines
。在click
上,我想检查单击是否在我的行上以突出显示它。
我也有一些rectangles
,只需使用正方形的start
和end point
即可轻松实现。
但是对于diagonal line
,我不能使用相同的技术,因为直线当然不能填充矩形。
但是我还能怎么做到呢?
此外,我还希望有一些“偏移”,这样,如果单击距线条足够近,它也会被标记,否则细线可能很难单击。
可能我缺少正确的关键字,因为我确定不是第一个想要这样做的关键字。希望您能提供帮助。
java大神给出的解决方案
Gabor是正确的,很容易计算两个点之间的距离并使用它。根据Roger的建议链接,这是一些从AWT源代码中提取的代码,用于测量两点之间的距离。 http://developer.classpath.org/doc/java/awt/geom/Line2D-source.html
因此,您的代码将类似于
if (ptLineDist(lineX1,lineY1,lineX2,lineY2,clickX,clickY) < someLimit)
clicked=true;
else clicked=false;
这是AWT代码(请查看上面的链接以获得许可证)
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:
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…
JAVA:如何检查对象数组中的所有对象是否都是子类的对象? - java我有一个对象数组。现在,我要检查所有这些对象是否都是MyObject的实例。有没有比这更好的选择:boolean check = true; for (Object o : justAList){ if (!(o instanceof MyObject)){ check = false; break; } } java大神给出的解决方案 如果您不喜欢循环,则…
如何使用BorderLayout(Java)扩展JTextField - java我有一个Java程序,其中使用的是JTextField,但如果我未指定默认大小,则其宽度为0。我将其插入BorderLayout中,因此如何制作它展开以填充整个容器? java大神给出的解决方案 在上面的示例中,文本字段将正常工作。但是,如果您插入EAST或WEST,则将不起作用。import java.awt.BorderLayout; import ja…
可以在没有操作系统的情况下运行Java程序吗? - java我知道所有Java程序都由JVM执行。这使Java与所有操作系统兼容(一次编写,可在任何地方运行)。但是我可以在没有操作系统的情况下运行Java程序吗?也许只运行JVM?并且,如果可能,功能是否会受到任何影响?注意:我的主要问题是,java程序可以直接在硬件上运行(通过JVM)吗?我可以在计算机中“启动”任何低级别的JVM吗? java大神给出的解决方案 实…