วันพฤหัสบดีที่ 25 กรกฎาคม พ.ศ. 2556

Calculate Max/Min/Avg

int[] n={59.9,45.9,21,18.4,16.9,4.8,3.5,0.6}; //เป็นการประกาศตัวแปรเป็น Array จำนวนเต็ม  ชื่อ n ซึ่งตัวแปรชนิดนี้จำเก็บค่าได้หลายตัว โดยที่มีตำแหน่ง index เป็นตัวกำกับ
int max=n[0]; //เป็นการประกาศให้ค่า max =ค่าn[0] ในที่นี้คือ 59.9 
int min=n[0]; //เป็นการประกาศให้ค่าmin =ค่าn[0] ในที่นี้คือ 0.6 
int count=0;
float sum=0;
float avg;
while(count<n.length){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
จากคำสั่งเงื่อนไข คือ count<n.length ซึ่ง(n.length)คือ ความกว้างของตัวแปรarray n
  if(max<n[count]){ //กำหนดเงื่อนไขในการคำนวน ถ้าค่า max<n[count]
    max=n[count]; //ถ้าจริงให้ max เก็บค่า n[count] ซึ่งเมื่อวนลูปเสร็จจะได้ค่า max
  }
  if(min>n[count]){ //กำหนดเงื่อนไขในการคำนวน ถ้าค่า min>n[count]
    min=n[count]; //ถ้าจริงให้ min เก็บค่า n[count] ซึ่งเมื่อวนลูปเสร็จจะได้ค่า min
  }
  sum=sum+n[count]; //เป็นการหาผลรวมของจำนวนทั้งหมดเพื่อนำไปคิดค่าเฉลี่ย
  count=count+1; //count=count+1 คือ count จะมีค่าเพิ่มขึ้น 1 ทุกๆครั้งที่มีการวนลูป
}
avg=sum/n.length; //คิดหาค่าเฉลี่ย
println("max="+max+" min="+min+" average="+avg);//เป็นการแสดงค่า max , min , average


Bar Chart.


Percentage of population aged 6 years and over used mobile phone by activity.






float[]p = {59.9,45.9,21.0,18.4,16.9,4.8,3.5,0.6}; //เป็นประกาศตัวแปรเป็น Array จำนวนทศนิยม ชื่อ p ซึ่งตัวแปรชนิดนี้จะเก็บค่าได้หลายตัวโดยมีตำแหน่ง index เป็นตัวกำกับ
String[]a = {"Received news or information","SMS, MMS","Wacthing TV, listening music or radio","Games","Loading image or music","E-mail","Surfing Internet","Mobile Banking"};
//เป็นการประกาศตัวแปรเป็น Array ชนิดข้อความ ชื่อ a ซึ่งตัวแปรชนิดนี้จะเก็บค่าได้หลายตัวโดยมีตำแหน่ง index เป็นตัวกำกับ

int n = 0;
void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(500,400); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas
  background(0); //ใส่สีดำให้กับพื้นหลัง
  drawStatics(); //เรียกใช้ ฟังก์ชัน drawStatics
}
void drawStatics(){ //เป็นฟังก์ชันที่เราสร้างขึ้นมาเอง โดยใช้ชื่อ drawStatics
  int x = 20;
  int y = 130;
  int w = 40;
  int v = 0;
  
  stroke(255); //stroke คือ การใส่สีให้กับเส้น กำหนดค่าได้ดังนี้ (แดง,เขียว,น้ำเงิน) จากตัวอย่าง stroke(255); จะได้สีขาว
  line(0,130,500,130);
  while(n<p.length){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
จากคำสั่งเงื่อนไข คือ while(n<p.length) ซึ่ง(p.length)คือ ความกว้างของตัวแปรarray p
  if(n==0){fill(0,0,255);} //เป็นการสร้างเงื่อนไขในการใส่สี จากคำสั่งนี้จำได้สีน้ำเงิน
  if(n==1){fill(255,0,0);} //red color
  if(n==2){fill(0,255,127);} //green color
  if(n==3){fill(255,255,0);} //yellow color
  if(n==4){fill(255,20,147);} //pink color
  if(n==5){fill(148,0,211);} //violet color
  if(n==6){fill(0,191,255);} //blue sky color
  if(n==7){fill(255,165,0);} //orange color
  
  rect(x,y,w,-p[n]);
  text(p[n]+"%",x,y-p[n]-10); //เป็นคำสั่งที่แสดงข้อความโดยกำหนดดังนี้ (ค่าที่ต้องการแสดง+"ข้อความ",ตำแน่งแกนx,ตำแหน่งแกนy)
  rect(16,162+v,5,5);
  text(a[n],25,170+v);
  x=x+60; //x=x+60 คือ x จะมีค่าเพิ่มขึ้น 60  ทุกๆครั้งที่มีการวนลูป
  v=v+30; //v=v+30 คือ v จะมีค่าเพิ่มขึ้น 30  ทุกๆครั้งที่มีการวนลูป
  n=n+1;  //n=n+1 คือ n จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
  }
}

