Pen¶

The pen extension is a drawing module. It adds ix nighttime light-green blocks.

../_images/pen_blocks.png

The pencil sprite¶

We are going to use the Pencil sprite. In order to make the line appear at the point of the pencil, you must go to the sprite editor, select the whole drawing (cmd+A) and identify the tip of the pencil at the origin marking.

../_images/pen_sprite.png

Depict a line¶

Now we tin can place the pencil somewhere on the phase and draw a line. To do so nosotros:

  • erase all
  • set the pen color (red)
  • set the pen size (3 points)
  • put the pen down
  • movement 100 steps

../_images/pen_move.png

This is the result, fatigued on the backdrop with xy axes.

../_images/pen_move2.png

RGB colors¶

The set pen colour to () block has a drib-downwardly carte to select the color via:

  • colour
  • saturation
  • brightness

../_images/pen_color.png

It is also possible to provide the color with an integer variable. Most computers represent color with the 3 components:

  • red
  • green
  • bluish

This is besides chosen the RGB color system. The three components are expressed with a byte-sized value. The intensity of each color component can go from 0 to 255.

Each color component occupies i byte and the 3 components can be combined into a 3-byte integer. When expressed as hexadecimal digit, the RGB color number has this format:

For example:

  • 0xFF0000 is crimson
  • 0x00FF00 is light-green
  • 0x0000FF is blue

We use iii variables and express each base colour at maximum intensity.

../_images/pen_rgb_var.png

At present we tin used these variables to draw iii line segments in these base colors. - blue is 255 - greenish is 255 shifted by 8 bits (multiplied by 256) - red is 255 shifted by 16 bits (multiplied by 256*256)

../_images/pen_rgb.png

This is the result:

../_images/pen_rgb2.png

We can likewise add two base of operations colors to go a new color.

  • crimson and light-green = yellow
  • cerise and blue = magenta
  • blue and gren = cyan

../_images/pen_cym.png

This is the result:

../_images/pen_cym2.png

Move slowly¶

The Motion category has a glide to x, y block. We tin can create a similar block for gliding a certain distance.

In Scratch a loop executes 25 times each second. To concluding t seconds, it must echo t*25 times.

The distance increment of each iteration is 1/(t*25) of the total altitude.

../_images/pen_glide.png

To test the gliding move we write:

../_images/pen_glide2.png

Draw a square¶

Now we combine the irksome motility and the irksome turn block to draw a foursquare.

../_images/pen_square.png

This is the outcome.

../_images/pen_square2.png

If nosotros do not set the rotation way, the pen will turn by 90 degrees at each corner. That does non requite a natural animation style. It's better to not rotate the pen. Therefore at the starting time we fix the rotation fashion to don't rotate.

../_images/pen_rotation.png

Depict a polygon¶

We can turn this into a function polygon which tin draw whatsoever arbitrary regular polygon.

../_images/pen_polygon.png

This is the code to test for a hexagon.

../_images/pen_polygon2.png

This is the result drawn to the stage.

../_images/pen_polygon3.png

Draw a star¶

There is a simple algorithm to draw a star. Information technology's nigh like a polygon, merely it has an actress parameter one thousand.

  • n : the number of points
  • m : the number of points to spring over

../_images/pen_star.png

This is the result for a (seven, iii) star which is a seven-pointed star, jumping always to the 3rd signal.

../_images/pen_star2.png

Here is the lawmaking:

https://scratch.mit.edu/projects/397107138

Store points in a listing¶

In the following section nosotros wait at a technique to store points in a list. For this nosotros need to define 3 variables:

  • 10, y the 2d coordinates
  • i an integer index which points to the current point in the list

../_images/pen_var.png

Then we create a list called Points. This list contains the coordinates of all the points of the shape.

The image below shows the Points list with the points (0, 0), (100, 0) and (100, -50). They represent a rectangular triangle. The first indicate is repeated at the end to close the shape.

../_images/pen_points.png

We define a fonction set 10, y to add the betoken (x, y) to the Points list.

../_images/pen_set.png

The part Triangle sets the iv points of the triangle shown in a higher place.

