You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorials/text/2d-transformations/index.mdx
+22-22
Original file line number
Diff line number
Diff line change
@@ -20,25 +20,25 @@ Processing has built-in functions that make it easy for you to have objects in a
20
20
21
21
As you know, your Processing window works like a piece of graph paper. When you want to draw something, you specify its coordinates on the graph. Here is a simple rectangle drawn with the code `rect(20, 20, 40, 40)`. The coordinate system (a fancy word for “graph paper”) is shown in gray.
22
22
23
-
<FixedImagecenterwidth={230}height={230}>
23
+
<FixedImagecenterwidth={650}height={400}>
24
24
25
-

25
+

26
26
27
27
</FixedImage>
28
28
29
29
If you want to move the rectangle 60 units right and 80 units down, you can just change the coordinates by adding to the _x_ and _y_ starting point: `rect(20 + 60, 20 + 80, 40, 40)` and the rectangle will appear in a different place. (We put the arrow in there for dramatic effect.)
30
30
31
-
<FixedImagecenterwidth={230}height={230}>
31
+
<FixedImagecenterwidth={650}height={400}>
32
32
33
-

33
+

34
34
35
35
</FixedImage>
36
36
37
37
But there is a more interesting way to do it: **move the graph paper instead**. If you move the graph paper 60 units right and 80 units down, you will get exactly the same visual result. Moving the coordinate system is called _translation_.
38
38
39
-
<FixedImagecenterwidth={240}height={240}>
39
+
<FixedImagecenterwidth={650}height={600}>
40
40
41
-

41
+

42
42
43
43
</FixedImage>
44
44
@@ -79,7 +79,7 @@ Yes, you could have done a `translate(-60, -80)` to move the grid back to its or
79
79
80
80
You may be thinking that picking up the coordinate system and moving it is a lot more trouble than just adding to coordinates. For a simple example like the rectangle, you are correct. But let's take an example of where `translate()` can make life easier. Here is some code that draws a row of houses. It uses a loop that calls a function named `house()`, which takes the _x_ and _y_ location of the house's upper-left corner as its parameters.
81
81
82
-
<FixedImagecenterwidth={358}height={81}>
82
+
<FixedImagecenterwidth={400}height={100}>
83
83
84
84

85
85
@@ -129,15 +129,15 @@ void house(int x, int y)
129
129
130
130
In addition to moving the grid, you can also rotate it with the `rotate()` function. This function takes one argument, which is the number of _radians_ that you want to rotate. In Processing, all the functions that have to do with rotation measure angles in radians rather than degrees. When you talk about angles in degrees, you say that a full circle has 360°. When you talk about angles in radians, you say that a full circle has 2π radians. Here is a diagram of how Processing measures angles in degrees (black) and radians (red).
131
131
132
-
<FixedImagecenterwidth={158}height={157}>
132
+
<FixedImagecenterwidth={650}height={400}>
133
133
134
-

134
+

135
135
136
136
</FixedImage>
137
137
138
138
Since most people think in degrees, Processing has a built-in `radians()` function which takes a number of degrees as its argument and converts it for you. It also has a `degrees()` function that converts radians to degrees. Given that background, let's try rotating a square clockwise 45 degrees.
139
139
140
-
<FixedImagesidewidth={120}height={120}>
140
+
<FixedImagesidewidth={200}height={200}>
141
141
142
142

143
143
@@ -164,9 +164,9 @@ void setup()
164
164
165
165
Hey, what happened? How come the square got moved and cut off? The answer is: the square did not move. The **grid** was rotated. Here is what really happened. As you can see, on the rotated coordinate system, the square still has its upper left corner at (40, 40).
@@ -178,15 +178,15 @@ The correct way to rotate the square is to:
178
178
2. Rotate the grid π/4 radians (45°)
179
179
3. Draw the square at the origin.
180
180
181
-
<FixedImagecenterwidth={310}height={250}>
181
+
<FixedImagecenterwidth={650}height={600}>
182
182
183
-

183
+

184
184
185
185
</FixedImage>
186
186
187
187
And here is the code and its result, without the grid marks.
188
188
189
-
<FixedImagesidewidth={120}height={120}>
189
+
<FixedImagesidewidth={200}height={200}>
190
190
191
191

192
192
@@ -219,7 +219,7 @@ void setup()
219
219
220
220
And here is a program that generates a wheel of colors by using rotation. The screenshot is reduced to save space.
221
221
222
-
<FixedImagesidewidth={87}height={86}>
222
+
<FixedImagesidewidth={200}height={200}>
223
223
224
224

225
225
@@ -250,7 +250,7 @@ void draw(){
250
250
251
251
The final coordinate system transformation is scaling, which changes the size of the grid. Take a look at this example, which draws a square, then scales the grid to twice its normal size, and draws it again.
252
252
253
-
<FixedImagesidewidth={145}height={145}>
253
+
<FixedImagesidewidth={200}height={200}>
254
254
255
255

256
256
@@ -284,7 +284,7 @@ There is no law saying that you have to scale the _x_ and _y_ dimensions equally
284
284
285
285
When you do multiple transformations, the order makes a difference. A rotation followed by a translate followed by a scale will not give the same results as a translate followed by a rotate by a scale. Here is some sample code and the results.
286
286
287
-
<FixedImagesidewidth={144}height={168}>
287
+
<FixedImagesidewidth={200}height={200}>
288
288
289
289

290
290
@@ -344,7 +344,7 @@ The robot is modeled on [this drawing](http://www.openclipart.org/detail/5457),
344
344
345
345
When we refer to left and right in this drawing, we mean your left and right (the left and right side of your monitor), not the robot's left and right.
346
346
347
-
<FixedImagesidewidth={84}height={136}>
347
+
<FixedImagesidewidth={200}height={200}>
348
348
349
349

350
350
@@ -379,9 +379,9 @@ void drawRobot()
379
379
380
380
```
381
381
382
-
<FixedImagecenterwidth={82}height={52}>
382
+
<FixedImagecenterwidth={650}height={250}>
383
383
384
-

384
+

385
385
386
386
</FixedImage>
387
387
@@ -426,7 +426,7 @@ void drawRightArm()
426
426
427
427
Now test to see if the arms rotate properly. Rather than attempt a full animation, we will just rotate the left side arm 135 degrees and the right side arm -45 degrees as a test. Here is the code that needs to be added, and the result. The left side arm is cut off because of the window boundaries, but we'll fix that in the final animation.
0 commit comments