Count points from an array that lies inside a semi | Javascript program for above approach ; Traverse the array ; Stores if a point lies above the diameter or not ; Stores if the R is less than or equal to the distance between center and point ; Driver code
function getPointsIns ( x1 , y1 , radius , x2 , y2 , points ) { let ans = 0 ; for ( let i = 0 ; i < points . length ; i ++ ) { let condOne = false , condTwo = false ; if ( ( points [ i ] [ 1 ] - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] [ 0 ] - x2 ) >= 0 ) { condOne = true ; } if ( radius >= Math . sqrt ( Math . pow ( ( y1 - points [ i ] [ 1 ] ) , 2 ) + Math . pow ( x1 - points [ i ] [ 0 ] , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } let X = 0 ; let Y = 0 ; let R = 5 ; let P = 5 ; let Q = 0 ; let arr = [ [ 2 , 3 ] , [ 5 , 6 ] , [ - 1 , 4 ] , [ 5 , 5 ] ] ; document . write ( getPointsIns ( X , Y , R , P , Q , arr ) ) ;