Interesting Programming Guidance for Designer--Get Your Picture Running(Part Two)
Math, for most of you, seems useless. The most commonly used in our daily life is just add, subtract, multiply and divide. However, it is quite different if you can create with program. The more you know, the more wonderful result you will get.
Movement & Function
Let me show you several unknown pictures to stimulate your taste.


What’s this? Now just keep this question first and latter you will know and use it.
In the last chapter, we have learned function setup and function draw, which can make the static graphics become dynamic. However, this movement format is just too simple. We are going to use function knowledge we mastered before to run our graphics with their own character.



[cceN_cpp theme="dawn"]
float x, y;
void setup(){
size(300, 300);
background(0);
x = 0;
}
void draw(){
stroke(255);
strokeWeight(2);
y = pow(x, 2) / 100.0; //Function pow will return to the nth power of designation number. (x,2) represents the square of x. The first parameter is the base number and the second one is the index.
point(x, y);
x++;
}
[/cceN_cpp]
float x, y;
void setup(){
size(300, 300);
background(0);
x = 0;
}
void draw(){
stroke(255);
strokeWeight(2);
y = pow(x, 2) / 100.0; //Function pow will return to the nth power of designation number. (x,2) represents the square of x. The first parameter is the base number and the second one is the index.
point(x, y);
x++;
}
[/cceN_cpp]
Running Effect

Next, choose function sin. Formula: y = 150 + sin(x).

[cceN_cpp theme="dawn"]
float x,y;
void setup(){
size(300, 300);
background(0);
x = 0;
}
void draw(){
y = height/2 + sin(radians(x)) * 150; //Function radian transform x into angle.
x++;
stroke(255);
strokeWeight(2);
point(x, y);
}
[/cceN_cpp]
float x,y;
void setup(){
size(300, 300);
background(0);
x = 0;
}
void draw(){
y = height/2 + sin(radians(x)) * 150; //Function radian transform x into angle.
x++;
stroke(255);
strokeWeight(2);
point(x, y);
}
[/cceN_cpp]
Running Effect

To Write Function
I have listed several frequently used functions below. Hope these can help us to translate functions into code that can be recognized by computer.

y = x² → y = pow(x, 2) or y = sq(x)
y = x³ → y = pow(x, 3)
y = xⁿ → y = pow(x, n)
y = 4ⁿ → y = pow(4, n)
y =logₑ² → y = log(2)
y = e² → y = exp(2)
y = √5 → y = sqrt(5)
You can also randomly write a function into program and see what its movement track will look like. Remember to consider the range of value field and definition domain, or your graphic will run out of your screen.
Trigonometric Function
Now, let’s go further to know some writings of trigonometric functions.


The usage of function degrees is comparatively opposite. It can transform radian into angle. Input print(degrees(PI/2)) directly into the edit area , and see what you will get.
Control Graphic Movement with Trigonometric Function
Here’s a case for you to see the actual effect of graphic movement.
[cceN_cpp theme="dawn"]
float x, y;
void setup(){
size(700, 300);
}
void draw(){
background(234, 113, 107);
y = sin(radians(x)) * 150 + 150;
x++;
noStroke();
ellipse(x, y, 50, 50);
}
[/cceN_cpp]
float x, y;
void setup(){
size(700, 300);
}
void draw(){
background(234, 113, 107);
y = sin(radians(x)) * 150 + 150;
x++;
noStroke();
ellipse(x, y, 50, 50);
}
[/cceN_cpp]

Spinning Circle
Well, we have finally come into the most import part in this chapter. How to draw a circle path in a program? How to use functions to display it? Let me show you the two pictures we saw at the beginning of this article again.


Actually they have visually exposed the relationship between circumference coordinate and trigonometric function. Movement in the above pictures are driven by the constantly increasing independent variable θ . Left is the image of function sin and cos, and the right stands for a point doing circular movement after being mapped. Isn’t it very smart? It is not mysterious any more. You can use code to realize it.
A simple example:
[cceN_cpp theme="dawn"]
float x, y, r, R, angle;
void setup(){
size(300, 300);
r = 20; //Circle diameter
R = 100; //Radius of movement path
x = 0;
angle = 0;
y = height/2;
}
void draw(){
background(234, 113, 107);
translate(width/2, height/2); //Move the original point to the screen center.
noStroke();
x = R * cos(angle);
y = R * sin(angle);
ellipse(x, y, r, r);
angle += 0.05;
}
[/cceN_cpp]
float x, y, r, R, angle;
void setup(){
size(300, 300);
r = 20; //Circle diameter
R = 100; //Radius of movement path
x = 0;
angle = 0;
y = height/2;
}
void draw(){
background(234, 113, 107);
translate(width/2, height/2); //Move the original point to the screen center.
noStroke();
x = R * cos(angle);
y = R * sin(angle);
ellipse(x, y, r, r);
angle += 0.05;
}
[/cceN_cpp]


