Hey Joe, here's a brain teaser for you.
So in a function, I have these lines calling my collision function
x=0
y=1
test = polygon + [polygon[0]] //close the polygon for line segment testing
collide = util.collision(polygon,[50, 100], 10)
Polygon is a list of points similar to this
My collision function, edited for just the essentials, looks like this
collision = function(polygon, centre, radius)
for point in range(0,polygon.len-2)
x1 = polygon[point][x]
x2 = polygon[point+1][x]
y1 = polygon[point][y]
y2 = polygon[point+1][y]
denominator = sqrt((y2-y1)^2 + (x2-x1)^2)
distance = abs((y2-y1)*centre[x] - (x2-x1)*centre[y] + x2*y1-y2*x1)/denominator
end for
end function
One loop through the collision function takes about 0.3 milliseconds.
If I change the third line of code above to this
test = polygon + polygon[0] //close the polygon for line segment testing
Each loop through the collision function now takes 66 - 70 milliseconds. I have my thoughts why, but I'd love to get an expert opinion.
Russell.