• 线段相交的判断


    第一种方法是:判断这两个直线是否相交,相交并且交点在线段上,就可以保证两个线段是相交的。

    第二种方法是:跨立算法。属于计算几何的一个基础。保证几个有向面积是相反的即可。是充要条件。知道三个点计算一个三角形的有向面积就是一个三阶的最后一列为1的行列式的求值。

    这里用第二种方法:

     

     1 import java.awt.Paint;
     2 
     3 public class SetmentTest {
     4 
     5     // 参考文献 https://www.2cto.com/kf/201404/296319.html
     6     public static double get_area(Point a0, Point a1, Point a2) {
     7         double s = a0.x * a1.y + a2.x * a0.y + a1.x * a2.y - a2.x * a1.y - a0.x * a2.y - a1.x * a0.y;
     8         return s;
     9     }
    10 
    11     public static boolean segmentIntersect(Point st1, Point ed1, Point st2, Point ed2) {
    12         double s1 = get_area(st1, ed1, st2);
    13         double s2 = get_area(st1, ed1, ed2);
    14         double s3 = get_area(st2, ed2, st1);
    15         double s4 = get_area(st2, ed2, ed1);
    16         if (s1 * s2 <= 0 && s3 * s4 <= 0) {
    17             // System.out.println("Intersection
    ");
    18             return true;
    19         } else {
    20             // System.out.println("No Intersection");
    21             return false;
    22         }
    23     }
    24 
    25     public static void main(String[] args) {
    26         Point aa = new Point(1, 1);
    27         Point bb = new Point(2, 2);
    28         Point cc = new Point(1, 2);
    29         Point dd = new Point(2, 1);
    30         System.out.println(SetmentTest.segmentIntersect(aa, bb, cc, dd));
    31         if (SetmentTest.segmentIntersect(aa, bb, cc, dd) == true)
    32             System.out.println("有交点");
    33         else
    34             System.out.println("没有交点");
    35 
    36     }
    37 
    38 }

    参考文献:

    • https://blog.csdn.net/wingrez/article/details/75042006
    • http://www.cnblogs.com/jbelial/archive/2011/08/04/2127487.html
    • https://blog.csdn.net/k_koris/article/details/81711396?utm_source=blogxgwz4
    • https://www.2cto.com/kf/201404/296319.html

    万事走心 精益求美


  • 相关阅读:
    第十八次发博不知道用什么标题好
    第十七次发博不知道用什么标题好
    第十六次发博不知道用什么标题好
    第十五次发博不知道用什么标题好
    第十四次发博不知道用什么标题好
    第十三次发博不知道用什么标题好
    第十二次发博不知道用什么标题好
    NEC学习 ---- 布局 -两列, 左侧定宽,右侧自适应
    jQuery 利用 $.getJson() 实现跨域
    HTML: 字符實體
  • 原文地址:https://www.cnblogs.com/kongchung/p/9913889.html
Copyright © 2020-2023  润新知