line and rect coordinates

I’m finding glitches when trying to draw one line to the corner of another.

How do I interpret the parameters for line and rect?

Assuming noSmooth() and strokeWidth(0) and recMode(CORNERS):

How many pixels are colored with: line (x, y, x + 10, y)?

How many pixels wide is: rect(x, y, x + 10, y + 10)?

Can I assume that the answer for both is either: 10 or 11?

X-Windows would draw 11 pixels but GDI would draw 10 pixels.

Could someone explain the drawing model for Codea (e.g. Apple documentation, underlying drawing API)?

Codea uses OpenGL ES 2 for drawing. Lines, rects and all other primitives are implemented as individual fragment shaders in the backend.

One exception is noSmooth() lines: these are implemented using the GL_LINE primitive. Which duplicates the line pixels along the line axis based on the strokeWidth() — you can’t have a 0 width line using GL_LINE.

For your example:

rectMode(CORNERS)
rect(x, y, x + 10, y + 10)

Coordinates for rectMode(CORNERS) are computed as follows:

x = x, y = y, w = (x+10), h = (y+10)
//t is a temporary

if x > w 
    t = x
    x = w
    w = t
                
if y > h 
    t = y
    y = h
    h = t                

w = w - x
h = h - y

These coordinates are then sent to OpenGL as vertex coordinates for a GL_TRIANGLE_STRIP primitive. The vertex data would look something like:

GLfloat rectVerts[] = 
{
    x,   y,
    x+w, y,
    x,   y+h,
    x+w, y+h,
}; 

You can assume it will be 10 pixels wide if:

  • you use the noSmooth() shader path.
  • you are using the identity transform
  • You should expect slightly less than 10 pixels wide if you use the smooth() shader path — this is due to the anti-aliasing in the shader.

Edit: if you use scale(8) and draw near 0,0 it will be able to see the above

So why does there appear to be a glitch when drawing a simple rectangle with lines. Try running this and watching the top-left corner as you change the “Glitch” parameter from 0 to 1.

function setup()
    iparameter ("glitch", -2, 2)
    glitch = 0
end


function draw()            
    background(255, 255, 255)

    noSmooth()    
    resetMatrix()
    strokeWidth(1)
    stroke (0, 0, 0, 255)

    local x1, x2 = 100, 306
    local y1, y2 = 220, 413
    
    line (x1, y1, x2, y1) -- bottom line
    line (x1, y2, x2, y2) -- top line
    line (x1, y1, x1, y2 + glitch) -- left line
    line (x2, y1, x2, y2) -- right side
end

Your code seems to render with the top right corner not lining up, for me (200% zoom):

GL_LINE

This looks like it might be specific to the GL_LINE implementation. I’ll investigate further after 1.3.1 is out.

Consider lodging it on the issue tracker here: https://bitbucket.org/TwoLivesLeft/codea/issues