Flat Shapes
Our First Shape
Click Run below to run this Python code:
That’s a rectangle. Rect is a Python function, and calling it is enough to
draw it, no extra steps. Now edit the numbers above and click Run a few
times. Watch how the rectangle changes shape and moves. If something stops
working, click Reset to start over.
The Flat Grid
Every shape here needs to know where to go. We use (x, y) coordinates for
that:
(0, 0)is the center of the grid.- As
xincreases, you head right. - As
yincreases, you head down.
That last one trips people up the first time, so slow down on it. On paper,
you’re used to “up” meaning a bigger number, like a graph in math class.
Here, y works the opposite way: bigger y means further down. This
matches how a lot of drawing tools work, this one included.
If you increase a shape's y value, which way does it move on the screen?
Rectangles
Here is the code from above again:
Rect(1, 1, 4, 3)
In full, Rect takes four values, all required, in this order: left,
top, width, height. left and top are the coordinates of the
rectangle’s top-left corner. width and height say how big it is
from there. So the code above draws a rectangle whose top-left corner sits
at (1, 1), 4 units wide and 3 units tall.
That’s worth slowing down on too: a Rect is not centered on the point you
give it. The point you give it is a corner, and the rectangle grows to the
right and down from there.
Question: if you call Rect(2, 1, 4, 3), where is the corner opposite
(2, 1)? (Hint: which two values do you add, and to which two numbers?)
Which call draws a rectangle 4 wide and 3 tall, with its top-left corner at x=2, y=1?
Circles
Circle is the other shape you’ll use here:
Circle takes three values: centerX, centerY, radius. Notice the
first two are called centerX/centerY, not left/top. That’s not just
a naming choice: a Circle is anchored by its center, not a corner.
There isn’t really a “corner” of a circle to anchor it by.
Rect(2, 1, 4, 3) and Circle(2, 1, 4) both start with the numbers 2, 1. Why do they end up in different places?
You can also color a shape in with fill=:
Checking Your Work
Exercise: draw a rectangle 4 wide and 3 tall with its
top-left corner at (1, 1), and a circle with radius 2
centered at (-3, -3).
Practice
That’s it for flat shapes! One thing to know before you move on: in the
Studio (the full sandbox tool), Rect and Circle work a little
differently. There, they describe a flat outline, and nothing shows up
until you call extrude(...) to give it real height. We kept things
simple here on purpose, so you could focus on positioning first.
Here’s a small preview of that, working right now – with a bonus shape,
Label, that draws text:
extrude(shape, height) takes a shape you already drew and gives
it a real height instead of the thin default. This one viewer, just for
this example, lets you drag to rotate, so you can actually see that
height. (Every other viewer in this lesson stays locked flat, since this
lesson is about a flat page.) Go ahead and change "Mr. Brown" to your
own name. You’ll meet extrude (and Label’s full set of
options) for real once you get to the Studio and the Cheatsheet – along
with a second kind, rotate_extrude, once you’re further along.
From here, move on to real 3D shapes with height, or jump straight to the Studio or Cheatsheet.