Raycasting is the process of drawing a line from some point to measure the distance to the nearest solid. It has a huge number of uses, with beams and lasers being perhaps the most common. However raycasting is also used to measure angle relative to a solid, testing which solid is the closest, etc.
Here is my optimized raycasting function. It's fully commented and has two major optimizations - a system to pick only the relevant solids before raycasting, and a halving increment system to decrease the required iterations.
Feel free to use this in your projects! Credit is appreciated. Let me know if you have any questions or suggestions! I'm always looking for improvements.
[Updated 20/10/2014. Fixed bug where max range was higher than the max range parameter, and implemented Magistross' method for stopping the loop to slightly improve performance.]
The capx file is attached. (Over there, up and to the left)