Source : http://web.nso.go.th/en/stat_theme_ict.htm

Some UFO.




int[] n = {4,3,2,1}; //ประกาศตัวแปรเป็น Array จำนวนเต็มชื่อ n ซึ่งตัวแปรชนิดนี้จะเก็บค่าได้หลายตัว โดยมีตำแหน่ง index เป็นตัวกำกับ
//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย 
int x=0;
int i=0;  
void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(400,300); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas
}
void draw(){ //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }
ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ
  background(119,136,153); //Light Slate Gray Color
  drawUFO(x); //เรียกใช้ฟังก์ชัน  drawUFO(x) โดยส่งค่า x ไปที่ฟังก์ชัน
  if(i<=500){ //if( ){ }เป็นการกำหนดเงื่อนไข โดยกำหนดเงื่อนไขไว้ใน ( ) ถ้าเงื่อนไขเป็นจริง ให้ทำตามคำสั่งใน { }
    x=x+1;
}
  else{ //else คือฟังก์ชันที่ขยายโครงสร้างของฟังก์ชัน if() ตามด้วยโปรแกรมที่จะให้เลือกระหว่างคำสั่ง 2 ชุดหรือมากกว่านั้น และจะระบุคำสั่งที่ปฏิบัติ เมื่อประโยคในฟังก์ชั่น if() ผิดพลาด
  x=x-1; 
}

  i=i+1;                          
  if(i==1400){ 
  x=0; i=0;
  }
}
void drawUFO(int x){ //เป็นฟังก์ชันที่เราสร้างขึ้นมาเอง โดยใช้ชื่อ drawUFO(int x) โดย (int x) คือ การกำหนดตัวแปรเพื่อให้สามารถรับค่าที่ส่งมาเมื่อมีการเรียกใช้ฟังก์ชัน และส่งต่อไปยังตัวแปร x ในคำสั่งต่าง ๆ
  int count = 0;
  int y=60;
  while(count<n.length){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
จากคำสั่งเงื่อนไข คือ count<n.length ซึ่ง(n.length)คือ ความกว้างของตัวแปรarray n
    int c=0;
    int t=30;
  while(c<n[count]){
    ellipse(t+x,y,40,40);
    ellipse(t+x,y+5,80,25);  
    t=t+80; //t=t+80 คือ x จะมีค่าเพิ่มขึ้น 80 ทุกๆครั้งที่มีการวนลูป
    c=c+1; //c=c+1 คือ x จะมีค่าเพิ่มขึ้น 1 ทุกๆครั้งที่มีการวนลูป
  }
  y=y+60 ; //y=y+60 คือ y จะมีค่าเพิ่มขึ้น 60 ทุกๆครั้งที่มีการวนลูป
  count=count+1; //count=count+1 คือ count จะมีค่าเพิ่มขึ้น 1 ทุกๆครั้งที่มีการวนลูป
  }
}

อ้างอิงคำสั่งพื้นฐานจาก http://com5630264.blogspot.com/search/label/Lab%201

Binary Display.

Binary Calculation.

int[]bi = new int[1000];
  int n = 20;
  int i = 0;
  while (n!=0) {
    bi[i]=n%2;
    n=n/2;
    if (n!=0) {
      i++;
    }
  }
  while (i>=0) {
    print(bi[i]);
    i--;
  }

Output
10100

วันอาทิตย์ที่ 21 กรกฎาคม พ.ศ. 2556

Array (Hard)

Code
int[]bi = new int[1000];
  int n = 20;
  int i = 0;
  while (n!=0) {
    bi[i]=n%2;
    n=n/2;
    if (n!=0) {
      i++;
    }
  }
  while (i>=0) {
    print(bi[i]);
    i--;
  }


Array (Medium)

Array (Easy)

Preposition
Write the code to calculate the average of the element included in a one-dimensional double array named rates. The array has five element.


