OEOE.MOE

[ frontpage ]
about / projects / useful things / not very useful things
page #1 (ptl, dda) page #2 (tri) page #3 (grd, dth) page #4 (3d) page #5 (buf) page #6 (idx) scratches

Distance from point to line .raw
var x0 = 0.2;
var y0 = 0.4;
var x1 = 0.7;
var y1 = 0.6;

var px = 0.5;
var py = 0.5;

var dx = x1 - x0;
var dy = y1 - y0;

var length2 = (dx * dx) + (dy * dy);
var t = ((px - x0) * dx + (py - y0) * dy) / length2;

var ix = x0 + t * dx;
var iy = y0 + t * dy;

var d = sqrt((px - ix) * (px - ix) + (py - iy) * (py - iy));

DDA line drawing algorithm .raw
void DrawLine_DDA(int x0, int y0, int x1, int y1, u32 color)
{
    int dx = x1 - x0;
    int dy = y1 - y0;

    int length = max(abs(dx), abs(dy));
    if (length > 0) {

        f32 sx = dx / length;
        f32 sy = dy / length;

        f32 px = x0 + 0.5;
        f32 py = y0 + 0.5;

        for (int i = 0; i < length; ++i) {
            DrawPoint(floor(px), floor(py), color);

            px += sx;
            py += sy;
        }
    }
}