The Wayback Machine - https://web.archive.org/web/20240823161811/https://www.geeksforgeeks.org/count-points-from-an-array-that-lies-inside-a-semi-circle/
Open In App

Count points from an array that lies inside a semi-circle

Last Updated : 28 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Easy
51.59%
3K

Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the number of points from the array that lies inside or on the 
semicircle. 
Note: The semicircle above the diameter is considered.

Examples:

Input: X = 0, Y = 0, R = 5, P = 5, Q = 0, arr[][] = { {2, 3}, {5, 6}, {-1, 4}, {5, 5} }
Output: 2
Explanation: The points {2, 3} and {-1, 4} are inside the semi-circle.

Input: X = 2, Y = 3, R = 10, P = 12, Q = 3, arr[][] = { {-7, -5}, {0, 6}, {11, 4} }
Output: 2

Approach: The given problem can be solved based on the following observations: 

  • The points that lies on or inside the semicircle must be above or on the diameter of semicircle and the distance between center and that point should be ? R.
  • Suppose a\times x + b\times y + c           is the equation of diameter. 
    The point (R, S) lies above the line if 
    a\times R + b\times S + C>=0
     
  • A point (R, S) lies above the line formed by joining points (X, Y) and (P, Q) if(S - Q)\times(X-P) - (R-P)\times (Y-Q) >= 0

Follow the steps below to solve the problem:

  • Find the equation of line the diameter of the semi-circle from the points (X, Y) and (P, Q).
  • Initialize a variable, say ans, to store the count of required points.
  • Traverse the array arr[] and perform the following operations:
    • Calculate the distance between the points (X, Y) and (P, Q) and store it in a variable, say d.
    • Put arr[i][0] and arr[i][1] in the place of R and S respectively, in the formula 
      (S - Q)\times(X-P) - (R-P)\times (Y-Q)
      and store the result in a variable, say f.
    • Increment the count of ans by 1 if R ? d and f ? 0.
  • After completing the above steps, print the value stored in ans.

Below is the implementation of the above approach:

C++

// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
int getPointsIns(int x1, int y1, int radius, int x2,
                 int y2, vector<pair<int, int>> points)
{
    int ans = 0;
     
    // Traverse the array
    for(int i = 0; i < points.size(); i++)
    {
         
        // Stores if a point lies
        // above the diameter or not
        bool condOne = false, condTwo = false;
        if ((points[i].second - y2) *
              (x2 - x1) - (y2 - y1) *
             (points[i].first - x2) >= 0)
        {
            condOne = true;
        }
 
        // Stores if the R is less than or
        // equal to the distance between
        // center and point
        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;
}
 
// Driver code
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;
}
 
// This code is contributed by nirajgusain5

                    

Java

// Java program for above approach
import java.io.*;
 
class Gfg {
  public static int getPointsIns(int x1, int y1,int radius,
                                 int x2,int y2, pair points[])
  {
    int ans = 0;
    // Traverse the array
    for (int i = 0; i < points.length; i++)
    {
       
      // Stores if a point lies
      // above the diameter or not
      boolean condOne = false, condTwo = false;
      if ((points[i].b - y2) *
          (x2 - x1)- (y2 - y1) *
          (points[i].a - x2)>= 0)
      {
        condOne = true;
      }
 
      // Stores if the R is less than or
      // equal to the distance between
      // center and point
      if (radius >= (int)Math.sqrt(Math.pow((y1 - points[i].b), 2)+
                                   Math.pow(x1 - points[i].a, 2)))
      {
        condTwo = true;
      }
      if (condOne && condTwo)
      {
        ans += 1;
      }
    }
    return ans;
  }
   
  // Driver code
  public static void main(String[] args)
  {
    int X = 0;
    int Y = 0;
    int R = 5;
    int P = 5;
    int Q = 0;
 
    pair arr[] = {new pair(2, 3), new pair(5, 6), new pair(-1, 4), new pair(5,5)};
 
    System.out.print(getPointsIns(X, Y, R, P, Q, arr));
  }
}
class pair
{
  int a;
  int b;
  pair(int a,int b)
  {   
    this.a = a;
    this.b = b;
  }
}

                    

Python3

# Python implementation of above approach
def getPointsIns(x1, y1, radius, x2, y2, points):
    # Stores the count of ans
    ans = 0
 
    # Traverse the array
    for point in points:
 
        # Stores if a point lies
        # above the diameter or not
        condOne = (point[1] - y2) * (x2 - x1) \
                  - (y2 - y1) * (point[0] - x2) >= 0
 
        # Stores if the R is less than or
        # equal to the distance between
        # center and point
 
        condTwo = radius >= ((y1 - point[1]) ** 2 \
                  + (x1 - point[0]) ** 2) ** (0.5)
 
        if condOne and condTwo:
            ans += 1
 
    return ans
 
 