Code
int[] avg= {
  20, 13, 15, 17, 18
};
int i;
float sum=0;
for (i=0;i<avg.length;i++) { 
  sum=sum+avg[i];
}
println(sum/avg.length);


Output
16.6


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2

วันเสาร์ที่ 20 กรกฎาคม พ.ศ. 2556

Loop (Hard)

Proposition
Design and write a Java application that takes as input an integer and a character from the screen. The output should be a diamond on the screen composed of the character and extending for the width specified by the integer. For example, if the integer is 11 and the character is an asterisk (*), the diamond would look like this:
                       *
                      ***
                     *****
                    *******
                   *********
                  ***********
                   *********
                    *******
                     *****
                      ***
                       *


Code
int j = 0; int m; int n = 10; if (n/2==0) { m = n/2; } else { m = (n/2)+1; } int[] a = new int [m]; for (int i=1;i<=n;i++) { if (i%2!=0) { a[j] = i; j++ ; } } for (int k=0; k<m; k++) { for (int i=0; i<a[k]; i++) { print ("*") ; } println () ; } if (n%2!=0) { for (int c=m-2; c>=0; c--) { for (int i=0; i<a[c]; i++) { print ("*") ; } println () ; } } else { for (int d=m-1; d>=0; d--) { for (int i=0; i<a[d]; i++) { print ("*") ; } println () ; } }


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2

Loop (Medium)

Proposition
Write a nested loop code segment that produces this output:
1
12
123
1234


Code
void setup() {
  int c = 1;
  int n = 5;
  while (c <= n) {

    Row(c);
    println();
    c = c + 1;
  }


void Row(int c) {
  int a = 1;
  while (a < c) {
    String sa = str(a);
    print(sa);
    a = a + 1;
  }

}


Output
1
12
123
1234

Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2


Loop (Easy)

Proposition
Write a code segment that sums the even integers from 16 through 26, inclusive.

Code

int sum=0;
int start=16;
int finish=26;
int i;
for (i=start;i<finish;i++) {
  if (i%2==0) sum+=i;
}
println(sum);


Output
126

Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2


วันศุกร์ที่ 19 กรกฎาคม พ.ศ. 2556

Condition (Hard)

Condition (Medium)

Proposition
(Checking Whether a number is even) Write a program that reads an integer and checks whether it is even. For example ,if your input is 25,the out put should be
'IS 25 an even number? False'


Code
int x=26;

if (x%2==0) {
  println("Is"+" "+x+" an even number? True");
}
else {
  println("Is"+" "+x+"an even number? False");

}


Output
Is 26 an even number? True


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2


Condition (Easy)

Proposition
Given float variables x1,x2,y1,y2, and m, write a code segment to find the slope of a line through the two points (xv,y1) and (x2,y2). Use the formula

m = y2-y1/x2-x1
to determine the slope of the line. If x1 equals x2, the line is vertical and the slope is undefined. The segment should display the slope with an appropiate label. If the slope is undefined, it should display the message "Slope undefined."



Code
int y2 = 5;
int y1 = 3;
int x2 = 4;
int x1 = 4;
if(x2-x1!=0){
  int m = (y2-y1)/(x2-x1);
}
else{
  print ("Slope undefined");
}


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2

Function (Hard)

Function (Medium)

Preposition
Write a function that take an integer n and prints a list of squares and cubes of the number 1-n. From main(), read the number n  from the user.

Code
int n=9;
int i=1;
void draw() {
  Print();
}
void Print() {
  if (i<=n) {
    print(i);
    i++;
  }
}


Output
123456789


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2

Function (Easy)

Variable (Hard)

Proposition
Write expressions to compute both solutions for the quadratic formula. The formula is
-b+-square root b^2-4ac/2a
The +- symbol means "plus or minus" and indicates that the equation has two solutions:one in which the result of the square root is added to 2a and one in which the result is subtracted from 2a. Assume all variables are float variables.


Code
int a = 1;
int b = 3;
int c = 2;
float x1 = (((-1)*b)-(sqrt(sq(b)-(4*a*c))))/(2*a);
float x2 = (((-1)*b)+(sqrt(sq(b)-(4*a*c))))/(2*a);
println (x1);
println (x2);


Output
-2.0
-1.0


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2


Variable (Medium)

Preposition
Write an assignment statement to calculate the sum of the number from 1 through n
using Gauss'formula 

sum=n(n+1)/2 

Code
int n=20;
int sum=0;
sum=n*(n+1)/2;

print(sum);


Output
210


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2

Variable (Easy)

Proposition
Write a Java application that takes an integral Celsius temperature as input and converts it to its Kelvin equivalent. The formula is

Kelvin = Celsius+273


After the Celsius temperature is input,it should be displayed along with the corresponding Kelvin equivalent. The application should include appropriate messages identifying each value. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do in the code in this chapter.


Code
int c = 40;
int k = c+ 273;

print (k);


Output
313


Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2


วันพฤหัสบดีที่ 18 กรกฎาคม พ.ศ. 2556

Express Way.





//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย 
int count = 0;
int n = 3;
int x = 4;
int y = 50;

size(400,400); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas 
background(155,96,59); //ใส่สีน้ำตาลให้กับพื้นหลัง

strokeWeight (3);
rect (100,-3,200,403);
line (200,0,200,400);

while(count<x){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
  strokeWeight (8) ;
  line (150,y-30,150,y);
  line (250,y-30,250,y);
  fill(192,192,192);
  strokeWeight (3); //strokeWeight คือ การกำหนดความกว้างของเส้น
  rect(-2,y,402,60);
  y = y+120; //y = y+120 คือ y จะมีค่าเพิ่มขึ้น 120  ทุกๆครั้งที่มีการวนลูป
  count = count+1; //count = count+1 คือ count จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
}

อ้างอิงคำสั่งพื้นฐานจาก http://com5630264.blogspot.com/search/label/Lab%201

Mobile.




void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
   size(200,200); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas

  background(173,216,230); //ใส่สีฟ้าอ่อนให้กับพื้นหลัง
}
void draw(){ //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }
ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ
  fill(255,255,0); //yellow color
  rect(20,30,160,10);
  strokeWeight(5); //strokeWeight คือ การกำหนดความกว้างของเส้น
  line(100,0,100,30);
  fill(255,0,0);
  ellipse(100,15,10,10);
  
//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย
  int x=25;
  int y=42;
  int l=20;
  int n=8;
  int c=0;
  while(c<n){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
    line(x,y,x,y+l);
    ellipse(x,y+l,10,10);
    line(200-x,y,200-x,y+l);
    ellipse(200-x,y+l,10,10);
    x=x+10; //x=x+10 คือ x จะมีค่าเพิ่มขึ้น 10  ทุกๆครั้งที่มีการวนลูป
    l=l+15; //l=l+15 คือ l จะมีค่าเพิ่มขึ้น 15  ทุกๆครั้งที่มีการวนลูป
    c=c+1; //c=c+1 คือ c จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
  
  }
}

