cosine and negative sine of a direction are the normalized components of the vector that points in that direction. When you normalize a vector, you have a new vector which points in the same direction, but has a length of 1.

so every time you go through that loop (and there is nothing in the way), you move exactly 1 pixel in the direction of the vector that points from (xprevious,yprevious) to (x,y).

Note: repeating the loop dist times will only give exact results if dist is an integer. What you could do to fix this is repeat while dist > zero, reduce dist by 1 every time through the loop, and increase x and y by "xdir" and "ydir" multiplied by min(1,dist), that is to say, 1 or dist, whichever is smaller.

Another way to normalize a vector is to divide each component by the length of the vector. So another way to get xdir, and ydir, without having to use trig or point_direction would be to do this:

dist = point_distance(xprevious,yprevious,x,y);

xdir = (x - xprevious) / dist;

ydir = (y - yprevious) / dist;