วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Solar




int i=0;
int[] moon= {
  0, 0, 1, 2, 39, 30, 21, 8
};
int[] radius= {
  10, 15, 15, 12, 50, 40, 30, 27
};
int[] distant= {
  8, 50, 90, 130, 170, 230, 280, 330
};
color[] c= {
  #903CD6, #3F2CF2, #07B711, #DEA241, #FF9D24, #D2E3A3, #8CD2E3, #83AABC
};
String[] planet = {
  "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Ouranos", "Neptune"
};

void setup() {
  background(0);
  size(500, 300);
}
void draw() {
  fill(random(209), random(54), random(12));
  ellipse(0, 150, 140, 140);
  ellipse(distant[5]+90, 150, 55, 7);
  drawstar();
  //drawmoon();
}
int a;
void drawstar() {

  while (i<distant.length) {
    fill(c[i]);
    text(planet[i], distant[i]+75, 200);
    ellipse(distant[i]+90, 150, radius[i], radius[i]);
    fill(255, 255, 0);

    if (a<moon[i]) {
      ellipse(distant[i]+90+a, 100, 3, 3);
      a=a+1;
    }else
     {a=0;}
    i=i+1;
  }
}

Fraction

class Fraction {
  int n;
  int d;
  Fraction(int a, int b) {
    n=a;
    d=b;
  }
  void add(int z) {
    this.n=this.d*z+this.n;
  }
  void minus(int z) {
    this.n=this.n-z*this.d;
  }
  void mul(int s) {
    this.n=this.n*s;
  }
  String toString() {
    String s =this.n+"/"+this.d;
    return s;
  }
  void reciprocal() {
    int temp;
    temp=n;
    n=d;
    d=temp;
  }
}
void setup() {
  background(0);
  Fraction f =new Fraction(9, 5);
  Fraction g =new Fraction(9, 8);
  g.add(5);
  f.minus(8);
  f.mul(3);
  textSize(10);
  text(f.toString(),10,40);
  text(g.toString(),10,70);
    text(g.toString(),10,70);
}

สามารถรันโค้ดได้ที่ : http://processingjs.org/tools/processing-helper.html


Array:Easy

Question : Complete the methods that draw a bar chart from an array of integer data
                สร้างฟังก์ชันวาดแผนภูมิจาก array ของข้อมูลจำนวนเต็ม

