Question: Write a function ( not a method ) , closest _ point _ on _ line _ segment ( p , a , b )
Write a function not a method closestpointonlinesegment p a b which returns a Vec
object that is the closest point to vec on the line segment formed by vec and vec Parameters a and a
all VecD objects.
There is a fairly simple bit of vector arithmetic we can use. The geometric arrangement is shown
Figure The algorithm is is :
Compute vecvecvec
Compute vec
If set or if set
Compute vectvec
In words, the closestpoint algorithm does the fol
lowing: Forms a vector from vec to vec and calls
that vec Finds the projection of the vector from
vec to vec onto the vector vec and scales the projected
length by Clips the value of to or
if it is outside the range What this does
is locks onto end points vec or vec if vec projects be
yond their extents. Finds point vec as the linear
weighting between end points vec and vec
Sample
from math import sqrt
a VecD
b VecDsqrtsqrt
p VecDsqrt
c closestpointonlinesegmentp a b
c
VecD
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