y = (The unknown expression of x?) ;
x++ ;
So we have to change our idea. Choose another angle as independent variable, and then use function sin and cos to transform it into horizontal and vertical coordinate.
x = R * cos(angle);
y = R * sin(angle);
angle += 0.05;
Some of you might wonder why it can display the path of circle movement. According to the definition of trigonometric function, we can easily reason out that function sin the ratio of the opposite side to the hypotenuse; function cos is the ratio of adjacent to hypotenuse. No matter where the circle point is, r (radius) will remain unchanged. Therefore we can conclude the expression of x coordinate and y coordinate.

Of course, it is all right if you can not fully understand it. You only have to know how to use it to draw a circle. This is a kind of “programming idea” too. Later on, we will often invoke some of the existed modules made by others to realize a certain kind of function. Just don’t push yourself to know it in detail.
However, function sin and cos is common. If you want to make higher level creation, you’d better try to know it thoroughly. If this question itself can drive ourselves to learn more mathematical knowledge, there are more interesting things waiting for you to dig out.


These are pictures closely relative to trigonometric function.
Movement Coordinate System
The previous effects are all about graphic coordinate changes. The coordinate system itself is static. Actually we can make the coordinate move to realize motional effect. This is just like the people on the beach watches the other people in the boat. For people on the boat, the boat is static. But what if the boat itself is moving, then people in the boat moves with it. The former cases are all about “people running on the boat”. Actually, the boat doesn’t move. The following is some common functions for changing coordinate system.

Function translate, we have talked about previously, is used to move coordinate system of the graphic horizontally.
Invoke format:
translate(a, b)
The first parameter stands for move to the positive direction of x axle for a pixels. The second parameter stands for move to the positive direction of y axle for b pixels.
Compare the two code and try to find any difference. (In order to simplify code,we can delete function size, the screen width and height are defaulted to be 100. )
Before we use:
ellipse(0, 0, 20, 20);

translate(50, 50);
ellipse(0, 0, 20, 20);

Function rotate
Invoke format:
rotate(a)
It is used to rotating coordinate system. When parameter is positive, it will choose the original point as center point and rotate in clockwise direction. The parameter input is same with trigonometric function to use radian.
Before use:
ellipse(50, 50, 20, 20);

rotate(radians(30));
ellipse(50, 50, 20, 20);


Function scale
Invoke format:
scale(a)
This function can zoom out coordinate system. The value is for scaling. When parameter is beyond 1, then zoom in; if it is lower than 1, then zoom out.
Before use:
ellipse(0, 0, 20, 20);

scale(4);
ellipse(0, 0, 20, 20);

scale(4,2);
ellipse(0, 0, 20, 20);

Superposition of Transformation Function
Here, superposition is all about changes relative to the present coordinate system. In other words, effects can be superimposed.
translate(40, 10);
translate(10, 40);
ellipse(0, 0, 20, 20);
Its final effect will equal to
translate(50, 50);
ellipse(0, 0, 20, 20);

rotate(radians(10));
rotate(radians(20));
ellipse(50, 50, 20, 20);
Equals to
rotate(radians(30));
ellipse(50, 50, 20, 20);

Before use:
ellipse(50, 50, 50, 20);
After use:
translate(50, 50);
rotate(radians(45));
ellipse(0, 0, 50, 20); //In order to see the rotate angle change, we have made an oval.

Horizontal Movement and Circular Movement
In the following cases, we are going to realize motional effect through changing coordinate system. At the same time, I would like to ask you refer to the former chapter example. Most of the time, you will find in order to realize a certain kind of effect, you can use a totally different method.
Horizontal Movement
[cceN_cpp theme="dawn"]
int x,y;
void setup(){
size(300, 300);
x = 0;
y = height/2;
}
void draw(){
background(234, 113, 107);
noStroke();
translate(x,y);
ellipse(0,0, 50, 50);
x++;
}
[/cceN_cpp]
int x,y;
void setup(){
size(300, 300);
x = 0;
y = height/2;
}
void draw(){
background(234, 113, 107);
noStroke();
translate(x,y);
ellipse(0,0, 50, 50);
x++;
}
[/cceN_cpp]

