Discuss Scratch

toyuu
Scratcher
66 posts

How to print a rectangle on screen using the pen ?

✨ Hi, iam wondering how to make a code that use the pen to create a rectangle on the screen, based on variables (XSize, YSize, XPos, YPos, Color).
I tried various things but the rect where not the right size or had round edges, thanks for your help. ✨

Also, if you want, can you tell me how to calculate the size of each rectangle so that X number of rectangles placed one after the other fill the screen?

Thanks for your help !
legendary34678
Scratcher
1000+ posts

How to print a rectangle on screen using the pen ?

To draw a rectangle with pen, you will need the starting point (top left corner), its width, and its height. You can also add its color if you want. Then, the code looks like this:

define draw rectangle at (x) (y) width (w) height (h) color (c)
pen up
go to x: (x) y: (y)
set pen color to (join [0x] (c)) //make sure c is the HEX CODE of the color
pen down
change x by (w)
change y by ((-1) * (h))
change x by ((-1) * (w))
change y by (h)
pen up

Try to figure out how the code works.
toyuu
Scratcher
66 posts

How to print a rectangle on screen using the pen ?

legendary34678 wrote:

To draw a rectangle with pen, you will need the starting point (top left corner), its width, and its height. You can also add its color if you want. Then, the code looks like this:

define draw rectangle at (x) (y) width (w) height (h) color (c)
pen up
go to x: (x) y: (y)
set pen color to (join [0x] (c)) //make sure c is the HEX CODE of the color
pen down
change x by (w)
change y by ((-1) * (h))
change x by ((-1) * (w))
change y by (h)
pen up

Try to figure out how the code works.

Thanks, but i forgot to precise i want the rectangle to be a filled rectangle.
NeonG4
Scratcher
1000+ posts

How to print a rectangle on screen using the pen ?

toyuu wrote:

snip
You need to use a triangle filler, and fill 2 triangles from the 3 corners.
legendary34678
Scratcher
1000+ posts

How to print a rectangle on screen using the pen ?

Or you can just modify it like so:

define draw rectangle at (x) (y) width (w) height (h) color (c)
pen up
go to x: (x) y: (y)
set pen color to (join [0x] (c)) //make sure c is the HEX CODE of the color
pen down
repeat until <(x position) = ((x) + (w))>
set y to (y)
change y by ((-1) * (h))
change x by (1)
end
pen up

I forgot to say this earlier, but set pen size to 1.

Last edited by legendary34678 (May 31, 2023 01:22:29)

toyuu
Scratcher
66 posts

How to print a rectangle on screen using the pen ?

legendary34678 wrote:

Or you can just modify it like so:

define draw rectangle at (x) (y) width (w) height (h) color (c)
pen up
go to x: (x) y: (y)
set pen color to (join [0x] (c)) //make sure c is the HEX CODE of the color
pen down
repeat until <(x position) = ((x) + (w))>
set y to (y)
change y by ((-1) * (h))
change x by (1)
end
pen up

I forgot to say this earlier, but set pen size to 1.


Thank you very much !
awesome-llama
Scratcher
1000+ posts

How to print a rectangle on screen using the pen ?

Note that filling a rectangle using 1px thick lines is a bit slow, if you can lessen the number of strokes, it can be made much faster. Might not matter for your use case, but just something to consider.
Have a look at some rectangle fillers made by other users for example. The fastest make use of larger pen sizes and adjust the size to efficiently fill the entire rectangle.

toyuu wrote:

Also, if you want, can you tell me how to calculate the size of each rectangle so that X number of rectangles placed one after the other fill the screen?
Divide the dimensions of the screen and divide by the number of rectangles you want to use.
The screen is 480 in width and 360 in height. If you wanted 12 rectangles along the x axis, you would do 480/12 which equals 40. You would draw 12 rectangles of width 40.

Last edited by awesome-llama (May 31, 2023 14:53:32)

Powered by DjangoBB