Rect draws inside given cords. As intended?

I am building a frame object to serve to translate and scale some other objects. In this, I had it draw itself by drawing

rect(0,0,w,h)

after translating to the frame’s assigned origin. Drew a line inside, representing a graph, and found that tho it started at 0,0, there was a little dot apparently outside the rectangle. Drew colored lines from 0,0 to w and h manually, and sure enough they are outside.

So it appears that rect makes sure not to actually draw in the border? Yes? Is this as planned?

Thanks!

You might need to post some code. I’m not sure what you are describing. You can draw a line on top of a rect.

.@RonJeffries it sounds like you are encountering antialiasing at the edges of your rectangle.

To have your rectangle draw exactly to the edges, try calling noSmooth() to disable smooth graphics.

Here’s a demo. It draws a white rectangle, then draws red lines at all four corners. You’ll see that some of the white rectangle shows inside the red lines. It appears that the red does cover some of the white, but definitely not all the white.


-- demo

-- Use this function to perform your initial setup
function setup()
end

-- This function gets called once every frame
function draw()
    background(40, 40, 50)
    translate(40,40)
    strokeWidth(5)
    noSmooth()
    stroke(255)
    rect(0,0,400,300)
    stroke(255,0,0)
    line(0,0,100,0)
    line(300,0,400,0)
    line(400,0,400,100)
    line(400,200,400,300)
    line(400,300,300,300)
    line(100,300,0,300)
    line(0,300,0,200)
    line(0,100,0,0)
end

If you set strokeWidth large, you can see that the white is drawn only inside the rectangle, not centered on the line (that’s probably good. perhaps there is a setting for that).

If you set strokeWidth to 1, I think there is still white inside the red, but at retina resolution with my old eyes, I’m not sure.

Anyway, the rectangle is clearly inside the lines. And, again … is this intended. It certainly could be.

Thanks!

Hi @RonJeffries, this is because Codea draws a rectangle with those co-ordinates, and uses the stroke inside those co-ordinates, so they don’t extrude from the bounding box, a line however, extrudes it’s stroke outwards, so if you shift the lines towards the center of the rect by half the stroke width, it should give you the desired results




-- demo

-- Use this function to perform your initial setup
function setup()
end

-- This function gets called once every frame
function draw()
    background(40, 40, 50)
    translate(40,40)
    strokeWidth(5)
    noSmooth()
    stroke(255)
    rect(0,0,400,300)
    stroke(255,0,0)
    line(0,2.5,100,2.5)
    line(300,2.5,400,2.5)
    line(397.5,0,397.5,100)
    line(397.5,200,397.5,300)
    line(400,297.5,300,297.5)
    line(100,297.5,0,297.5)
    line(2.5,300,2.5,200)
    line(2.5,100,2.5,0)
end

I see that it does that. Wondering if intended. Guess it doesn’t matter now, they probably couldn’t change it without trauma. Document it, perhaps.

Hello @RonJeffries. If you are asking questions about rect() renders, you may be interested in knowing about the undocumented features of ellipse() - see this fun with circles.

Aiee! :slight_smile: