jeudi 25 septembre 2008

Usefull scripts

Programming around for the nds is fun.
Sometimes you need something like "how to test if a point is within a triangle".
Well you can start thinking yourself, which is not bad, but other people have done it before, and still other people have refined their methods, so sometimes it is nice to use these tested scripts and go on with the game.
I found this site:
http://gmlscripts.com/

you have to rewrite the script in the language you are programming in, but that is easy :-)

ok for my problem, the triangle and the point:

{
var x1, y1, x2, y2, x3, y3, x4, y4, a, b, c;
x1 = argument[0];
y1 = argument[1];
x2 = argument[2];
y2 = argument[3];
x3 = argument[4];
y3 = argument[5];
x4 = argument[6];
y4 = argument[7];
a = (x1 - x4)*(y2 - y4) - (x2 - x4)*(y1 - y4);
b = (x2 - x4)*(y3 - y4) - (x3 - x4)*(y2 - y4);
c = (x3 - x4)*(y1 - y4) - (x1 - x4)*(y3 - y4);
return (sign(a) == sign(b) && sign(b) == sign(c));
}

this method uses three line-equations in an elegant way and tests the side of the lines, then the hypothesis that if two pairs of signs are the same, then the third has to agree, very funny!

Some info on the math.h functions is also usefull, but can be found practically everywhere.