Being in a relative world...

dhk's icon

...can be frustrating sometimes, when all you want is to draw boxes and lines at absolute (and integer) pixel positions.

Like in the example below. After some experimentation I managed to get frames at a regular interval, whatever interval or number of frames. But specifying lines with screentoworld seems more difficult. They don't appear where I want them to.

I've programmed them to appear at regular intervals, and the pixel positions are put out into the max window when using the number box. The resulting lines don't cohere with the numbers ? there should always be a one pixel gap between the box and the line. You can also (on the mac) measure the pixels using the Digital Color Meter.

What's wrong? Seems like some rounding error, since half the number of lines are correct and the other half is wrong.

Max patch:

Max Patch
Copy patch and select New From Clipboard in Max.

Javascript, save as lines-in-boxes.js:

sketch.default2d();
sketch.fsaa = 0

var mybrgb = [0.7,0.9,0.8];
var myfrgb = [0.,0.,0.];

var numitems = 3;

var boxwidth = 84;
var frameheight = 7;
var boxheight = (frameheight * numitems) + numitems + 1;
var relnum = 2 / numitems;
z = 0;

var asp = (boxwidth / boxheight);

box.size(boxwidth, boxheight);

draw();
tekst();

function tekst()
{
    with (sketch) {
        glcolor(myfrgb);        

        for(i=0;i
        //    moveto(-asp + 0.1, 1 - (i * relnum) - (0.2 * relnum));    
        //    lineto(-asp + 0.9, 1 - (i * relnum) - (0.2 * relnum));        // world method

            moveto(screentoworld(5, 2 + ((frameheight + 1) * i)));
            lineto(screentoworld(20, 2 + ((frameheight + 1) * i)));        // screen method

            post("line", i + 1 +":", 2 + ((frameheight + 1) * i), " - ");
            }
    refresh();    
    }
    post()
}

function draw()
{
    with (sketch) {
        glclearcolor(mybrgb);
        glclear();

        glcolor(myfrgb);
        framequad(-asp,1,z,asp,1,z,asp,-1,z,-asp,-1,z);

        x = 2 / numitems;
        textalign("left","center");    
        for(i=1;i
            moveto(-asp, (i * x) - 1);
            lineto(asp, (i * x) - 1);
            }
     refresh();    
    }
}

function msg_int(i)
{
    post()
    numitems = i;
    relnum = 2 / numitems;
    boxheight = (frameheight * numitems) + numitems + 1;
    asp = (boxwidth / boxheight);
    box.size(boxwidth, boxheight);
    dorefresh();
}

function dorefresh()
{
    draw();
    tekst();
    refresh();
}