Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started