In the attached image.
How can we find the position values of point Q1 and Q2.
this is my code but i am not able to get the points correctly.
How can we find the position values of point Q1 and Q2.
dim u as vertex
u = CVertex(P3.x - P4.x , P3.y - P4.y , 0.0)
dim v as vertex
v = CVertex( -u.y , u.x , v.z)
dim t1 as vertex
t1 = CVertex( (P1.x - P2.x) * v.x , (P1.y - P2.y) * v.y , 0.0)
dim t2 as vertex
t2 = CVertex( (P1.x - P3.x) * v.x , (P1.y - P3.y) * v.y , 0.0)
dim t as vertex
t = CVertex( t1.x / t2.x , t1.y / t2.y , 0.0)
dim Q1 as vertex
Q1.x = P1.x + t.x * (P3.x - P1.x)
Q1.y = P1.y + t.y * (P3.y - P1.y)
>Solution :
We make some vectors – for example, P31 = P3 - P1 and so on.
Points Q1, Q2 might be expressed as
Q1 = P1 + t * P31
Q2 = P1 + t * P41
where t is parameter in range 0..1 (for t=1 Q1 coincides with P3, Q2 coincides with P4).
Vectors q2-q1 and p2-q1 must be parallel, because Q1Q2 segment contains P2. So vector product of these vectors is zero. We express vector product using given vector components and solve simple linear equation to get t parameter that provides needed conditions.
Python code:
def segintri(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y):
p21x = p2x - p1x
p21y = p2y - p1y
p31x = p3x - p1x
p31y = p3y - p1y
p41x = p4x - p1x
p41y = p4y - p1y
p43x = p4x - p3x
p43y = p4y - p3y
t = (p43x * p21y - p43y*p21x) / (p43x * p31y - p43y*p31x)
q1 = (p1x + p31x*t, p1y + p31y*t)
q2 = (p1x + p41x*t, p1y + p41y*t)
return (q1, q2)
print(segintri(1, 5, 2, 2, 0, 0, 6, 0))
>>>((0.4, 2.0), (4.0, 2.0))

