📜  计算位于半圆内的数组中的点

📅  最后修改于: 2021-04-17 16:02:55             🧑  作者: Mango

给定两对(X,Y),(P,Q)R ,半圆的中心坐标,半圆的直径和半圆的交点坐标,半圆的半径以及数组arr [尺寸为N * 2的] ,由几个点的坐标组成,任务是从数组的内部或上方找到数组中的点数
半圆。
注意:考虑直径以上的半圆。

例子:

方法:可以根据以下观察结果解决给定问题:

请按照以下步骤解决问题:

  • 从(X,Y)和(P,Q)点找到半圆直径的线方程。
  • 初始化一个变量,例如ans ,以存储所需的点数。
  • 遍历数组arr []并执行以下操作:
    • 计算点(X,Y)(P,Q)之间的距离,并将其存储在变量中,例如d。
    • 在公式中,将arr [i] [0]arr [i] [1]分别置于RS的位置
      (S - Q)\times(X-P) - (R-P)\times (Y-Q)
      并将结果存储在变量f中
    • 递增1 ANS的计如果R≤df≥0。
  • 完成上述步骤后,打印存储在ans中的值。

下面是上述方法的实现:

Java
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#
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.


输出:
2

时间复杂度: O(N)
辅助空间: O(1)