📜  计算在给定点相交的直线

📅  最后修改于: 2021-04-29 09:45:43             🧑  作者: Mango

给定大小为N * 3的矩阵lines [] [] ,使得i行表示具有方程式lines [i] [0] * x + lines [i] [1] * y = lines [i] [ [2] ,以及整数XY ,分别表示点(X,Y)的坐标,任务是计算给定矩阵中在给定点(X,Y)上彼此相交的线数。

例子:

天真的方法:解决问题的最简单方法是,对于每条线,找到其与其他线的交点,并检查它们是否在给定点(X,Y)处相交。最后,打印与(X,Y)相交的行数
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:可以通过观察来优化上述方法:

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

  1. 因此,计算通过给定点的线数,令线数为cnt
  2. 计算完上述步骤后,打印相交总数:

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Structure to store point
struct Point {
 
    int x, y;
};
 
// Structure to store line
struct Line {
 
    int a, b, c;
};
 
// Function to check if a line
// passes through a point or not
bool point_lies_on_line(Line l,
                        Point p)
{
    // Condition for the line
    // to pass through point p
    if (l.a * p.x + l.b * p.y
        == l.c) {
 
        return true;
    }
    return false;
}
 
// Function to find lines
// intersecting at a given point
int intersecting_at_point(
    vector& lines, Point p)
{
    int cnt = 0;
    for (int i = 0; i < lines.size(); i++) {
 
        // If the point lies on a line
        if (point_lies_on_line(lines[i], p)) {
 
            // Increment cnt
            cnt++;
        }
    }
 
    // Count of intersections
    int ans = (cnt * (cnt - 1)) / 2;
 
    // Return answer
    return ans;
}
 
// Driver Code
int main()
{
    // Number of lines
    int N = 5;
 
    // Point (x, y)
    Point p = { 3, 4 };
 
    // Array to store the lines
    vector lines;
    lines.push_back({ 4, -1, 8 });
    lines.push_back({ 1, 0, 3 });
    lines.push_back({ 1, 1, 7 });
    lines.push_back({ 2, -7, -2 });
    lines.push_back({ 1, -3, 5 });
 
    // Function call
    int ans = intersecting_at_point(lines, p);
 
    cout << ans << endl;
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Structure to store point
static class Point
{
    int x, y;
 
    public Point(int x, int y)
    {
        super();
        this.x = x;
        this.y = y;
    }
};
 
// Structure to store line
static class Line
{
    int a, b, c;
 
    public Line(int a, int b, int c)
    {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    }
};
 
// Function to check if a line
// passes through a point or not
static boolean point_lies_on_line(Line l,
                                  Point p)
{
     
    // Condition for the line
    // to pass through point p
    if (l.a * p.x + l.b * p.y == l.c)
    {
        return true;
    }
    return false;
}
 
// Function to find lines
// intersecting at a given point
static int intersecting_at_point(Vector lines,
                                 Point p)
{
    int cnt = 0;
    for(int i = 0; i < lines.size(); i++)
    {
         
        // If the point lies on a line
        if (point_lies_on_line(lines.get(i), p))
        {
             
            // Increment cnt
            cnt++;
        }
    }
 
    // Count of intersections
    int ans = (cnt * (cnt - 1)) / 2;
 
    // Return answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Number of lines
    int N = 5;
 
    // Point (x, y)
    Point p = new Point(3, 4);
 
    // Array to store the lines
    Vector lines = new Vector();
    lines.add(new Line(4, -1, 8));
    lines.add(new Line(1, 0, 3));
    lines.add(new Line(1, 1, 7));
    lines.add(new Line(2, -7, -2));
    lines.add(new Line(1, -3, 5));
 
    // Function call
    int ans = intersecting_at_point(lines, p);
 
    System.out.print(ans + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to check if a line
# passes through a poor not
def point_lies_on_line(l, p):
 
    # Condition for the line
    # to pass through pop
    if (l[0] * p[0] + l[1] * p[1] == l[2]):
        return True
 
    return False
 
# Function to find lines
# intersecting at a given point
def intersecting_at_point(lines, p):
 
    cnt = 0
    for i in range(len(lines)):
 
        # If the polies on a line
        if (point_lies_on_line(lines[i], p)):
 
            # Increment cnt
            cnt += 1
             
    # Count of intersections
    ans = (cnt * (cnt - 1)) // 2
 
    # Return answer
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Number of lines
    N = 5
 
    # Po(x, y)
    p = [ 3, 4 ]
 
    # Array to store the lines
    lines = []
    lines.append([ 4, -1, 8 ])
    lines.append([ 1, 0, 3 ])
    lines.append([ 1, 1, 7 ])
    lines.append([ 2, -7, -2 ])
    lines.append([ 1, -3, 5 ])
 
    # Function call
    ans = intersecting_at_point(lines, p)
 
    print(ans)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Structure to store point
class Point
{
  public int x, y;
  public Point(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
};
 
// Structure to store line
class Line
{
  public int a, b, c;
  public Line(int a,
              int b, int c)
  {
    this.a = a;
    this.b = b;
    this.c = c;
  }
};
 
// Function to check if a line
// passes through a point or not
static bool point_lies_on_line(Line l,
                               Point p)
{  
  // Condition for the line
  // to pass through point p
  if (l.a * p.x + l.b * p.y == l.c)
  {
    return true;
  }
  return false;
}
 
// Function to find lines
// intersecting at a given point
static int intersecting_at_point(List lines,
                                 Point p)
{
  int cnt = 0;
  for(int i = 0; i < lines.Count; i++)
  {       
    // If the point lies on a line
    if (point_lies_on_line(lines[i], p))
    {           
      // Increment cnt
      cnt++;
    }
  }
 
  // Count of intersections
  int ans = (cnt * (cnt - 1)) / 2;
 
  // Return answer
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Number of lines
  int N = 5;
 
  // Point (x, y)
  Point p = new Point(3, 4);
 
  // Array to store the lines
  List lines = new List();
  lines.Add(new Line(4, -1, 8));
  lines.Add(new Line(1, 0, 3));
  lines.Add(new Line(1, 1, 7));
  lines.Add(new Line(2, -7, -2));
  lines.Add(new Line(1, -3, 5));
 
  // Function call
  int ans = intersecting_at_point(lines, p);
 
  Console.Write(ans + "\n");
}
}
 
// This code is contributed by Rajput-Ji


输出:
3


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