Number of quadrilaterals possible from the given points | class Point points ; determines the orientation of points ; check whether the distinct line segments intersect ; check if points overlap ( similar ) ; it is same , we are returning false because quadrilateral is not possible in this case ; it is not same , So there is a possibility of a quadrilateral ; check for collinearity ; it is collinear , we are returning false because quadrilateral is not possible in this case ; it is not collinear , So there is a possibility of a quadrilateral ; check if any of the points are same ; similar points exist ; check for collinearity ; points are collinear ; Checking for cases where no of quadrilaterals = 1 or 3 ; Driver code ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 5 , 9 ) ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 ) ; A = ( 0 , 9 ) , B = ( 0 , 10 ) , C = ( 0 , 11 ) , D = ( 0 , 12 ) ; A = ( 0 , 9 ) , B = ( 0 , 9 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 )
{ constructor ( ) { this . x = 0 ; this . y = 0 ; } } function orientation ( p , q , r ) { let val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } function doIntersect ( p1 , q1 , p2 , q2 ) { let o1 = orientation ( p1 , q1 , p2 ) ; let o2 = orientation ( p1 , q1 , q2 ) ; let o3 = orientation ( p2 , q2 , p1 ) ; let o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; return false ; } function similar ( p1 , p2 ) { if ( p1 . x == p2 . x && p1 . y == p2 . y ) return false ; return true ; } function collinear ( p1 , p2 , p3 ) { let x1 = p1 . x , y1 = p1 . y ; let x2 = p2 . x , y2 = p2 . y ; let x3 = p3 . x , y3 = p3 . y ; if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) return false ; else return true ; } function no_of_quads ( p1 , p2 , p3 , p4 ) { let same = true ; same = same & similar ( p1 , p2 ) ; same = same & similar ( p1 , p3 ) ; same = same & similar ( p1 , p4 ) ; same = same & similar ( p2 , p3 ) ; same = same & similar ( p2 , p4 ) ; same = same & similar ( p3 , p4 ) ; if ( same == false ) return 0 ; let coll = true ; coll = coll & collinear ( p1 , p2 , p3 ) ; coll = coll & collinear ( p1 , p2 , p4 ) ; coll = coll & collinear ( p1 , p3 , p4 ) ; coll = coll & collinear ( p2 , p3 , p4 ) ; if ( coll == false ) return 0 ; let check = 0 ; if ( doIntersect ( p1 , p2 , p3 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p3 , p2 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p2 , p4 , p3 ) ) check = 1 ; if ( check == 0 ) return 3 ; return 1 ; } let p1 , p2 , p3 , p4 ; p1 = new Point ( ) ; p2 = new Point ( ) ; p3 = new Point ( ) ; p4 = new Point ( ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 5 ; p4 . y = 9 ; document . write ( no_of_quads ( p1 , p2 , p3 , p4 ) + " " ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 0 ; p4 . y = 3 ; document . write ( no_of_quads ( p1 , p2 , p3 , p4 ) + " " ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = 0 ; p2 . y = 10 ; p3 . x = 0 ; p3 . y = 11 ; p4 . x = 0 ; p4 . y = 12 ; document . write ( no_of_quads ( p1 , p2 , p3 , p4 ) + " " ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = 0 ; p2 . y = 9 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 0 ; p4 . y = 3 ; document . write ( no_of_quads ( p1 , p2 , p3 , p4 ) + " " ) ;