อ้างอิงคำสั่งพื้นฐ่านต่างๆจาก http://com5630264.blogspot.com/search/label/Lab%201

Railway.




void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(500,300); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas
  background(0); //ใส่สีดำให้กับพื้นหลัง
}
void draw(){  //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }
ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ
  
//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย   
  int x = 0;
  int y = 120 ;
  int z = 0;
  int n =20;
  int m = 2;
  int count = 0;

while(count<n){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
  stroke(0,250,154); //stroke คือ การใส่สีให้กับเส้น กำหนดค่าได้ดังนี้ (แดง,เขียว,น้ำเงิน) จากตัวอย่าง stroke(0,250,154) จะได้สีเขียวอ่อน
  strokeWeight(10); //strokeWeight คือ การกำหนดความกว้างของเส้น
  line(x,100,x,200);
  x = x +50; // x = x + 50 คือ x จะมีค่าเพิ่มขึ้น 50  ทุกๆครั้งที่มีการวนลูป
  count = count +1; //count = count +1 คือ count จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
}
while(z<m){
  stroke(255,255,224); 
  strokeWeight(10);
  line(0,y,500,y);
  y = y + 60; // y = y + 60 คือ y จะมีค่าเพิ่มขึ้น 60  ทุกๆครั้งที่มีการวนลูป
  z = z +1; //z = z +1 คือ z จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
 }
}

อ้างอิงคำสั่งพื้นฐานจาก http://com5630264.blogspot.com/search/label/Lab%201

Bridge.




