Note: Cross lines is not good enough. If time delay will be quite big the collision with other dynamic bodies can be detected. It is not critical in scope of this game. Snake game do not have other dynamic body. Only snake.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It is possible use cross lines to check is snake moving head vector cross border.
Cross lines algorithm sample
http://algolist.manual.ru/maths/geom/intersect/lineline2d.php
One more sample with lines
http://e-maxx.ru/algo/segments_intersection_checking
Note: Cross lines is not good enough. If time delay will be quite big the collision with other dynamic bodies can be detected. It is not critical in scope of this game. Snake game do not have other dynamic body. Only snake.
Cross line code sample:
BOOL CrossLine(long x1, long y1, long x2, long y2, long x3, long y3, long x4, long y4, long &x, long &y) {
long dx1 = x2 - x1;
long dy1 = y2 - y1;
long dx2 = x4 - x3;
long dy2 = y4 - y3;
x = dy1 * dx2 - dy2 * dx1;
if(!x || !dx2)
return
false;
y = x3 * y4 - y3 * x4;
x = ((x1 * y2 - y1 * x2) * dx2 - y * dx1) / x;
y = (dy2 * x - y) / dx2;
return
((x1 <= x && x2 >= x) || (x2 <= x && x1 >= x)) && ((x3 <= x && x4 >= x) || (x4 <= x && x3 >= x));
}