public static int orientationRobust ( Point2D p , Point2D q , Point2D r ) { ECoordinate det_ec = new ECoordinate ( ) ; det_ec . set ( q . x ) ; det_ec . sub ( p . x ) ; ECoordinate rp_y_ec = new ECoordinate ( ) ; rp_y_ec . set ( r . y ) ; rp_y_ec . sub ( p . y ) ; ECoordinate qp_y_ec = new ECoordinate ( ) ; qp_y_ec . set ( q . y ) ; qp_y_ec . sub ( p . y ) ; ECoordinate rp_x_ec = new ECoordinate ( ) ; rp_x_ec . set ( r . x ) ; rp_x_ec . sub ( p . x ) ; det_ec . mul ( rp_y_ec ) ; qp_y_ec . mul ( rp_x_ec ) ; det_ec . sub ( qp_y_ec ) ; if ( ! det_ec . isFuzzyZero ( ) ) { double det_ec_value = det_ec . value ( ) ; if ( det_ec_value < 0.0 ) return - 1 ; if ( det_ec_value > 0.0 ) return 1 ; return 0 ; } // Need extended precision BigDecimal det_mp = new BigDecimal ( q . x ) ; BigDecimal px_mp = new BigDecimal ( p . x ) ; BigDecimal py_mp = new BigDecimal ( p . y ) ; det_mp = det_mp . subtract ( px_mp ) ; BigDecimal rp_y_mp = new BigDecimal ( r . y ) ; rp_y_mp = rp_y_mp . subtract ( py_mp ) ; BigDecimal qp_y_mp = new BigDecimal ( q . y ) ; qp_y_mp = qp_y_mp . subtract ( py_mp ) ; BigDecimal rp_x_mp = new BigDecimal ( r . x ) ; rp_x_mp = rp_x_mp . subtract ( px_mp ) ; det_mp = det_mp . multiply ( rp_y_mp ) ; qp_y_mp = qp_y_mp . multiply ( rp_x_mp ) ; det_mp = det_mp . subtract ( qp_y_mp ) ; return det_mp . signum ( ) ; }
Calculates the orientation of the triangle formed by p q r . Returns 1 for counter - clockwise - 1 for clockwise and 0 for collinear . May use high precision arithmetics for some special degenerate cases .