void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(300,200); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas
  background(0); //ใส่สีดำให้กับพื้นหลัง
}
void draw(){ //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }
ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ
  strokeWeight(3); //strokeWeight คือ กำหนดความกว้างของเส้น
  fill(205,92,92);
  rect(0,150,20,50);
  rect(280,150,20,50);
  noFill();
  stroke(0,255,255); //stroke คือ การใส่สีให้กับเส้น กำหนดค่าได้ดังนี้ (แดง,เขียว,น้ำเงิน) จากตัวอย่าง stroke(0,255,255); จะได้สีฟ้า
  quad(50,100,250, 100,290,150,10,150);
  rect(50,100,200,50);

  
//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย  
  int xPos = 50;
  int yPos = 100;
  int pollH = 48;
  int space = 50;
  int n = 4;
  int count = 0;
  while (count<n){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
    stroke(0,255,255);
    line(xPos, yPos, xPos+48, yPos+pollH);
    line(xPos, yPos+pollH, xPos+48, yPos);
    xPos = xPos+space; //xPos = xPos+space คือ xPos จะมีค่าเพิ่มขึ้น space  ทุกๆครั้งที่มีการวนลูป
    count = count+1; //count = count+1 คือ count จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
  }
}

อ้างอิงคำสั่งพื้นฐานจาก http://com5630264.blogspot.com/search/label/Lab%201

วันพุธที่ 17 กรกฎาคม พ.ศ. 2556

Curve Stitching.




//int คือ การประกาศตัวแปรเป็นค่าจำนวนเต็ม โดยค่าข้างขวาจะกำหนดตัวแปรข้างซ้าย 
int x=0;
int y=0;
int r=0;
int n=50;
int l=200;
int count=0;
void setup(){ //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(200,200); //size(กำหนดค่าตามแกนx,กำหนดค่าตามแกนy) คือ คำสั่งเพื่อใช้กำหนดขนาด Canvas
  background(0); //ใส่สีดำให้กับพื้นหลัง
  
  while(count<n){ //while คือ การทำงานในลักษณะวนซ้ำหรือที่เรามักจะได้ยินกันบ่อยๆว่าการ วนลูป ซึ่งการทำงานซ้ำในแต่ละรอบนั้นจะประมวลผลกลุ่มคำสั่งเดิมที่อยู่ภายใต้ ประโยคwhile ซึ่งรอบของการวนซ้ำจะนานเท่าไรนั้นก็ขึ้นอยู่กับว่าผ่านเงื่อนไขการทดสอบว่า เป็นเท็จหรือไม่ ถ้าเป็นเท็จจริงจึงจะหลุดจากการทำงาน
    stroke(255,20,147); //stroke คือ การใส่สีให้กับเส้น กำหนดค่าได้ดังนี้ (แดง,เขียว,น้ำเงิน) จากตัวอย่าง stroke(255,20,147) จะได้สีชมพู
    line(x,y+r,x+r,l);
    line(l,y+r,x+r,x);
    r=r+5; //r=r+5 คือ r จะมีค่าเพิ่มขึ้น 9  ทุกๆครั้งที่มีการวนลูป
    count=count+1; //count=count+1 คือ count จะมีค่าเพิ่มขึ้น 1  ทุกๆครั้งที่มีการวนลูป
  }
}

อ้างอิงคำสั่งพื้นฐานจาก http://com5630264.blogspot.com/search/label/Lab%201


วันอังคารที่ 16 กรกฎาคม พ.ศ. 2556




int startx = 0;
int starty = 200;

void setup() {
  size(200, 200);
  background(255);
}
void draw() {
  if (keyPressed==true) {
    startx=0;
    starty=200;
  }
}
void mousePressed() {  
  if (mouseY<starty) {  
    if (mouseX>startx) {
      strokeWeight(3);
      stroke(255, 0, 0);
      line(startx, starty, mouseX, mouseY);
      startx=mouseX;
      starty=mouseY;
    }
  }
}



วันพฤหัสบดีที่ 11 กรกฎาคม พ.ศ. 2556

ART




void setup() { //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
size(400, 400); // กำหนดขนาดส่วนแสดงผลเป็น 400*400
background(217); // ใส่สี พื้นหลัง
}

void draw() { //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }

ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ ellipse(mouseX, mouseY, 80, 80); /*วาดวงกลม โดยใส่จุดศูนย์กลางเป็น ตำแหน่งของเมาส์ ขณะนั้น
if (mousePressed == true) { ผลทีได้คือ วงกลมจะเคลื่อนที่ตามเมาส์*/
fill(129, 230, 290); // เงื่อนไข ถ้ามีการกดเมาส์เกิดขึ้น จะทำการใส่สีหนึ่ง
}
else { //แต่ถ้า เงื่อนไขไม่เป็นจริง ก็จะมีการใส่อีกสีนึงแทน
fill (255,234,15);
}
}


MUSIC


Compact Cassette.



