# Kenneth McCown & Erik Katzen # Minimum Distance Between a Point and a Line # Math 152 - 202 restart; with(Maplets[Tools]): with(Maplets[Elements]): with(plots):# with(plottools): StartEngine(); randomize(): lightorange:="#FFB300": lightcyan:="#DDFFFF": ColorFooter:=lightcyan: FontFooter:=Font("helvetica",10): pointcoord:=rand(-8..8): num1:=rand(-3..3): num2:=rand(-6..6): num3:=rand(-7..7): problem:=proc() global dist2, derDist2, answerX, answerY, finalDist; local co1, co2, co3, curve1, x1, x2, y1, y2, plotcrv, plotpt, curveplot; co1:=num1(): co2:=num2(): while co2=0 do co2:=num2(): end do; co3:=num3(): curve1:=co2*x+co3; x2:=x; y2:=curve1; answerX:=2,3; while nops([answerX])<>1 do x1:=pointcoord(); y1:=pointcoord(); dist2:=(x2-x1)^2+(y2-y1)^2; derDist2:=diff(dist2, x); answerX:=solve(derDist2=0,x); end do; answerY:=eval(curve1,x=answerX); finalDist:=sqrt(eval(dist2,x=answerX)); Set(TFcurv=curve1); Set(TFpt=[x1,y1]); plotcrv:=plot(curve1,x=-10..10, y=-10..10); plotpt:=plot([[x1,y1]], style=point, symbol=circle, symbolsize=16, color=blue); curveplot:=display([plotcrv,plotpt], axes=normal, tickmarks=[5,5], scaling=constrained); Set(CurvePlot=curveplot); Set(TFd2=""); Set(TFderiv=""); Set(TFx=""); Set(TFy=""); Set(TFd=""); Set(reply=""); end proc: hint1:=proc() Set(reply="Add the square of the difference between the x-coordinates with the square of the difference between the y-coordinates."): end proc: check1:=proc() global dist2; local user_d2; if Get(TFd2)="" then Set(reply="No answer given."); return; end if; user_d2:=Get(TFd2::anything): if simplify(dist2 - user_d2)=0 then Set(reply="You're right!"): else Set(reply="Nope, try again."): end if; end proc: show1:=proc() Set(TFd2=dist2): Set(reply="Way to just surrender there."): end proc: hint2:=proc() Set(reply="Take the derivative of D^2 using the chain rule."): end proc: check2:=proc() global derDist2; local user_deriv; if Get(TFderiv)="" then Set(reply="No answer given."); return; end if; user_deriv:=Get(TFderiv::anything): if simplify(derDist2 - user_deriv)=0 then Set(reply="Way to go!"): else Set(reply="That's not even close."): end if; end proc: show2:=proc() Set(TFderiv=derDist2): Set(reply="You did try to do it at least once, right?."): end proc: hint3:=proc() Set(reply="Find the x-coordinate that allows the derivative formula to be equal to zero."): end proc: check3:=proc() global answerX; local user_x; if Get(TFx)=""then Set(reply="No answer given."); return; end if; user_x:=Get(TFx::anything): if simplify(answerX - user_x=0) then Set(reply="You got it!"): else Set(reply="In espanol: No."): end if; end proc: show3:=proc() Set(TFx=answerX): Set(reply="C'mon, it's not that hard."): end proc: hint4:=proc() Set(reply="Plug the x-coordinate into the equation of the line."): end proc: check4:=proc() global answerY; local user_y; if Get(TFy)="" then Set(reply="No answer given."); return; end if; user_y:=Get(TFy::anything): if simplify(answerY - user_y=0) then Set(reply="You're a freakin' genius!"): else Set(reply="It could be that your life really does have a purpose...even if that purpose is just to serve as a warning to others."): end if; end proc: show4:=proc() Set(TFy=answerY): Set(reply="Try it on your own next time."): end proc: hint5:=proc() Set(reply="Plug the values of x and y into the original distance formula, y=sqrt((x2-x1)^2+(y2-y1)^2)."): end proc: check5:=proc() global finalDist; local user_d; if Get(TFd)="" then Set (reply="No answer given."); return; end if; user_d:=Get(TFd::anything): if simplify(finalDist - user_d=0) then Set (reply="You're the next Einstein!"): else Set (reply="Just look at it this way...we can't all be rocket scientists when we grow up."): end if; end proc: show5:=proc() Set(TFd=simplify(finalDist)): Set(reply="Don't give up so fast."): end proc: DistCurve:=Maplet(onstartup=RunWindow(MAIN), Font[helv10]("helvetica",10), Window[MAIN](title="Minimum Distance Between a Point and a Line", [ [ Button("New Problem", Evaluate(function=problem), background=magenta), HorizontalGlue(), Button("Quit", Shutdown(), background=pink) ], [ border=true, "Find the point on the line y =", TextField[TFcurv](width=16, editable=false, background=white), "that is closest to the point", TextField[TFpt](width=6, editable=false, background=white), "and solve for that minimum distance." ], [ halign=none, inset=0, spacing=0, [ halign=none, inset=0, spacing=0, [ border=true, caption="Enter the square of the Distance Formula:", "D^2 =", TextField[TFd2](width=17, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="hint1"), background=yellow), Button("Check", Evaluate(function="check1"), background=green), Button("Show", Evaluate(function="show1"), background=lightorange) ], [ border=true, caption="Enter the derivative of the formula:", "d/dx D^2 =", TextField[TFderiv](width=17, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="hint2"), background=yellow), Button("Check", Evaluate(function="check2"), background=green), Button("Show", Evaluate(function="show2"), background=lightorange) ], [ border=true, caption="Find the x-coordinate which gives minimum distance:", "x =", TextField[TFx](width=8, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="hint3"), background=yellow), Button("Check", Evaluate(function="check3"), background=green), Button("Show", Evaluate(function="show3"), background=lightorange) ], [ border=true, caption="Find the y-coordinate which gives minimum distance:", "y =", TextField[TFy](width=8, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="hint4"), background=yellow), Button("Check", Evaluate(function="check4"), background=green), Button("Show", Evaluate(function="show4"), background=lightorange) ], [ border=true, caption="Find the distance between the origin and this point along the curve:", "D =", TextField[TFd](width=10, background=turquoise), HorizontalGlue(), Button("Hint", Evaluate(function="hint5"), background=yellow), Button("Check", Evaluate(function="check5"), background=green), Button("Show", Evaluate(function="show5"), background=lightorange) ] ], [ border=true, caption="Plot", Plotter[CurvePlot](width=200, height=200) ] ], [ halign=none, TextField[reply](width=65, editable=false, background=white) ], [ halign=none, background=ColorFooter, Label("Programmers: Kenneth McCown & Erik Katzen", font=FontFooter), HorizontalGlue(), Label("Copyright: P. Yasskin 2008", font=FontFooter) ] ] ) ): Maplets[Display]( DistCurve );