../_images/pen_triangle.png

Load and shop¶

Operations are executed using two variables 10 and y. At whatever time, the index variable i points at a pair of coordinates in the list.

The load part loads the coordinates (x, y) from the Points list to the variables.

../_images/pen_load.png

The store role transfers the coordinates (x, y) from the variables back to the Points list.

../_images/pen_store.png

Describe a shape¶

The depict function does the following

  • set the index i to the kickoff point
  • repeat for each point
  • load the current bespeak to the variables x and y
  • go to position (x, y)
  • put the pen downwards
  • increment index i by 2

../_images/pen_draw.png

Translate a shape¶

The part add adds the vector (x, y) to the point currently pointed to by the alphabetize i.

../_images/pen_translate.png

The function translate does this for every bespeak in the Points listing. It also draws the shape.

../_images/pen_translate2.png

Now nosotros tin can employ the translation to the triangle.

../_images/pen_translate3.png

This is the issue.

../_images/pen_translate4.png

Scale a shape¶

The office mul multiplies the indicate currently pointed to past the index i with a scalar value yard. A positive bending turns counter-clockwise.

../_images/pen_scale.png

The function scale does this for every point in the Points list. It also draws the shape.

../_images/pen_scale2.png

At present we tin can apply the scaling to the triangle.

../_images/pen_scale3.png

This is the outcome.

../_images/pen_scale4.png

Rotate a shape¶

The function rot rotates the point currently pointed to by the index i by an angle a, with reference to the origin.

../_images/pen_rotate.png

The function rotate does this for every betoken in the Points list. It also draws the shape.

../_images/pen_rotate2.png

Now nosotros tin utilise the rotation to the triangle.

../_images/pen_rotate3.png

This is the result.

../_images/pen_rotate4.png

Move past (ten, y)¶

At that place are ii alter ten/y by blocks but no cake which changes both at the same time. Let's create such a office.

../_images/pen_move_by.png

At present let'due south define 1 to depict a line by an amount (x, y).

../_images/pen_line.png

Draw a grid¶

At present we have everything to draw a filigree.

../_images/pen_grid.png

We place the pen to the starting position. Now draw a 7x5 grid with a line distance d=20.

../_images/pen_grid2.png

And this is the result.

../_images/pen_grid3.png

We place the pen to another starting position, then change the color to regal and the thickness to 3. Now draw a 3xs filigree with a line altitude d=30.

../_images/pen_grid4.png

And this is the consequence.

../_images/pen_grid5.png

https://scratch.mit.edu/projects/398983654

Record pen motility¶

In this example we use a list to memorize the pen movenent. Clicking anywhere on the stage, starts drawing the line with the pen sprite, and also recording of the x, y coordinates.

../_images/pen4_stage.png

It uses the 4 variables:

  • mouse position x, y
  • mouse down state (true or faux)
  • list index i

../_images/pen4_var.png

At the start, nosotros set the color to crimson and the thickness to 3.

../_images/pen4.png

The function add point adds the electric current mouse position to the Points list.

../_images/pen4_add.png

When the stage is clicked, the message draw is circulate. This

  • moves the pen to the mouse
  • records the position
  • puts the pen downwardly

until the mouse is upwardly.

../_images/pen4_draw.png

When the Play button is pressed the recorded points are redrawn.

  • reset alphabetize i to 1
  • echo until the end of the list
  • read the 3 variables 10, y, downwardly from the Betoken list
  • to to x, y
  • if downwards is true, then pen down (otherwise up)

../_images/pen4_play.png

The Button sprite uses 2 emoticons. Pay attention to leave a space subsequently the first icon, in club to brand the frame larger. On the Android OS the icons are larger as on the iOS, and the icons are cut off on the right side. If you lot don't add the extras empty space afterwards the emoticon, the icon will exist cropped.

../_images/pen4_button.png

Make the push button hight xl points. Using ceiling we can calculate the index i of the button.

  • ane : delete
  • 2 : play

../_images/pen4_click.png

To use keyboard shortcuts on the reckoner, nosotros add this

../_images/pen4_key.png

Try it out

https://scratch.mit.edu/projects/404148380