Rotate Movement
[cceN_cpp theme="dawn"]
float r, R, angle;
void setup(){
size(300, 300);
r = 20; //Circle dimension
R = 100; //Radius of movement track
}
void draw(){
background(234, 113, 107);
translate(width/2, height/2); //Move the original point to screen center.
rotate(angle);
noStroke();
ellipse(0 ,R ,r ,r);
angle += 0.05;
}
[/cceN_cpp]
float r, R, angle;
void setup(){
size(300, 300);
r = 20; //Circle dimension
R = 100; //Radius of movement track
}
void draw(){
background(234, 113, 107);
translate(width/2, height/2); //Move the original point to screen center.
rotate(angle);
noStroke();
ellipse(0 ,R ,r ,r);
angle += 0.05;
}
[/cceN_cpp]

You can understand in this way. Once the code in function draw has completed an operation from up to the bottom, the coordinate system will return to initial status at the second operation. The original point of coordinate system will be defaulted to return back to the left top corner. So if we want to make the coordinate system change continuously, the angle parameters within function rotate shall constantly increase its value.
Access Coordinate Status
Sometimes, we don’t want the change of coordinate system status is based on the former one. At this time, we have to use function pushMatrix and popMatrix . The two functions usually appears in couple. Function pushMatrix is before popMatrix . They can not be used solely, or it will go wrong.
Example:
[cceN_cpp theme="dawn"]
pushMatrix(); //Store coordinate system status
translate(50, 50);
ellipse(0, 0, 20, 20);
popMatrix(); //Read coordinate system status
rect(0, 0, 20, 20);
[/cceN_cpp]
pushMatrix(); //Store coordinate system status
translate(50, 50);
ellipse(0, 0, 20, 20);
popMatrix(); //Read coordinate system status
rect(0, 0, 20, 20);
[/cceN_cpp]

Besides, function pushMatrix and popMatrix allow nesting.
For example
pushMatrix();
…
pushMatrix();
…
popMatrix();
…
popMatrix();
…
In order to show its relationship intuitively, we choose condense format.
Combined Movement or Movement in Movement?
Now the second wave of important part starts. Just try to push forward. Previously, we have used a metaphor of boat and people. Have you ever think about what if we make both the people and the boat move, what kind of feeling the people on the beach will have?
Like combine horizontal movement with rotating movement of coordinate system. The point here is actually to move in a direction only.
[cceN_cpp theme="dawn"]
int x, y;
float angle;
void setup(){
size(300, 300);
background(234, 113, 107);
noStroke();
x = 0; //When the initial value of x is 0, we can neglect this sentence of code.When declaring variable, the default value is 0.
y = 0; //Same to the above.
angle = 0; //Same to the above.
}
void draw(){
angle += 0.25;
y--;
translate(width/2, height/2);
pushMatrix();
rotate(angle);
ellipse(x, y, 5, 5);
popMatrix();
}
[/cceN_cpp]
int x, y;
float angle;
void setup(){
size(300, 300);
background(234, 113, 107);
noStroke();
x = 0; //When the initial value of x is 0, we can neglect this sentence of code.When declaring variable, the default value is 0.
y = 0; //Same to the above.
angle = 0; //Same to the above.
}
void draw(){
angle += 0.25;
y--;
translate(width/2, height/2);
pushMatrix();
rotate(angle);
ellipse(x, y, 5, 5);
popMatrix();
}
[/cceN_cpp]

[cceN_cpp theme="dawn"]
float x, y, angle;
void setup(){
size(300, 300);
background(234, 113, 107);
noStroke();
}
void draw(){
angle += 0.01;
x = sin(angle) * 100;
y = cos(angle) * 100;
translate(width / 2, height / 2);
pushMatrix();
scale(1 + 0.1 * sin(angle*10));
ellipse(x, y, 5, 5);
popMatrix();
}
[/cceN_cpp]
float x, y, angle;
void setup(){
size(300, 300);
background(234, 113, 107);
noStroke();
}
void draw(){
angle += 0.01;
x = sin(angle) * 100;
y = cos(angle) * 100;
translate(width / 2, height / 2);
pushMatrix();
scale(1 + 0.1 * sin(angle*10));
ellipse(x, y, 5, 5);
popMatrix();
}
[/cceN_cpp]

