public static Optional < Coord > intersection ( Line l1 , Line l2 ) { Check . notNull ( l1 ) ; Check . notNull ( l2 ) ; final double x1 = l1 . getX1 ( ) ; final double x2 = l1 . getX2 ( ) ; final double y1 = l1 . getY1 ( ) ; final double y2 = l1 . getY2 ( ) ; final double x3 = l2 . getX1 ( ) ; final double x4 = l2 . getX2 ( ) ; final double y3 = l2 . getY1 ( ) ; final double y4 = l2 . getY2 ( ) ; final double d = ( x1 - x2 ) * ( y3 - y4 ) - ( y1 - y2 ) * ( x3 - x4 ) ; if ( Double . compare ( d , 0 ) == 0 ) { return Optional . empty ( ) ; } final double xi = ( ( x3 - x4 ) * ( x1 * y2 - y1 * x2 ) - ( x1 - x2 ) * ( x3 * y4 - y3 * x4 ) ) / d ; final double yi = ( ( y3 - y4 ) * ( x1 * y2 - y1 * x2 ) - ( y1 - y2 ) * ( x3 * y4 - y3 * x4 ) ) / d ; return Optional . of ( new Coord ( xi , yi ) ) ; }
Get the intersection point of two lines .