# Driver Code
# Input
X = 0
Y = 0
R = 5
P = 5
Q = 0
arr = [[2, 3], [5, 6], [-1, 4], [5, 5]]
 
print(getPointsIns(X, Y, R, P, Q, arr))

                    

C#

// C# program for above approach
using System;
 
class Gfg
{
  public static int getPointsIns(int x1, int y1,
                                 int radius, int x2,
                                 int y2, pair[] points)
  {
    int ans = 0;
     
    // Traverse the array
    for (int i = 0; i < points.Length; i++) {
 
      // Stores if a point lies
      // above the diameter or not
      bool condOne = false, condTwo = false;
      if ((points[i].b - y2) * (x2 - x1)
          - (y2 - y1) * (points[i].a - x2)
          >= 0) {
        condOne = true;
      }
 
      // Stores if the R is less than or
      // equal to the distance between
      // center and point
      if (radius >= (int)Math.Sqrt(
        Math.Pow((y1 - points[i].b), 2)
        + Math.Pow(x1 - points[i].a, 2))) {
        condTwo = true;
      }
      if (condOne && condTwo) {
        ans += 1;
      }
    }
    return ans;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int X = 0;
    int Y = 0;
    int R = 5;
    int P = 5;
    int Q = 0;
 
    pair[] arr = { new pair(2, 3), new pair(5, 6),
                  new pair(-1, 4), new pair(5, 5) };
 
    Console.Write(getPointsIns(X, Y, R, P, Q, arr));
  }
}
public class pair {
  public int a;
  public int b;
  public pair(int a, int b)
  {
    this.a = a;
    this.b = b;
  }
}
 
// This code is contributed by code_hunt.

                    

Javascript

<script>
// Javascript program for above approach
 
function getPointsIns(x1,y1,radius,x2,y2,points)
{
    let ans = 0;
    // Traverse the array
    for (let i = 0; i < points.length; i++)
    {
        
      // Stores if a point lies
      // above the diameter or not
      let condOne = false, condTwo = false;
      if ((points[i][1] - y2) *
          (x2 - x1)- (y2 - y1) *
          (points[i][0] - x2)>= 0)
      {
        condOne = true;
      }
  
      // Stores if the R is less than or
      // equal to the distance between
      // center and point
      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;
}
 
// Driver code
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));
   
 
 
 
// This code is contributed by avanitrachhadiya2155
</script>

                    