Surprised? These are simple basic functions. But with different combination, we can create so many different effects. Till now, my exposure stops so as to spare some room for your exploration.
Comprehensive Usage
It is coming to an end soon for this chapter. The last two chapter, I have introduced the basic method of graphic movement. I believe you might have a deeper understanding for it, compared to your initial ideas. Last in the least, here’s some completed example for your reference.
[cceN_cpp theme="dawn"]
float x1, y1, x2, y2, r, R;
float angle1, angle2;
void setup(){
size(300, 300);
r = 12;
R = 120;
angle1 = 0;
angle2 = PI/4;
}
void draw(){
background(234, 113, 107);
noStroke();
translate(width / 2, height / 2);
angle1 += 0.02;
angle2 += 0.06;
x1 = R *sin(angle1);
y1 = R* cos(angle1);
x2 = R/2 *sin(angle2);
y2 = R/2 *cos(angle2);
ellipse(x1, y1, r/2, r/2);
ellipse(x2, y2, r, r);
ellipse(-x1, -y1, r/2, r/2);
ellipse(-x2, -y2, r, r);
ellipse(x1, -y1, r/2, r/2);
ellipse(x2, -y2, r, r);
ellipse(-x1, y1, r/2, r/2);
ellipse(-x2, y2, r, r);
stroke(255);
strokeWeight(3);
line(x1, y1, x2, y2);
line(-x1, -y1, -x2, -y2);
line(x1, -y1, x2, -y2);
line(-x1, y1, -x2, y2);
}
[/cceN_cpp]
float x1, y1, x2, y2, r, R;
float angle1, angle2;
void setup(){
size(300, 300);
r = 12;
R = 120;
angle1 = 0;
angle2 = PI/4;
}
void draw(){
background(234, 113, 107);
noStroke();
translate(width / 2, height / 2);
angle1 += 0.02;
angle2 += 0.06;
x1 = R *sin(angle1);
y1 = R* cos(angle1);
x2 = R/2 *sin(angle2);
y2 = R/2 *cos(angle2);
ellipse(x1, y1, r/2, r/2);
ellipse(x2, y2, r, r);
ellipse(-x1, -y1, r/2, r/2);
ellipse(-x2, -y2, r, r);
ellipse(x1, -y1, r/2, r/2);
ellipse(x2, -y2, r, r);
ellipse(-x1, y1, r/2, r/2);
ellipse(-x2, y2, r, r);
stroke(255);
strokeWeight(3);
line(x1, y1, x2, y2);
line(-x1, -y1, -x2, -y2);
line(x1, -y1, x2, -y2);
line(-x1, y1, -x2, y2);
}
[/cceN_cpp]

For which points matches ? Which lines matches? I can not figure out it too. But I remember it derives from a small section of code.


END
Last in our chapter, let’s turn back to a question we preserved for a long time since the beginning. What is the usage of spending so much effort to make a picture with program? After you learned this chapter, you will find there are so much playing methods waiting for your to explore.
[cceN_cpp theme="dawn"]
float browX, earD, eyeD, faceD;
void setup(){
size(500, 500);
}
void draw(){
background(200, 0, 0);
browX = 150 + sin(frameCount / 30.0) *20;
earD = 180 + sin(frameCount / 10.0) *20;
eyeD = 60 + sin(frameCount/30.0) *50;
faceD = 300;
strokeWeight(8);
ellipse(175, 220, earD, earD);
ellipse(width - 175, 220, earD, earD);
rect(100, 100, faceD, faceD);
line(browX, 160, 220, 240);
line(width-browX, 160, width-220, 240);
fill(random(255),random(255),random(255));
ellipse(175, 220, eyeD, eyeD);
ellipse(width-175, 220, eyeD, eyeD);
fill(255);
point(width/2, height/2);
triangle(170 - cos(frameCount / 10.0)* 20, 300 - sin(frameCount / 10.0) *20, width - (170 + cos(frameCount / 10.0) *20), 300 + sin(frameCount / 10.0) * 20, 250, 350);
}
[/cceN_cpp]
float browX, earD, eyeD, faceD;
void setup(){
size(500, 500);
}
void draw(){
background(200, 0, 0);
browX = 150 + sin(frameCount / 30.0) *20;
earD = 180 + sin(frameCount / 10.0) *20;
eyeD = 60 + sin(frameCount/30.0) *50;
faceD = 300;
strokeWeight(8);
ellipse(175, 220, earD, earD);
ellipse(width - 175, 220, earD, earD);
rect(100, 100, faceD, faceD);
line(browX, 160, 220, 240);
line(width-browX, 160, width-220, 240);
fill(random(255),random(255),random(255));
ellipse(175, 220, eyeD, eyeD);
ellipse(width-175, 220, eyeD, eyeD);
fill(255);
point(width/2, height/2);
triangle(170 - cos(frameCount / 10.0)* 20, 300 - sin(frameCount / 10.0) *20, width - (170 + cos(frameCount / 10.0) *20), 300 + sin(frameCount / 10.0) * 20, 250, 350);
}
[/cceN_cpp]

If you have a heart that want to break everything and combine it again, study program will greatly help you to fulfil this idea.

This article comes from designer Wenzy.
Relative Readings:
This article is from: https://www.elecfreaks.com/10920.html
If you have any questions, you can contact: louise@elecfreaks.com
评论
发表评论