Answer :
void setup() {
  size (450, 450);
  background(25);
}
void draw () {
  int x = 50;
  int i = 0;
  int y = 50;
  int [] B = {13,20, 14,10, 22,28, 32 } ;
                 
  while (i < B.length) {
    textSize(10);
    fill(#1B9AF7);
    rect (x, 380, 20, -B[i]*10);
    fill (#FC0AA8);
    text(B[i]+"", x,400);
    i = i + 1;
    x = x + 50;
  }
  textSize(19);
  fill (#FC0AA8);
  text("integer data ", 50, 50);
}
source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3


Function : Easy

   Question : Code a method which draws a circle,giveen the coordinates of the centre and the radius.It header should be
    สร้างฟังก์ชันที่วาดวงกลมโดยมีการกำหนด centre  และ radius

         private void circle(Graphics g,int xCentre, int yCentre, int radius)

   Answer :

          void setup() {
             size(200, 200);
             background(0);
          }
          void draw() {
             fill(#F5076F);
             circle(100, 100, 100);
          }
          void circle(int xCentre, int yCentre, int radius) {
             ellipse(xCentre, yCentre, radius, radius);
          }

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3



Chessboard

void setup() {
  background(0);
  size(400, 400);//
}
  int [][]a= { {  0, 100,200, 300 }, {  50, 50,50,50},{150,150,150, 150},{250,250,250,250},{350,350,350,350 } };//มีการประกาศและกำหนดเป็น array
  int i=0;//
  void draw(){
    drawOthello();//
  }

void drawOthello() {//

  while (i<a[0].length) {
    rect(a[0][i],a[2][i] , 50, 50);
    rect(a[0][i],a[1][i] , 50, 50);
    rect(a[0][i],a[3][i] , 50, 50);
    rect(a[0][i],a[4][i] , 50, 50);
    rect(a[0][i]+50,a[2][i]-50 , 50, 50);
    rect(a[0][i]+50,a[1][i]-50 , 50, 50);
    rect(a[0][i]+50,a[3][i]-50 , 50, 50);
    rect(a[0][i]+50,a[4][i]-50 , 50, 50);
    i=i+1;
  }

}




Button






...
class button {
  int x;
  int y;
  String name;
  int i;
  button(String n, int x, int y) {
    this.name=n;
    this.x=x;
    this.y=y;
  }
  void display() {
    fill(#E535A5);
    rect(x, y, 160, 50);
    textSize(20);
    fill(0);
    if ((mousePressed&&mouseX>=x&&mousePressed&&mouseX<=x+80)&&(mousePressed&&mouseY>=y&&mousePressed&&mouseY<=y+50)) {
      text("Panadda ", 100, 110);
    }else {
      text("Click ", 120, 110);
    }
  }
  void click() {
    if ((mousePressed&&mouseX>=x&&mousePressed&&mouseX<=x+80)&&(mousePressed&&mouseY>=y&&mousePressed&&mouseY<=y+50)) {
      if (i%8==0) {
        println(this.name);
      }
      i=i+1;
    }
  }
}
void setup() {
  size(300, 300);
}
void draw() {
  background(0);
  button a =new button("Panadda", 65, 80);
  a.display();
  a.click();
}

For loop

This program prints a four-line triangle of asterisks(*)
เครื่องหมายดอกจันทร์เรียงกันเป็ยสามเหลี่ยมจำนวนสี่แถว

for(int i=1;i<=4;i++){
  for(int j=1;j<=i;j++)
  print("*");
  println();
}


Reversestring


void setup(){
String Vocab = "VOCABULARY"; //ใช้สตริงในการประกาศและกำหนดตัวอักษร print(Vocab+" reverse is "+reverseString(Vocab));//statement ให้ทำการแสดงผล } String reverseString(String A){ String R = "";//ประกาศสตริงเพื่อเก็บตัวอักษรใหม่ int i=0;//ประการและกำหนดเพื่อใช้ในการวนลูป while(i<A.length()){//เงื่อนไข while loop จะกระทำซ้ำๆตามเงื่อนไขมีการวนจนกว่าจะไม่เป็นไปตามเงื่อนไข จากนั้นก็จะจบการทำงานภายใน while loop char n=A.charAt(A.length()-1-i); R=R+n;//เก็บค่าตัวอักษรไว้ที่สตริง R i=i+1;//บวกเพิ่มเพื่อกลับไปวนลูป }//เมื่อไม่เป็นไปตามเงื่อนไขด้านบนก็จะจบการทำงานและมีการรีเทิร์นค่าของ R return R;//รีเทิร์นค่า R เพื่อนำไปใช้ }






การแสดงผล : VOCABULARY reverse is YRALUBACOV

วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

Othello




void setup() {
  size(400, 400);//ขนาดของ canvas
}
  int [][]a= { {  0, 50, 100, 150, 200, 250, 300, 350 }, { 0, 50, 100, 150, 200, 250, 300, 350 } };//มีการประกาศและกำหนดเป็น array
  int i=0;//ประกาศและกำหนดตัวเเปรเพื่อนำไปใช้ในการวนลูป
  void draw(){
    drawOthello();//เรียกใช้ user function
  }

void drawOthello() {//user function สร้างขึ้นมาเอง

  while (i<a[0].length) {//เงื่อนไข while loop จะกระทำซ้ำๆตามเงื่อนไขมีการวนจนกว่าจะไม่เป็นไปตามเงื่อนไข จากนั้นก็จะจบการทำงานภายใน while loop
    rect(a[0][i],a[1][0] , 50, 50);
    rect(a[0][i],a[1][1] , 50, 50);
    rect(a[0][i],a[1][2] , 50, 50);
    rect(a[0][i],a[1][3] , 50, 50);
    rect(a[0][i],a[1][4] , 50, 50);
    rect(a[0][i],a[1][5] , 50, 50);
    rect(a[0][i],a[1][6] , 50, 50);
    rect(a[0][i],a[1][7] , 50, 50);
    i=i+1;
  }  //เมื่อไม่เป็นไปตามเงื่อนไขด้านบนก็จะจบการทำงานและทำ ststment ด้านล่างนี้
  strokeWeight(9);//ขนาดของเส้นที่ใช้วาด  fill(250);//สีขาวใช้ในการวาดรูปวงกลม
  ellipse(a[0][5],a[1][5] , 40, 40);
  ellipse(a[0][4],a[1][5] , 40, 40);
  fill(0);//สีดำใช้ในการวาดวงกลม
  ellipse(a[0][4],a[1][4] , 40, 40);
  ellipse(a[0][5],a[1][4] , 40, 40);
}


OX



 void setup() {
  size(300, 300);
}
  int [][]a= {{ 0, 100, 200},{ 0, 100, 200 }  };//มีการประกาศและกำหนดเป็น array
  int i=0;  //ประกาศและกำหนดตัวเเปรเพื่อนำไปใช้ในการวนลูป

  void draw(){
  drawOtello();//เรียกใช้ user function
  }

void drawOtello() {//เป็น user fuction ที่เราสร้างขึ้นมาเอง
  fill(250);//การใส่สีขาว
  strokeWeight(7);//ขนาดของเส้นที่ใช้วาด
  stroke(0);
  while (i<a[0].length) {//เงื่อนไข while loop จะกระทำซ้ำๆตามเงื่อนไขมีการวนจนกว่าจะไม่เป็นไปตามเงื่อนไข จากนั้นก็จะจบการทำงานภายใน while loop
    rect(a[0][i],a[1][0] , 100, 100);//ช่องสี่เหลี่ยมด้านบน
    rect(a[0][i],a[1][1] , 100, 100);//ช่องสี่เหลี่ยมตรงกลาง
    rect(a[0][i],a[1][2] , 100, 100);//ช่องสี่เหลี่ยมด้านล่าง
    i=i+1;
  }  //เมื่อไม่เป็นไปตามเงื่อนไขด้านบนก็จะจบการทำงานและทำ ststment ด้านล่างนี้
  strokeWeight(9);//ขนาดของเส้นที่ใช้วาด
  fill(250);//พื้นหลังของวงกลมมีสีขาว
  stroke(245,103,93);//สีของเส้นที่ใช้ในการวาด
  ellipse(a[0][1]+50,a[1][0]+50 , 80, 80);//วาดวงกลมด้านบนช่องกลางโดยนำค่ามาจาก array
  ellipse(a[0][2]+50,a[1][1]+50, 80, 80);
  stroke(25,153,245);
  line(a[0][1],a[1][2] ,a[0][2],a[1][1] );//วาดเส้นกากบาท
  line(a[0][1],a[1][1] ,a[0][2],a[1][2] );

}

วันพฤหัสบดีที่ 19 กันยายน พ.ศ. 2556

Parallel array





void setup() {  
  size (450, 450);
  background(25);
}
void draw () {
  int x = 50;
  int i = 0;
  int j = 0;
  int y = 50;
  float [][] B = { { 13.51, 14.74, 12.598, 32.65, 26.50 } ,
                         { 8.86, 11.75, 15.07, 39.32, 24.99  } };

  while (i < B[0].length) {
    textSize(10);
    fill(#23FC55);
    rect (x, 380, 20, -B[0][i]*10);
    text(B[0][i]+"%", x,400);
    fill(#FC7C03);
    rect (x+20, 380, 20, -B[1][j]*10);
    text(B[1][i]+"%", x+20,400+10);
    i = i + 1;
    x = x + 70;
  }
  textSize(15);
  fill (#FC0AA8);
  text("No. of marine fishery establishments ", 30, 30);
}

วันพุธที่ 18 กันยายน พ.ศ. 2556

ARRAY : MEDIUM

Question  Graph drawer  Write a class that provides a method to draw a graph of a function given as an array of x-coordinates and an array of corresponding y-coordinates. It has the heading 

วาดกราฟ โดยเขียนฟังก์ชันวาดกราฟโดยให้ค่า x และ y ในรูปแบบของ array
              
              public void drawGraph(float[]x,float[]y)

              The method draws straight lines from one point to another.It also draws the axes.

ฟังก์ชั่นวาดเส้นตรงตามแกน

Answer

int i;
int j;
float[]x={5,20,50,75.5,105,130,150,180,195};
float[]y={150,50,130,80,100,60,80,10,50};

void setup(){
size(200,200);
strokeWeight(5);
background(250);
drawGraph();
}
void drawGraph(){
  while(i<x.length&&j<=x.length){
    j=i+1;
  line(x[i],y[i],x[j],y[j]);
  ellipse(x[i],y[i],5,5);
  i=i+1;
  j=j+2;
  }
}





source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3

วันอังคารที่ 3 กันยายน พ.ศ. 2556

GCD and LCD

void setup(){
  gcd(100,83);// เรียกใช้ user function
  println(gcd(100,83));//การแสดงผลของ ห.ร.ม.
  println(lcd(100,83));//การแสดงผลของ ค.ร.น.

}

int gcd(int x, int y){//เป็น user function ชนิดพรามิเตอร์ที่มีการรีเทิร์นค่า(หา ห.ร.ม)
    if(x==0){//ถ้า x เท่ากับ 0 ให้รีเทิร์นค่า y ไปใช้ได้เลย(ส่วนนี้เป็น base case)
      return y;
    }
    if(y==0){//ถ้า y เท่ากับ 0 ให้รีเทิร์นค่า x ไปใช้ได้เลย(ส่วนนี้เป็น base case)
      return x;
    }
    if(x>y){
      return gcd (y,x%y);//ถ้า x มากกว่า y ให้ให้รีเทิร์นค่า y และเศษที่เหลือจาก x mod y
    }else{
      return gcd (x,y%x);//ถ้า y น้อยกว่า x ให้ให้รีเทิร์นค่า x และเศษที่เหลือจาก y mod x
    }
//มีการเรียกใช้ตัวเอง(recursive case)
}

int lcd(int x, int y){//เป็น user function ชนิดพรามิเตอร์ที่มีการรีเทิร์นค่า(หา ค.ร.น)
  return (x*y)/gcd(x,y);//เอาจำนวนทั้งสองมาคูณกันแล้วหารด้วย ห.ร.ม ของจำนวนเหล่านั้น
}