- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Program to find how many lines intersect in Python

Suppose we are given a list that contains values in pairs of (m, c). These values represent a line, where y = mx + c. We are also given two values, l, and r. We have to find out the number of the lines that intersect with each other between the range x = l to x = h.

So, if the input is like input_list = [[4, 6],[-6, 10],[8, 12]], l = 0, h = 2, then the output will be 2.

If we look at the given photo, the lines 4x + 6 = 0 and -6x + 10 intersect within the given range. So, there are two lines that are intersecting, so the output is 2.

To solve this, we will follow these steps −

- seg := a list containing pairs [(m * l + c, m * h + c, i) for index i, and values (m, c) in input_list]
- sort the list seg
- ans := a new list of the size of input_list containing 0s
- c := a new map from seg
- for each (x, y, i) in seg, do
- if c[x] > 1, then
- ans[i] := 1

- if c[x] > 1, then
- max_c := -(10 ^ 10)
- prv := -(10 ^ 10)
- for each (x, y, i) in seg, do
- if x is same as prv, then
- ans[i] := 1

- if y <= max_c, then
- ans[i] := 1

- max_c := maximum of (max_c, y)
- prv := x

- if x is same as prv, then
- min_c = 10 ^ 10
- prv = 10 ^ 10
- for each (x, y, i) in seg reversed, do
- if x is same as prv, then
- ans[i] := 1

- if y >= min_c, then
- ans[i] := 1

- min_c := minimum of (min_c, y)
- prv := x

- if x is same as prv, then
- return sum of the elements of list (ans)

## Example

Let us see the following implementation to get better understanding −

from collections import Counter def solve(input_list, l, h): seg = [(m * l + c, m * h + c, i) for i, (m, c) in enumerate(input_list)] seg.sort() ans = [0 for _ in input_list] c = Counter(seg) for (x, y, i) in seg: if c[x] > 1: ans[i] = 1 max_c = -(10 ** 10) prv = -(10 ** 10) for (x, y, i) in seg: if x == prv: ans[i] = 1 if y <= max_c: ans[i] = 1 max_c = max(max_c, y) prv = x min_c = 10 ** 10 prv = 10 ** 10 for (x, y, i) in seg[::-1]: if x == prv: ans[i] = 1 if y >= min_c: ans[i] = 1 min_c = min(min_c, y) prv = x return sum(ans) print(solve([[4, 6],[-6, 10],[8, 12]], 0, 2))

## Input

[[4, 6],[-6, 10],[8, 12]], 0, 2

## Output

2

- Related Articles
- In how many points can two distinct lines at the most intersect?
- How many pairs of adjacent angles are formed when two lines intersect in a point?
- Python Program to find out how many cubes are cut
- C++ Program to use above below primitive to test whether two lines intersect
- Program to find how many ways we can climb stairs in Python
- Program to find how many swaps needed to sort an array in Python
- Program to find intervals that do not intersect the cut interval in Python
- Program to find maximum how many water bottles we can drink in Python
- Program to find out how many transfer requests can be satisfied in Python
- Program to find how many updates required to make string half monotonous in Python
- Program to find in which interval how many tasks are worked on in Python
- Program to find how many total amount of rain we can catch in Python
- Program to find how many years it will take to reach t amount in Python
- Program to find out how many boxes can be put into the godown in Python
- Program to count how many times we can find "pizza" with given string characters in Python

Advertisements