Year1Class5

From Graphic Design Arnhem Digital Media
Jump to: navigation, search

Additional Example Code

using random for positioning

  • This example uses the point function to put a dot on the screen
  • The for loops simple state how many times we want a random point
  • By setting the upper and lower limit of the random function we can position a field of points
 1 size(800,600);
 2 background(255);
 3 strokeWeight(4);
 4 // top left side. RED
 5 for(int i = 0;i < 200;i = i + 1)
 6 {
 7   stroke(255, 0, 0);
 8   float ranX = random(0, 400);
 9   float ranY = random(0, 300);
10   point(ranX, ranY);
11 }
12 // top right side. GREEN
13 for(int i = 0;i < 200;i = i + 1)
14 {
15   stroke(0, 255, 0);
16   float ranX = random(400, 800);
17   float ranY = random(0, 300);
18   point(ranX, ranY);
19 }
20 // bottom left side. BLUE
21 for(int i = 0;i < 200;i = i + 1)
22 {
23   stroke(0, 0, 255);
24   float ranX = random(0, 400);
25   float ranY = random(300, 600);
26   point(ranX, ranY);
27 }
28 // bottom right side. BLACK
29 for(int i = 0;i < 200;i = i + 1)
30 {
31   stroke(0);
32   float ranX = random(400, 800);
33   float ranY = random(300, 600);
34   point(ranX, ranY);
35 }

using a more complex function (Bezier) 1

  • The bezier function uses a lot of numbers(8)
  • There is a commented out line function to help you
  • There is two point functions that are the same positions as the bezier anchor points
  • The behavior of the bezier function is not unlike curves in illustrator or inkscape
  • The outer numbers of the bezier function are its begin and end coordinates(same as line)
  • Try editing the inner 4 numbers(100, 400, 700, 200) and see the result of the bending of the bezier curve
 1 size(800, 600); 
 2 noFill();
 3 background(255);
 4 
 5 stroke(255, 0, 0);
 6 strokeWeight(5);
 7 point(100, 400);
 8 point(700, 200);
 9 
10 stroke(0);
11 strokeWeight(1);
12 //line(100, 300, 700, 300);
13 bezier(100, 300, 100, 400, 700, 200, 700, 300);

using a more complex function (Bezier) 2

  • To assist you further in understanding the bezier function here is an example where one of the anchor points is controlled with the mouse
  • Try switching anchor points in the point functions and the middle of the bezier function
  • Try writing a println() statement that helps you understand the position of an anchor point
 1 void setup()
 2 {
 3   size(800, 600); 
 4   noFill();
 5 }
 6 void draw()
 7 {
 8   background(255);
 9   stroke(255, 0, 0);
10   strokeWeight(5);
11   point(mouseX, mouseY);
12   point(700, 200);
13 
14   stroke(0);
15   strokeWeight(1);
16   bezier(100, 300, mouseX, mouseY, 700, 200, 700, 300);
17 }

using a more complex function (Bezier) 3

  • Using the bezier function in a for loop we can produce interesting shapes.
  • Here the bezier begin point and one anchor point are moved 20 pixels each time the loop runs
  • We can use more than one bezier function in one loop
  • Try changing the loop properties, adding and substracting numbers, adding more bezier functions
1 size(800, 600);
2 background(255);
3 stroke(0);
4 noFill();
5 for(int i = 0;i< 500; i += 20)
6 {
7   bezier(100 + i, 300, 100, 300 + i, 700, 300, 700, 300);
8   bezier(100 + i, 300, 100, 300 - i, 700, 300, 700, 300);
9 }