tags:

views:

30

answers:

1

Hi,

I'm currently using CGContextMoveToPoint, CGContextAddLineToPoint & CGContextStrokePath to draw lines on a view.

Basically making a snake game that also allows user to move diagonally.

Is there a way to find when 2 lines that I draw intersect or do I have to manually calculate each lines and see if anything intersects?

Thanks,
Tee

A: 

Since the CG methods are somewhat CPU heavy, your best bet is to write a method that calculates an intersection point using basic geometry. Or use the Sweep Line algorithm, it shouldn't be too hard.

EDIT: In fact, since you're making a snake game wherein blocks are laid out in a grid, you can just flag some of the blocks as containing a snake/whatever and if a block is about to be flagged twice, then you do whatever you wanted to do upon line intersection.

Arseniy Banayev