Output: 
2

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Queries on count of points lie inside a circle
Given n coordinate (x, y) of points on 2D plane and Q queries. Each query contains an integer r, the task is to count the number of points lying inside or on the circumference of the circle having radius r and centered at the origin.Examples : Input : n = 5 Coordinates: 1 1 2 2 3 3 -1 -1 4 4 Query 1: 3 Query 2: 32 Output : 3 5 For first query radiu
8 min read
Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
A circle is given with k equidistant points on its circumference. 2 points A and B are given in the circle. Find the count of all obtuse angles (angles larger than 90 degree) formed from /_ACB, where C can be any point in circle other than A or B. Note : A and B are not equal. A &lt; B. Points are between 1 and K(both inclusive). Examples : Input :
5 min read
Count of integral coordinates that lies inside a Square
Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1 = 1, y1 = 1, x2 = 4, x3 = 4 Output: 4 Approach: T
4 min read
Equation of circle when three points on the circle are given
Given three coordinates that lie on a circle, (x1, y1), (x2, y2), and (x3, y3). The task is to find the equation of the circle and then print the centre and the radius of the circle. The equation of circle in general form is x² + y² + 2gx + 2fy + c = 0 and in radius form is (x - h)² + (y -k)² = r², where (h, k) is the centre of the circle and r is
11 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Count of unordered pairs of semi-prime numbers with prime sum in range [1, N]
Given a positive integer N, the task is to find the number of unordered pairs of semi-prime numbers in the range [1, N] such that their sum is prime. Examples: Input: N = 25Output: 5Explanation:The valid pairs of semi-prime numbers whose sum is also prime are (10, 21), (14, 15), (15, 22), (20, 21), and (21, 22). The count of such numbers is 5. Inpu
8 min read
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides
Given an array arr[][] of dimension N * 2 representing the co-ordinates of N points and an array Q[] consisting of M integers, the task for every element in the Q[i] is to find the number of points lying inside or on the right-angled isosceles triangle formed on the positive co-ordinate axis with two equal sides of length Q[i]in each query. Example
7 min read
Count Integral points inside a Triangle
Given three non-collinear integral points in XY plane, find the number of integral points inside the triangle formed by the three points. (A point in XY plane is said to be integral/lattice point if both its co-ordinates are integral). Example: Input: p = (0, 0), q = (0, 5) and r = (5,0) Output: 6Explanation: The points (1,1) (1,2) (1,3) (2,1) (2,2
8 min read
Count pairs (i, j) from an array such that |arr[i]| and |arr[j]| both lies between |arr[i] - arr[j]| and |arr[i] + arr[j]|
Given an array arr[] of size N, the task is to count the number of pairs (arr[i], arr[j]) such that |arr[i]| and |arr[j]| lies between |arr[i] - arr[j]| and |arr[i] + arr[j]|. Examples: Input: arr[] = {1, 3, 5, 7} Output: 2 Explanation: Pair (arr[1], arr[2]) (= (3, 5)) lies between |3 - 5| (= 2) and |3 + 5| (= 8).Pair (arr[2], arr[3]) (= (5, 7)) li
6 min read
Count pairs from a given array whose product lies in a given range
Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i &lt; j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: Valid pairs are {4, 1}, {1, 5} and {4, 2}. Input: a
13 min read
Check if an integer can be expressed as a sum of two semi-primes
Given a positive integer N, check if it can be expressed as a sum of two semi-primes or not.Semi-primes A number is said to be a semi-prime if it can be expressed as product of two primes number ( not necessarily distinct ).Semi-primes in the range 1 -100 are: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65,
8 min read
Print all Semi-Prime Numbers less than or equal to N
Given an integer N, the task is to print all the semi-prime numbers ? N.A semi-prime number is an integer that can be expressed as a product of two distinct prime numbers. For example, 15 = 3 * 5 is a semi-prime number but 9 = 3 * 3 is not. Examples: Input: N = 20 Output: 6 10 14 15 Input: N = 50 Output: 6 10 14 15 21 22 26 33 34 35 38 39 46 Prereq
12 min read
Sum of Semi-Prime Numbers less than or equal to N
Given an integer N, the task is to find the sum of semi-prime numbers which are less than or equal to N. A Semi prime number is a number that is a multiple of two prime numbers. Examples: Input: N = 6 Output: 10 4 and 6 are the semi primes ? 6 4 + 6 = 10Input: N = 10000000 Output: 9322298311255 Approach: First Calculate the primes less than or equa
8 min read
Smallest Semi-Prime Number with at least N difference between any two of its divisors
Given a positive integer N, the task is to find the smallest semi-prime number such that the difference between any of its two divisors is at least N. Examples: Input: N = 2Output: 15Explanation:The divisors of 15 are 1, 3, 5, and 15 and the difference between any of its two divisors is greater than or equal to N(= 2). Input: N = 3Output: 55 Approa
7 min read
Find minimum radius such that atleast k point lie inside the circle
Given a positive integer K, a circle center at (0, 0) and coordinates of some points. The task is to find minimum radius of the circle so that at-least k points lie inside the circle. Output the square of the minimum radius. Examples: Input : (1, 1), (-1, -1), (1, -1), k = 3Output : 2We need a circle of radius at least 2to include 3 points.Input :
12 min read
Coordinates of rectangle with given points lie inside
Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained rectangle. Note: n &gt;= 4 (we have at least 4 points a
6 min read
Generate all integral points lying inside a rectangle
Given a rectangle of length L, and width, W, the task is to generate all integral coordinates (X, Y) that lie within a rectangle pf dimensions L * W having one of its vertexes in the origin (0, 0). Examples: Input: L = 3, W = 2Output: (0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)Explanation: Total number of integral coordinates existing within the rect
7 min read
Number of points lying inside a rectangle as well as a triangle
Given two 2D arrays rectangle[][] and triangle[][], representing the coordinates of vertices of a rectangle and a triangle respectively, and another array points[][] consisting of N coordinates, the task is to count the number of points that lies inside both the rectangle and the triangle. Examples: Input: rectangle[][] = {{1, 1}, {6, 1}, {6, 6}, {
15 min read
Count the number of intervals in which a given value lies
Given a 2D-array of [Tex]N [/Tex]integer intervals [Tex][L, R] [/Tex]and a value [Tex]V [/Tex]. For every interval [li, ri], the task is to check if V lies in between li and ri, both inclusive. The task is to print the count of intervals that satisfies this. Note: 1&lt;=li&lt;= riExamples: Input: arr[][] = {{1, 10}, {5, 10}, {15, 25}, {7, 12}, {20,
13 min read
Practice Tags :