Count straight lines intersecting at a given point
Last Updated :
26 May, 2021
Given a matrix lines[][] of size N * 3, such that ith row denotes a line having the equation lines[i][0] * x + lines[i][1]*y = lines[i][2], and integers X and Y, denoting coordinates of a point (X, Y), the task is to count the number of lines from the given matrix which intersect with one another at the given point (X, Y).
Examples:
Input: N=5, X = 3, Y = 4, Lines[][] = {{4, -1, 8}, {2, -7, -2}, {1, 1, 7}, {1, -3, 5}, {1, 0, 3}}
Output: 3
Explanation:
Lines 4*x – y = 8, 1*x + 1* y = 7 and 1*x – 0*y = 3 intersect with each other at point (3, 4).
Input: N=4, X = -2, Y = 3, Lines[][] = {{3, -2, -12}, {1, 3, 5}, {1, -1, -5}, {2, 3, 4}}
Output: 2
Explanation:
Lines 3*x – 2*y = -12 and 1*x – 1*y = -5 intersect with each other at point (-2, 3).
Naive Approach: The simplest approach to solve the problem is that for each line, find its intersection point with other lines and check if they intersect at the given point (X, Y) or not. Finally, print the count of lines intersecting at (X, Y).
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using the observation:
If two lines pass through the same point, then they will definitely intersect at that point.
Follow the steps below to solve the problems:
- So, count the number of lines passing through the given point, let the count of lines be cnt.
- After calculating the above step, print the total number of intersections:
Count of intersections of cnt lines intersecting at (X, Y) = cntC2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
};
struct Line {
int a, b, c;
};
bool point_lies_on_line(Line l,
Point p)
{
if (l.a * p.x + l.b * p.y
== l.c) {
return true ;
}
return false ;
}
int intersecting_at_point(
vector<Line>& lines, Point p)
{
int cnt = 0;
for ( int i = 0; i < lines.size(); i++) {
if (point_lies_on_line(lines[i], p)) {
cnt++;
}
}
int ans = (cnt * (cnt - 1)) / 2;
return ans;
}
int main()
{
int N = 5;
Point p = { 3, 4 };
vector<Line> 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 });
int ans = intersecting_at_point(lines, p);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Point
{
int x, y;
public Point( int x, int y)
{
super ();
this .x = x;
this .y = y;
}
};
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;
}
};
static boolean point_lies_on_line(Line l,
Point p)
{
if (l.a * p.x + l.b * p.y == l.c)
{
return true ;
}
return false ;
}
static int intersecting_at_point(Vector<Line> lines,
Point p)
{
int cnt = 0 ;
for ( int i = 0 ; i < lines.size(); i++)
{
if (point_lies_on_line(lines.get(i), p))
{
cnt++;
}
}
int ans = (cnt * (cnt - 1 )) / 2 ;
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
Point p = new Point( 3 , 4 );
Vector<Line> lines = new Vector<Line>();
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 ));
int ans = intersecting_at_point(lines, p);
System.out.print(ans + "\n" );
}
}
|
Python3
def point_lies_on_line(l, p):
if (l[ 0 ] * p[ 0 ] + l[ 1 ] * p[ 1 ] = = l[ 2 ]):
return True
return False
def intersecting_at_point(lines, p):
cnt = 0
for i in range ( len (lines)):
if (point_lies_on_line(lines[i], p)):
cnt + = 1
ans = (cnt * (cnt - 1 )) / / 2
return ans
if __name__ = = '__main__' :
N = 5
p = [ 3 , 4 ]
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 ])
ans = intersecting_at_point(lines, p)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class Point
{
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
class Line
{
public int a, b, c;
public Line( int a,
int b, int c)
{
this .a = a;
this .b = b;
this .c = c;
}
};
static bool point_lies_on_line(Line l,
Point p)
{
if (l.a * p.x + l.b * p.y == l.c)
{
return true ;
}
return false ;
}
static int intersecting_at_point(List<Line> lines,
Point p)
{
int cnt = 0;
for ( int i = 0; i < lines.Count; i++)
{
if (point_lies_on_line(lines[i], p))
{
cnt++;
}
}
int ans = (cnt * (cnt - 1)) / 2;
return ans;
}
public static void Main(String[] args)
{
int N = 5;
Point p = new Point(3, 4);
List<Line> lines = new List<Line>();
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));
int ans = intersecting_at_point(lines, p);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
function point_lies_on_line(l, p)
{
if (l[0] * p[0] + l[1] * p[1]
== l[2]) {
return true ;
}
return false ;
}
function intersecting_at_point( lines, p)
{
var cnt = 0;
for ( var i = 0; i < lines.length; i++) {
if (point_lies_on_line(lines[i], p)) {
cnt++;
}
}
var ans = (cnt * (cnt - 1)) / 2;
return ans;
}
var N = 5;
var p = [3, 4];
var lines = [];
lines.push([ 4, -1, 8 ]);
lines.push([ 1, 0, 3 ]);
lines.push([ 1, 1, 7 ]);
lines.push([ 2, -7, -2 ]);
lines.push([ 1, -3, 5 ]);
var ans = intersecting_at_point(lines, p);
document.write( ans );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...