﻿ CodeForYoungs # Sample Object Placement Algorithm with Scratch

In programming, you may need to arrange objects on the screen at equal intervals in some cases. It is difficult to do this with one hand and it is very time consuming. For this purpose, if an algorithm like this is developed and the code is written accordingly, our work will be easier in many respects.

In here our aim here is requesting rows and columns  numbers  from the user, and copy and arrange them evenly according to the number of rows and columns on the screen.

### Step 1: Determining the coordinate values ​​of the screen where we will sort the shapes

Since we will do this program in Scratch, we first need to know the standard coordinate measurements in Scratch. The Scratch screen has 480 pixels horizontally and 360 pixels vertically. The coordinate values ​​of the Scratch screen are as follows. ### Step 2:Determining the variables required for shape sorting

first_x: x coordinate of the first object to be created

first_y: y coordinate of the first object to be created

interval_x: Horizontal distance between two objects

interval_y: Vertical distance between two objects

horizontal_n: Number of horizontal rows

vertical_n: Number of vertical rows

counter_x: Counter to sort horizontal objects

counter_y: Counter to sort vertical objects

first_control: Variable specifying the first object to create

For example, in a screen shot of  arranged 4x3 objects, some of these variables are shown: In here;

horizontal_n=3

vertical_n=4

Horizontal distance between objects variable interval_x is calculated by

interval_x = 480/(vertical_n+1) formula because, for example, if there are 4 coloumns vertically as above, horizontal length between objects is divided into 5 equal parts.

Vertical distance between objects variable interval_y is calculated by

interval_y = 360/(horizontal_n+1) formula because, for example if there are 3 rows horizontally as above, vertical length between objects is divided into  4 equal parts. h

### Step 3: Preparing and coding the algorithm of the program

There are 2 objects in the program. These are the character that asks the question and the object to be copied and placed. According to this;

The algorithm given to the character asking the question is:

1. Start

2. Sync variables first_x, first_y, interval_x, interval_y, horizontal_n, vertical_n to 0

3. Ask "How many coloumns will be created?"

5. Ask "How many rows will be created?"

7. if horizontal_n >0 and vertical_n >0  then

8.    interval_x = 480/(vertical_n+1)

9.    first_x = -240 + interval_x

10.  aralik_y = -360/(dikey_n+1)

11.  first_y= -180 + interval_y

12.  release "run"

The algorithm for the object to be copied and sorted is as follows:

1. Start (whed "run" broadcast comes)

2. Set object size

3. Show

4. Go x=first_x y=first_y coordinate

5. Sync variables first_control, counter_x, counter_y to 0

6. Repeat until counter_y = vertical_n

7.   Repeat until counter_x = horizontal_n

8.     counter_x = counter_x + 1

9.     if first_control>0 then

10.        create clone of self

11.        when I was start a clone; Go x=first_x+(counter_x-1)*interval_x,  y=first_y+counter_y*interval_y  coordinate

12.    first_control = first_control +1

13.  counter_y = counter_y + 1

14.  counter_x = 0

Summary,

According to algorithms above , first  number of rows and columns will be requested from the user, horizontal and vertical distances between objects will be calculated, and first_x and first_y variables will be calculated as the coordinates of the first object to be created by horizontal and vertical distance. Then first object will be created in these coordinates. The first object will be created at left head on the bottom line. Then the objects will be copied from left to right, starting from the bottom row, and placed according to the calculated coordinate.

To view the full version of this sample which made in Scratch, click the link below.

If you can create a different and shorter algorithm, we expect you to share it in the comments.

865 views