The Essential Guide to HTML5

(Greg DeLong) #1

MAZES


Code Explanation


} Close function


Function intersect(sx,sy,fx,fy,cx,cy,rad) { Function header intersect


var dx; For intermediate value


var dy; For intermediate value


var t; For expression in t


var rt; For holding distance squared


dx = fx-sx; Set x difference


dy = fy-sy; Set y difference


t =0.0-((sx-cx)dx+(sy-cy)dy)/
((dxdx)+(dydy));


Taking the formula for the distance
squared from each point to cx,cy. Take
derivative and solve for 0.

if (t<0.0) { If closest is at t <0


t=0.0; } Check at 0 (this will be further)


else if (t>1.0) { If closest is at t>1


t = 1.0; Check at 1 (this will be further)


} Close clause


dx = (sx+t*(fx-sx))-cx; Compute difference at this value of t


dy = (sy +t*(fy-sy))-cy; Compute difference at this value of t


rt = (dxdx) +(dydy); Compute distance squared


if (rt<(rad*rad)) { Compare to rad squared


Return true; } Return true


else { Else

Free download pdf