int note1move = 10 ; // กำหนดตัวแปร note1move ให้ใช้ในการเพิ่มค่าตัวแปร (ทำให้เคลื่อนไหว)
int note2move = 100 ; // กำหนดตัวแปร note2move ให้ใช้ในการเพิ่มค่าตัวแปร (ทำให้เคลื่อนไหว)
void setup() { // ตั้งค่า
size(300, 200); //กำหนดขนาดของส่วนแสดงผล
}
void drawCassette() { //สร้างฟังก์ชัน drawCassette
int x=200; // กำหนดตัวแปร x เอาไว้ใช้ใน ฟังก์ชัน
int r=20; // กำหนดตัวแปร y เอาไว้ใช้ใน ฟังก์ชัน

fill(255, 0, 0); //red color // ใส่สีให้ object ด้านล่าง
rect(0, 0, 300, 200); //สร้างสี่เหลี่ยม

fill(0, 0, 255); //blue color
rect(15, 15, 270, 130);

fill(255, 165, 0); //orange color
rect(r, r, 260, r+10);

fill(0); //black color
triangle(50, 200, 70, 160, 100, 200); // สร้างสามเหลี่ยม
triangle(x+30, x, x+40, 160, 260, x);
rect(70, 160, 170, 50);

fill(250, 255, 0); //yellow color
ellipse(80, 190, 13, 13); //สร้างวงกลม
ellipse(110, 183, r-7, r-7);

ellipse(x+30, 190, r-7, r-7);
ellipse(x, 183, r-7, r-7);

fill(0); //black color
rect(65, 60, 170, 50);

fill(250, 255, 0); //yellow color
ellipse(90, 85, r*2, r*2);
ellipse(x+8, 85, r*2, r*2);

fill(0); //black color
ellipse(90, 85, r+10, r+10);
ellipse(x+8, 85, r+10, r+10);
}
void note1(int note1x, int note1y) { // สร้างฟังก์ชัน note1 กำหนดให้มีการรับค่าตัวแปร note1x, int note1y
fill (255) ;
stroke (255) ; // ใส่สีให้เส้น
strokeWeight (3) ; // กำหนดขนาดเส้น
ellipse (note1x+50, note1y+50, 40, 30) ;
line (note1x+70, note1y+50, note1x+70, note1y-20) ; // สร้างเส้นตรง
ellipse (note1x+120, note1y+20, 40, 30) ;
line (note1x+140, note1y+20, note1x+140, note1y-50) ;
line (note1x+70, note1y-20, note1x+140, note1y-50) ;
}
void note2(int note2x, int note2y) { //สร้างฟังก์ชัน note2 กำหนดให้มีการรับค่าตัวแปร note2x, int note2y
fill (255) ;
stroke (255) ;
strokeWeight (3) ;
ellipse (note2x+50, note2y+50, 40, 30) ;
line (note2x+70, note2y+50, note2x+70, note2y) ;
}
void draw() { //เป็นฟังก์ชันที่จะถูกเรียกใช้งานต่อจากฟังก์ชัน void setup(){ }

ภายใน{ } ของ void draw(){ }มีคำสั่งต่างๆสำหรับวาดรูปกราฟิกเมื่อโปรแกรมทำงาน ฟังก์ชันนี้จะถูกเรียกใช้งานอย่างต่อเนื่องไปเรื่อยๆ
drawCassette(); //เรียกใช้ ฟังก์ชัน drawCassette
note1 (note1move, 100) ; // เรียกใช้ฟังก์ชัน note1 พร้อมกำหนดค่าที่ต้องการ
note2 (note2move, 100) ; // เรียกใช้ฟังก์ชัน note2 พร้อมกำหนดค่าที่ต้องการ
note1move = note1move -1 ; // กำหนดให้มีการเปลี่ยนค่าตัว เมื่อวาดซ้ำ จะทำให้ object เลื่อตำแหน่ง (เหมือนมีการเคลื่อนไหว)
note2move = note2move +1 ; // กำหนดให้มีการเปลี่ยนค่าตัว เมื่อวาดซ้ำ จะทำให้ object เลื่อนตำแหน่ง (เหมือนมีการเคลื่อนไหว)
if (note1move<-200 || note2move>300) { /* เงื่อนไข ถ้า note1move มีค่าน้อยกว่า -200 หรือ ( || หมายถึง หรือ)
note1move = 20 ; note2move มีค่ามากว่า 300 จะให้มีการรันโค้ดด้านล่าง */
note2move = 100 ; // กำหนดให้ note1move กลับไปมีค่าเป็น 20 และ note2move มีค่าเป็น 100
}
}