Dynamic Convex hull | Adding Points to an Existing Convex Hull | C # program to add given a point p to a given convext hull . The program assumes that the point of given convext hull are in anti - clockwise order . ; checks whether the point crosses the convex hull or not ; Returns the square of distance between two input points ; Checks whether the point is inside the convex hull or not ; Initialize the centroid of the convex hull ; Multiplying with n to avoid floating point ; if the mid and the given point lies always on the same side w . r . t every edge of the convex hull , then the point lies inside the convex hull ; Adds a point p to given convex hull a [ ] ; If point is inside p ; point having minimum distance from the point p ; Find the upper tangent ; Find the lower tangent ; Initialize result ; making the final hull by traversing points from up to low of given convex hull . ; Modify the original vector ; Driver code ; the set of points in the convex hull ; Print the modified Convex Hull
using System ; using System . Collections . Generic ; public class GFG { static int orientation ( List < int > a , List < int > b , List < int > c ) { int res = ( b [ 1 ] - a [ 1 ] ) * ( c [ 0 ] - b [ 0 ] ) - ( c [ 1 ] - b [ 1 ] ) * ( b [ 0 ] - a [ 0 ] ) ; if ( res == 0 ) return 0 ; if ( res > 0 ) return 1 ; return - 1 ; } static int sqDist ( List < int > p1 , List < int > p2 ) { return ( p1 [ 0 ] - p2 [ 0 ] ) * ( p1 [ 0 ] - p2 [ 0 ] ) + ( p1 [ 1 ] - p2 [ 1 ] ) * ( p1 [ 1 ] - p2 [ 1 ] ) ; } static bool inside ( List < List < int > > A , List < int > p ) { List < int > mid = new List < int > ( ) { 0 , 0 } ; int n = A . Count ; for ( int i = 0 ; i < n ; i ++ ) { mid [ 0 ] += A [ i ] [ 0 ] ; mid [ 1 ] += A [ i ] [ 1 ] ; } for ( int i = 0 , j ; i < n ; i ++ ) { j = ( i + 1 ) % n ; int x1 = A [ i ] [ 0 ] * n , x2 = A [ j ] [ 0 ] * n ; int y1 = A [ i ] [ 1 ] * n , y2 = A [ j ] [ 1 ] * n ; int a1 = y1 - y2 ; int b1 = x2 - x1 ; int c1 = x1 * y2 - y1 * x2 ; int for_mid = a1 * mid [ 0 ] + b1 * mid [ 1 ] + c1 ; int for_p = a1 * p [ 0 ] * n + b1 * p [ 1 ] * n + c1 ; if ( for_mid * for_p < 0 ) return false ; } return true ; } static void addPoint ( List < List < int > > a , List < int > p ) { if ( inside ( a , p ) ) return ; int ind = 0 ; int n = a . Count ; for ( int i = 1 ; i < n ; i ++ ) { if ( sqDist ( p , a [ i ] ) < sqDist ( p , a [ ind ] ) ) { ind = i ; } } int up = ind ; while ( orientation ( p , a [ up ] , a [ ( up + 1 ) % n ] ) >= 0 ) up = ( up + 1 ) % n ; int low = ind ; while ( orientation ( p , a [ low ] , a [ ( n + low - 1 ) % n ] ) <= 0 ) low = ( n + low - 1 ) % n ; List < List < int > > ret = new List < List < int > > ( ) ; int curr = up ; ret . Add ( a [ curr ] ) ; while ( curr != low ) { curr = ( curr + 1 ) % n ; ret . Add ( a [ curr ] ) ; } ret . Add ( p ) ; a . Clear ( ) ; for ( int i = 0 ; i < ret . Count ; i ++ ) { a . Add ( ret [ i ] ) ; } } static public void Main ( ) { List < List < int > > a = new List < List < int > > ( ) ; a . Add ( new List < int > ( ) { 0 , 0 } ) ; a . Add ( new List < int > ( ) { 3 , - 1 } ) ; a . Add ( new List < int > ( ) { 4 , 5 } ) ; a . Add ( new List < int > ( ) { - 1 , 4 } ) ; int n = a . Count ; List < int > p = new List < int > ( ) { 100 , 100 } ; addPoint ( a , p ) ; foreach ( List < int > e in a ) { Console . Write ( " ( " + e [ 0 ] + " , ▁ " + e [ 1 ] + " ) ▁ " ) ; } } }