Python 3!! By creating and using the helper functions 1-8, build a step by step
ID: 3752324 • Letter: P
Question
Python 3!!
By creating and using the helper functions 1-8, build a step by step solution to the main function which implements all of the given helper functions. The notes under each function name will serve as a guide to writing the code. Do not use numpy, or any other packages.
The algorithm to solving the main function is as follows:
Let L1 and L2 be lines in 2-space.
Let H1= (a1, b1, c1) be a homogeneous representation of the line L1, and let H2= (a2, b2, c2) be a homogeneous representation of the line L2.
Then the intersection of L1 and L2 is the point H1×H2 (cross product) in projective 2-space.
1. def Unit (v):
"""Normalize a vector.
Params: v (float 2-tuple): a vector in 2-space
Returns: (float 2-tuple) unit vector (Euclidean length 1) in same direction
"""
return
2. def PPtoPV (PP):
"""Translate from 2-point to point-vector representation of a line.
Params: PP (float 4-tuple): 2-point representation of a line: px,py,qx,qy
Returns: (float 4-tuple): point-vector rep of the line: px,py,vx,vy
note that the vector is unit
"""
return
3. def PVtoPN (PV):
"""Translate from point-vector to point-normal representation of a line.
Alg: if V = (v0,v1), N = (-v1,v0)
(so their inner product is 0, so their angle is pi/2)
Params: PV (float 4-tuple): point-vector rep of a line: px, py, vx, vy
the vector is unit
Returns: (float 4-tuple): point-normal rep of the line: px, py, nx, ny
the normal is unit
"""
return
4. def PNtoHomog (PN):
"""Translate from point-normal to homogeneous representation of a line.
Params: PN (float 4-tuple): point-normal rep of a line: px, py, nx, ny
the normal is unit
Returns: (float 3-tuple): homogeneous representation (a,b,c) of the line
"""
return
5. def PPtoHomog (L):
"""Translate from 2-point to homogeneous representation of a line.
Params: L (float 4-tuple) 2-point representation (x1,y1,x2,y2) of a line
Returns: (float 3-tuple) homogeneous representation (a,b,c) of the line
"""
return
6. def cross (a,b):
"""Cross product of 2 vectors in 3-space (or projective 2-space).
(Foreshadowing: this is a built-in function in numpy.)
Params: a (float 2-vector) a vector in 2-space
Params: b (float 2-vector) a vector in 2-space
Returns: a x b
"""
return
7. def LiLiIntersect_proj (L1, L2):
"""Intersect two lines in projective space.
Params:
L1 (float 4-tuple): first line, defined by two points x1, y1, x2, y2
L2 (float 4-tuple): second line, also defined in 2-point rep
Returns: (3-tuple) intersection point in projective 2-space
(this intersection is at infinity if its 3rd coord is 0)
"""
return
8. def ProjToEuc (P):
"""Translate a fine point in projective 2-space to Euclidean 2-space.
Params: P (float 3-tuple) finite point in projective 2-space
Returns: (float 2-tuple) associated point in Euclidean 2-space
"""
return
MAIN FUNCTION= def LiLiOblique (L1, L2):
return
"""Intersect two oblique lines in Euclidean space.
Since two lines are oblique (not parallel), intersection is finite.
Params:
L1 (float 4-tuple): first line, defined by two points x1, y1, x2, y2
L2 (float 4-tuple): second line, also defined in 2-point rep
Returns: (float 2-tuple) intersection point in Euclidean space
"""
Explanation / Answer
import math
def Unit(v):
# unit vector of a vector = (x/sqrt(x^2 + y^2), y/sqrt(x^2 + y^2))
v1 = math.sqrt((v[0]*v[0]) + (v[1]*v[1]))
unit = (v[0]/v1, v[1]/v1)
return unit
def PPtoPV(PP):
# vector of a line is (x2-x1, y2-y1)
return (PP[0], PP[1], PP[2]-PP[0], PP[3]-PP[1])
def PVtoPN(PV):
# normal of a vector is (-y, x)
return (PV[0], PV[1], -PV[3], PV[2])
def PNtoHomog(PN):
# If the given normal vector is (p, q, x, y)
# then its homogeneous form is
# a = -x
# b = -y
# c = qy + px
den = PN[1] * PN[3] + PN[0] * PN[2]
a = -PN[2]
b = -PN[3]
c = den
return (a, b, c)
def PPtoHomog(PP):
# If the given points are is (p1, q1, p2, q2)
# then its homogeneous form is
# a = q2 - q1
# b = p2 - p1
# c = q1p2 - p1q2
den = PP[1] * PP[2] - PP[0] * PP[3]
a = PP[3] - PP[1]
b = PP[0] - PP[2]
c = den
return (a, b, c)
def cross(a, b):
# cross product of two vectors that are parallel to XY plane is
# (0, 0, ax*by - ay*bx)
return (0, 0, a[0] * b[1] - a[1] * b[0])
def LiLiIntersect_proj (L1, L2):
a = PPtoHomog(L1)
b = PPtoHomog(L2)
den = a[0] * b[1] - b[0] * a[1]
x = (a[1] * b[2] - b[1] * a[2]) / den
y = (b[0] * a[2] - a[0] * b[2]) / den
return (x, y, 1)
def ProjToEuc(P):
# conversion of proj to euc is
# (x/z, y/z)
return (P[0]/P[2], P[1]/P[2])
def LiLiOblique (L1, L2):
# find the intersection in projection space
# convert the answer to euclidean space
proj = LiLiIntersect_proj (L1, L2)
return ProjToEuc(proj)
L1 = (2, 3, 4, 5)
L2 = (4, 5, 6, 7)
LiLiOblique (L1, L2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.