Count points from an array that lies inside a semi | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPointsIns ( int x1 , int y1 , int radius , int x2 , int y2 , vector < pair < int , int > > points ) { int ans = 0 ; for ( int i = 0 ; i < points . size ( ) ; i ++ ) { bool condOne = false , condTwo = false ; if ( ( points [ i ] . second - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] . first - x2 ) >= 0 ) { condOne = true ; } if ( radius >= ( int ) sqrt ( pow ( ( y1 - points [ i ] . second ) , 2 ) + pow ( x1 - points [ i ] . first , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } int main ( ) { int X = 0 ; int Y = 0 ; int R = 5 ; int P = 5 ; int Q = 0 ; vector < pair < int , int > > arr = { make_pair ( 2 , 3 ) , make_pair ( 5 , 6 ) , make_pair ( -1 , 4 ) , make_pair ( 5 , 5 ) } ; cout << getPointsIns ( X , Y , R , P , Q , arr ) ; return 0 ; }