Game Development Reference
In-Depth Information
if rectX + rectWidth >= circleX - circleRadius and
rectX <= circleX + circleRadius then
return true
else
return false
end
else
if rectX < circleX then
if rectY < circleY then
return pointInCircle( rectX + rectWidth, rectY + rectHeight,
circleX, circleY, circleRadius )
else
return pointInCircle( rectX + rectWidth, rectY,
circleX, circleY, circleRadius )
end
else
if rectX < circleY then
return pointInCircle( rectX, rectY + rectHeight,
circleX, circleY, circleRadius )
else
return pointInCircle( rectX, rectY, circleX, circleY, circleRadius )
end
end
end
Using pointInTriangle
In some cases, triangles will suit your needs better than rectangles or circles. There are plenty of
methods for checking the bounding values of a triangle. However, the one described following is the
simplest. It simply extends lines from each of the triangle's vertices to the point we are checking,
thereby dividing the triangle into three triangles; it then calculates the area of each of these triangles.
If totalArea is the same as the area of the three new triangles created, then the point can be said to
be in the bounding triangle. Here's the function that determines whether a point is inside a triangle:
function triangleArea( pt1x, pt1y, pt2x, pt2y, pt3x, pt3y )
local dA = pt1x - pt3x
local dB = pt1y - pt3y
local dC = pt2x - pt3x
local dD = pt2y - pt3y
return 0.5 * math.abs( ( dA * dD ) - ( dB * dC ) )
end
function pointInTriangle(ptx, pty, pt1x, pt1y, pt2x, pt2y, pt3x, pt3y)
local areaT = triangleArea(pt1x, pt1y, pt2x, pt2y, pt3x, pt3y)
local areaA = triangleArea(ptx, pty, pt1x, pt1y, pt2x, pt2y)
local areaB = triangleArea(ptx, pty, pt3x, pt3y, pt2x, pt2y)
local areaC = triangleArea(ptx, pty, pt1x, pt1y, pt3x, pt3y)
return (areaT == (areaA + areaB + areaC))
end
 
Search WWH ::




Custom Search