Add calcLinearIntersection method

This method calculates the intersection point between two Lines. This
method returns the coordinates of intersection if there is a unique
point, 0 if there are none, and math.inf if the lines coincide.
This commit is contained in:
xypnox 2019-10-28 22:10:23 +05:30 committed by altf_four
parent 9b3d06402a
commit a1587e73f7
3 changed files with 46 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.pythonPath": "env/bin/python3"
}

View File

@ -1,3 +1,5 @@
import math
class Linear:
def __init__(self, a, c, length):
@ -7,3 +9,23 @@ class Linear:
def calc_linear_line(self):
print([(0 - self.length, self.a * (0 - self.length)), (0 - self.length, self.a * self.length)])
def calcLineaIntersection(self, line):
"""
This function accepts one line of Linear class
and returns the coordinates of intersection between the self and the line passed
If there is no intercept it returns 0
If there are infinite intercepts it return math.inf
If there is one unique intercept it returns a tuple with (x, y)
"""
if self.a == line.a:
if self.c == line.c:
return math.inf
return 0
x = (line.c-self.c)/(self.a - line.a)
y = self.a*(line.c - self.c)/(self.a - line.a) + self.c
return (x, y)

View File

@ -0,0 +1,21 @@
import math
def calcLinearIntersection(line1, line2):
"""
This function accepts two lines of Linear class
and returns the coordinates of intersection between the two lines
If there is no intercept it returns 0
If there are infinite intercepts it return math.inf
If there is one unique intercept it returns a tuple with (x, y)
"""
if line1.a == line2.a:
if line1.c == line2.c:
return math.inf
return 0
x = (line2.c-line1.c)/(line1.a - line2.a)
y = line1.a*(line2.c - line1.c)/(line1.a - line2.a) + line1.c
return (x, y)