Skip to content

Commit 2a083cc

Browse files
committed
Merge branch 'master' of github.com:processing/p5.js-website into showcase
2 parents ffdd6d0 + a5f5db9 commit 2a083cc

File tree

7 files changed

+442
-1
lines changed

7 files changed

+442
-1
lines changed

contributor_docs/Adding_examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Examples help demonstrate different programming concepts and functionality of th
5454
4. [Grunt](https://gruntjs.com/) should now be installed, and you can use it to build the website by navigating to the top level directory and typing:
5555
5656
```bash
57-
grunt server
57+
npm run watch
5858
```
5959
6060
5. A local build of the p5js.org site should open in your web browser and you can navigate to the examples page to see your changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @name Stepping Feet Illusion
3+
* @description Stepping feet illusion is a very famous psychological experiment
4+
* Both the bricks will appear to move at different speed
5+
* even though they are moving at the same speed.
6+
* Click the mouse inside Canvas to confirm that
7+
* they are moving at the same speed.
8+
* Contributed by Sagar Arora.
9+
*/
10+
11+
// this class describes the structure
12+
// and movents of the brick
13+
class Brick{
14+
constructor(bc, y){
15+
this.brickColor = bc;
16+
this.yPos = y;
17+
this.xPos = 0;
18+
}
19+
20+
// this function creates the brick
21+
createBrick(){
22+
fill(this.brickColor);
23+
rect(this.xPos, this.yPos, 100, 50);
24+
}
25+
26+
// this function sets the speed
27+
// of movement of the brick to 1
28+
setSpeed(){
29+
this.xSpeed = 1;
30+
}
31+
32+
// this function set the bricks in motion
33+
moveBrick(){
34+
this.xPos+=this.xSpeed;
35+
if(this.xPos+100 >= width || this.xPos <= 0){
36+
this.xSpeed*=-1;
37+
}
38+
}
39+
}
40+
41+
function setup() {
42+
createCanvas(720, 400);
43+
createP("Keep the mouse clicked").style('color','#ffffff');
44+
createP("to check whether the bricks").style('color','#ffffff');
45+
createP("are moving at same speed or not").style('color','#ffffff');
46+
}
47+
48+
// creating two bricks of
49+
// colors white and black
50+
let brick1 = new Brick("white",100);
51+
let brick2 = new Brick("black",250);
52+
53+
//
54+
brick1.setSpeed();
55+
brick2.setSpeed();
56+
57+
function draw () {
58+
background(0);
59+
if(mouseIsPressed){
60+
background(50);
61+
}
62+
brick1.createBrick();
63+
brick1.moveBrick();
64+
if(!mouseIsPressed){
65+
createBars();
66+
}
67+
brick2.createBrick();
68+
brick2.moveBrick();
69+
}
70+
71+
// this function creates the black and
72+
// white bars across the screen
73+
function createBars() {
74+
let len = 12;
75+
for(let i = 0;i<width/len;i++){
76+
fill("white");
77+
if(i%2 == 0)
78+
rect(i*len,height,len,-height);
79+
}
80+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @name Stepping Feet Illusion
3+
* @description Stepping feet illusion is a very famous psychological experiment
4+
* Both the bricks will appear to move at different speed
5+
* even though they are moving at the same speed.
6+
* Click the mouse inside Canvas to confirm that
7+
* they are moving at the same speed.
8+
* Contributed by Sagar Arora.
9+
*/
10+
11+
// this class describes the structure
12+
// and movents of the brick
13+
class Brick{
14+
constructor(bc, y){
15+
this.brickColor = bc;
16+
this.yPos = y;
17+
this.xPos = 0;
18+
}
19+
20+
// this function creates the brick
21+
createBrick(){
22+
fill(this.brickColor);
23+
rect(this.xPos, this.yPos, 100, 50);
24+
}
25+
26+
// this function sets the speed
27+
// of movement of the brick to 1
28+
setSpeed(){
29+
this.xSpeed = 1;
30+
}
31+
32+
// this function set the bricks in motion
33+
moveBrick(){
34+
this.xPos+=this.xSpeed;
35+
if(this.xPos+100 >= width || this.xPos <= 0){
36+
this.xSpeed*=-1;
37+
}
38+
}
39+
}
40+
41+
function setup() {
42+
createCanvas(720, 400);
43+
createP("Keep the mouse clicked").style('color','#ffffff');
44+
createP("to check whether the bricks").style('color','#ffffff');
45+
createP("are moving at same speed or not").style('color','#ffffff');
46+
}
47+
48+
// creating two bricks of
49+
// colors white and black
50+
let brick1 = new Brick("white",100);
51+
let brick2 = new Brick("black",250);
52+
53+
//
54+
brick1.setSpeed();
55+
brick2.setSpeed();
56+
57+
function draw () {
58+
background(0);
59+
if(mouseIsPressed){
60+
background(50);
61+
}
62+
brick1.createBrick();
63+
brick1.moveBrick();
64+
if(!mouseIsPressed){
65+
createBars();
66+
}
67+
brick2.createBrick();
68+
brick2.moveBrick();
69+
}
70+
71+
// this function creates the black and
72+
// white bars across the screen
73+
function createBars() {
74+
let len = 12;
75+
for(let i = 0;i<width/len;i++){
76+
fill("white");
77+
if(i%2 == 0)
78+
rect(i*len,height,len,-height);
79+
}
80+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @name Bubble Sort
3+
* @description Sorts the randomly distributed bars
4+
* according to their height in ascending order
5+
* while simulating the whole sorting process.
6+
* Took references from Coding Challenge by The Coding Train.
7+
*/
8+
9+
let values = [];
10+
let i = 0;
11+
let j = 0;
12+
13+
// The statements in the setup() function
14+
// execute once when the program begins
15+
// The array is filled with random values in setup() function.
16+
function setup() {
17+
createCanvas(720, 400);
18+
for(let i = 0;i<width/8;i++){
19+
values.push(random(height));
20+
}
21+
}
22+
23+
// The statements in draw() function are executed until the
24+
// program is stopped. Each statement is executed in
25+
// sequence and after the last line is read, the first
26+
// line is executed again.
27+
function draw() {
28+
background(220);
29+
bubbleSort();
30+
simulateSorting();
31+
}
32+
33+
// The bubbleSort() function sorts taking 8 elements of the array
34+
// per frame. The algorithm behind this function is
35+
// bubble sort.
36+
function bubbleSort() {
37+
for(let k = 0;k<8;k++){
38+
if(i<values.length){
39+
let temp = values[j];
40+
if(values[j] > values[j+1]){
41+
values[j] = values[j+1];
42+
values[j+1] = temp;
43+
}
44+
j++;
45+
46+
if(j>=values.length-i-1){
47+
j = 0;
48+
i++;
49+
}
50+
}
51+
else{
52+
noLoop();
53+
}
54+
}
55+
}
56+
57+
// The simulateSorting() function helps in animating
58+
// the whole bubble sort algorithm
59+
// by drawing the rectangles using values
60+
// in the array as the length of the rectangle.
61+
function simulateSorting(){
62+
for(let i = 0;i<values.length;i++){
63+
stroke(100, 143, 143);
64+
fill(50);
65+
rect(i*8 , height, 8, -values[i],20);
66+
}
67+
}

0 commit comments

Comments
 (0)