For a horizontal or vertical line (such as the edges of the screen) this is almost trivial, as only the x or y coordinates need be considered for each. For example to check whether a circle lies entirely inside a rectangle, the following could be used (a member function of the class used on Monday). It is written to avoid the accessors of the Rectangle class, for performance reasons
function inRect(r:Rectangle):Boolean {
// get x and y as are needed more than once
var rX:Number = r.x, rY:Number = r.y;
return (fX > rX + fR && fX < rX + r.width - fR
&& fY > rY + fR && fY < rY + r.height - fR);
}
Handling an general line is a little more complex, but not by much. There are a few ways to represent a general line, but in this example the endpoints are given. I.e. the line is the line through two distinct points (x1, y1) and (x2, y2).
function hitLine(x1:Number, y1:Number,
x2:Number, y2:Number):Boolean {
// calculate the unit vector along the line
var dX:Number = x1 - x2;
var dY:Number = y1 - y2;
var len:Number = Math.sqrt(dX * dX + dY * dY);
var recip:Number = 1 / len;
dX *= recip;
dY *= recip;
// perp dot product:
return Math.abs((fX - x1) * dY - (fY - y1) * dx) < fR;
}
No comments:
Post a Comment