วันพฤหัสบดีที่ 30 ตุลาคม พ.ศ. 2557

Hours of using Internet 3 year.

void setup()
{
  int [][] num_net = {
    {
      2, 5, 9, 3
    }// First Month and each element is each week of this month
    , {
      4, 3, 8, 6
    }//Second Month
    ,
    {
      7, 10, 5, 4
    }//Third Month
  };
  for (int i = 0; i<num_net.length; i++) {
    println ("Sum in "+(i+1)+" month is "+(Sum(num_net[i])));
    println ("Max in "+(i+1)+" month is "+(Max(num_net[i])));
    println ("Min in "+(i+1)+" month is "+(Min(num_net[i])));
    println ("Average in "+(i+1)+" month is "+(Average(num_net[i])));
    println();
  }
  println ("Total Sum Of "+(num_net.length)+" is "+(Sum_All(num_net)));
  println ("Max in All Month is "+(Max_in_All(num_net)));
  println ("Min in all Month is "+(Min_in_All(num_net)));
  println ("Total Average Of "+(num_net.length)+" is "+(Average_All(num_net)));
}
///////////////////////
int Sum (int [] a) {
  int sum = 0;
  for (int i=0; i<a.length; i++) {
    sum  = sum + a[i];
  }
  return sum;
}

int Sum_All (int [][] a) {
  int sum = 0;
  for (int i = 0; i<a.length; i++) {
    for (int j =0; j<a.length; j++) {
      sum = sum + a[i][j];
    }
  }
  return sum;
}
////////////////////////
int Max (int [] a) {
  int max = 0;
  for (int i =0; i<a.length; i++) {
    if (max<=a[i]) {
      max = a[i];
    }
  }
  return max;
}

int Max_in_All (int [][] a) {
  int Max = 0;
  for (int i = 0; i<a.length; i++) {
    for (int j =0; j<a[0].length; j++) {
      if (a[i][j]>=Max) {
        Max = a[i][j];
      }
    }
  }
  return Max;
}
/////////////////////////
int Min (int [] a) {
  int min = a[0];
  for (int i = 0; i<a.length; i++) {
    if (min>=a[i]) {
      min = a[i];
    }
  }
  return min;
}
int Min_in_All (int [][] a) {
  int Min = a[0][0];
  for (int i = 0; i<a.length; i++) {
    for (int j = 0; j<a.length; j++) {
      if (Min >= a[i][j]) {
        Min = a[i][j];
      }
    }
  }
  return Min;
}
////////////////////////
float Average(int [] a)
{
  float sum=0;
  for (int i=0; i<a.length; i++)
  {
    sum = sum+a[i];
  }
  float avg = sum/a.length;
  return avg;
}

float Average_All (int [][]a) {
  int sum = 0;
  float Avg =0;
  for (int i=0; i<a.length; i++) {
    for (int j = 0; j<a[0].length; j++) {
      sum = sum + a[i][j];
    }
  }
  Avg = sum/((a.length)*(a[0].length));
  return Avg;
}

วันพุธที่ 22 ตุลาคม พ.ศ. 2557

Class Balloon

//balloon
Balloon b;
Balloon [] a = new Balloon [3];

void setup () {
  size (500, 500);
  b = new Balloon (250, 250, 100);
  a [0] = new Balloon (100, 300, 50);
  a [1] = new Balloon (400, 300, 50);
  a [2] = new Balloon (250, 400, 20);
}

void draw () {
  background (255);
  b.display();
  a[0].display();
  a[1].display();
  a[2].display();
  b.move_up();
  a[0].move_up();
  a[1].move_up();
  a[2].move_up();
}

class Balloon {
  int x;
  int y;
  int r;

  Balloon (int x, int y, int r) {
    this.x = x;
    this.y = y;
    this.r = r;
  }

  void display () {
    fill (255, 255, 0);
    line (x, y, x, y+r);
    ellipse (x, y, r, r);
  }

  void move_up () {
    this.y = this.y -1;
    if (this.y <= 10) {
      this.y = height;
    }
  }
}

วันศุกร์ที่ 3 ตุลาคม พ.ศ. 2557

Write A Function To Create Random Values In An Array 2



 (with parameter to specify whether duplicate values are allowed)

int [] value  = new int [5];
void setup () { 
  size (130, 50);
  background (0, 0, 204);
  Random_value (10, 50);
}
void Random_value (int start, int end) {
  int space = 15;
  text ("{", 10, 30);
  for (int i = 0; i<value.length; i++) {
    value [i] = int (random (start, end));
  }
  for (int i = 0; i<value.length; i++) {
    if (i < value.length) {
      text (value[i], space, 30);
      space = space + 20;
      if (i<value.length-1) {
        text (",", space-5, 30);
      }
    }
    if (i == value.length-1) {
      text ("}", 110, 30);
    }
  }

}

write a function to create random values in an array 1

 

(duplicate values are allowed)

int [] value  = new int [10];

void setup () {
  size (230, 50);
  background (0);
  Random_value (15);
}
void Random_value (int space) {
  text ("{", 10, 30);
  for (int i = 0; i<value.length; i++) {
    value [i] = int (random (100));
  }
  for (int i = 0; i<value.length; i++) {
    if (i < value.length) {
      text (value[i], space, 30);
      space = space + 20;
      if (i<value.length-1) {
        text (",", space-5 , 30);
      }
    }
    if (i == value.length-1) {
      text ("}", 215, 30);
    }
  }
}

Write a function to find the index of a value in an array.



e.g.  K = { 4, 8 , 5}
   5 has index = 2 in K
   4 has index = 0 in K
   9 is not in K

int [] k = {
  4, 8, 5
};

void setup () {
  size (130, 100);
  background (255, 255, 0);
  fill (0);
  Find_Index (5, 40);
  Find_Index (4, 60);
  Find_Index (9, 80);
}
void draw () {
}
int Find_Index (int value, int pointY) {
  boolean stop = false;
  for (int i = 0; i<k.length; i++) {
    if (k[i] == value) {
      text(+value+" has index = "+i+" in K", 10, pointY);
      stop = true;
    }  
    else if (i == (k.length-1) && stop == false) {
      text (+value+ " is not in K", 30, pointY);
    }
  }
  return value;

}

Calculate sum/average/max/min of values in array.


void setup () {
  int  [] value = {
    30, 55, 63, 78, 105, 47
  };
  size (150, 150);
  background (255, 102, 51);
  //show the value
  fill(255);
  textSize (15);
  text ("Sum is "+(Calculate_Sum(value)), 20, 30);
  text ("Average is "+(Calculate_Average(value)), 20, 60);
  text ("Max is "+(Calculate_Max(value)), 20, 90);
  text ("Min is "+(Calculate_Min(value)), 20, 120);
}

void draw () {
}

float Calculate_Sum (int [] a) {
  float sum = 0;
  for (int i = 0; i<a.length; i++) {
    sum = sum + a[i];
  }
  return sum;
}

float Calculate_Average (int [] a) {
  float Average = 0;
  Average = Calculate_Sum(a)/a.length;
  return Average;
}

int Calculate_Max (int []a) {
  int Max = 0;
  for (int i = 0; i<a.length; i++) {
    if (a[i]>=Max) {
      Max = a[i];
    }
  }
  return Max;
}

int Calculate_Min (int [] a) {
  int Min = Calculate_Max(a);
  for (int i =0; i<a.length; i++) {
    if (a[i] <= Min) {
      Min = a[i];
    }
  }
  return Min;
}

วันพุธที่ 1 ตุลาคม พ.ศ. 2557

Histogram (Bar char) in Array & Loop & Function


int [] bar_tableX = {
  20, 80, 140, 200, 260, 320, 380, 440
};
int [] value = {
  200, 150, 30, 75, 100, 180, 55, 130
};

void setup() {
  size(500, 500);
}

void draw() {
  background(255);
  fill (102, 0, 153);
  textSize(25);
  text ("Histogram", (width/2)-50, 40);
  bar(bar_tableX, value);
  Detail_Of_Bar(bar_tableX, value);
}


void bar(int [] a, int [] b)
{
  for (int i=0; i<a.length; i++)
  {
    if (b[i]==max(b))
    {
      fill(225, 0, 51);
    }
    if (b[i]==min(b))
    {
      fill(153, 250, 0);
    }
    if (b[i]!=max(b)&&b[i]!=min(b))
    {
      fill(255, 255, 0);
    }
    noStroke();
    rect(a[i], 310, 40, (-b[i]));
    stroke(51, 0, 0);
    strokeWeight (2);
  }
}

void Detail_Of_Bar(int [] a, int [] b)
{
  float avg = 0;
  float Total=0;
  float count = 0;
  //draw  grid
  line(a[0]-10, -max(b)+270, a[0]-10, 315);
  fill(51, 0, 0);
  textSize(15);
  text("y", a[0]-10, -max(b)+260);
  line(a[0]-10, 315, max(a)+50, 315);
  text("x", max(a)+40, 330);

  for (int i=0; i<a.length; i++)
  {
    //caculate Sum
    text(b[i], a[i], 335);
    float Sum = b[i] ;
    Total = Total+Sum;
    count++;
  }
  //calculate Avg.
  avg = Total/count;
  stroke(0, 51, 204);
  strokeWeight (1);
  line (30, 315-avg, 490, 315-avg);
  //show average bar
  fill (0, 0, 255);
  text ((avg), (width/2)-20, (310-avg));
 
  //show detail
  noStroke ();
  text(" Max = "+max(b), (width/2)-30, 400);
  fill(225, 0, 51);
  rect((width/2)-60, 385, 15, 15);

  fill(51, 0, 0);
  text(" Min = "+min(b), (width/2)-30, 420);
  fill(153, 250, 0);
  rect((width/2)-60, 405, 15, 15);

  fill(51, 0, 0);
  text(" Average = "+avg, (width/2)-30, 440);
  fill(0, 51, 204);
  rect((width/2)-60, 425, 15, 15);
}

void mousePressed()
{
  for (int i=0; i<bar_tableX.length; i++)
  {
    if (mouseButton==LEFT&&mouseX>=bar_tableX[i]&&mouseX<=(bar_tableX[i]+40))
    {
      value[i]=int(random(210));
    }
  }
}

Balloon in Array & Loop & Function


int [] y = {
  500, 400, 300
};

int [] x = {
  100, 200, 300
};

int [] R = {
  255, 153, 0
};

int [] G = {
  0, 255, 51
};

int [] B = {
  153, 0, 255
};
int [] size = {
  75, 100, 150
};
void setup() {
  size(400, 400);
}
void draw() {
  background(255);
  for (int i=0; i<y.length; i++) {
    fill (R[i], G[i], B[i]);
    draw_balloon(x[i], y[i], size[i]);
    y[i]=y[i]-1;
    if (y[i] <= 0) {
      y[i] = height;
    }
  }
}
void mousePressed () {
  for (int i=0; i<y.length; i++) {
    if (mouseButton==LEFT&&mouseX>=(x[i])&&mouseX<=(x[i]+size[i]))
    {
      y[i] = height+100;
    }
  }
}
void draw_balloon(int x, int y, int size) {
  strokeWeight(3);
  stroke (0);
  line(x, y, x, y+size);
  noStroke();
  ellipse(x, y, size, size);
}

Pencil in Array & Function & Loop



int [] highPen = {
  75, 150
};
int [] colorr = {
  51, 102, 25
};
int a = 150;
void setup () {
  size (350, 300);
}
void draw () {
  background (204, 255, 0);
  text ("Pess Down or UP Arrow to control them",(width/2)-100 ,height-50 );
  int highPenY= a;
  for (int i=0; i<highPen.length; i++) {
    draw_pencil (highPen[i], colorr[i]);
  }
  draw_pencil (highPenY, colorr[2]);

  if (keyCode == DOWN) {
    a = a +1;
  }
  else {
    a = a -1;
    if (a <= 0) {
      a =height-50;
    }
  }
}

void draw_pencil (int highPen, int colorr) {
  noStroke ();
  fill (153, colorr, 0);
  triangle (20, highPen, 70, (highPen-20), 70, (highPen+20));
  rect (70, highPen-20, 200, 40);
  fill (255);
  rect (270, highPen-20, 30, 40);
  fill (0);
  triangle (20, highPen, 40, (highPen-7), 40, (highPen+7));
}

The Turtle in Array & Function & Loop


int [] moveTutle = {
  100, 300
};
int [] size = {
  10, 20, 40
};

int [] colorr = {
  255, 153, 102
};

int a = 500;
void setup () {
  size (300, 600);
  background (255);
}

void draw () {
  background (255);
  int moveTutleY = a;
    if (mouseX<=mouseY) {
    background (0, 0, 204);
  }
  for (int i=0; i<moveTutle.length; i++) {
    draw_tutle (moveTutle[i], size[i], colorr[i]);
  }
  draw_tutle (moveTutleY, size[2], colorr[2]);
  a = a - 1;
  if (a <= 0) {
    a = height;
  }
}

void draw_tutle (int moveTutle, int size, int colorr) {
  noStroke ();
  fill (255, 102, 0);
  ellipse ((width/2)-50, moveTutle-40, size, size/2);
  ellipse ((width/2)+50, moveTutle-40, size, size/2);
  ellipse ((width/2)-50, moveTutle+40, size, size/2);
  ellipse ((width/2)+50, moveTutle+40, size, size/2);

  ellipse (width/2, moveTutle-75, size, size+30);

  fill (colorr, 51, 0);
  ellipse (width/2, moveTutle, size+80, size+130);
  fill (colorr, 204, 0);
  ellipse (width/2, moveTutle, size+60, size+110);
}

The Bicycle in Array & Function & Loop

int colour [] = {
  0, 102
};
int fill [] = {
  102, 153
};
int driveBicycle [] = {
  150, 400
};
int a = 700;

void setup () {
  size (600, 300);
  frameRate(50);
}

void draw () {
  int driveBicycleA = a;

  background (255);
  a = a-1;
  if (a <= 0) {
    a =width;
  }
  if (mouseX>= width/2 && mouseY >= height/2) {
    fill(0);
    textSize(20);
    text ("The Bicycle", (width/2)-50, 30);
  }
  draw_bicycle (driveBicycleA, 204, 255);

  for (int i = 0; i<driveBicycle.length; i++) {
    draw_bicycle (driveBicycle[i], fill[i], colour[i]);
  }
}

void draw_bicycle (int driveBicycle, int fill, int colour) {
  int i = 0;
  strokeWeight (10);
  stroke (102, 51, 0);
  line (driveBicycle, (height/2)-30, driveBicycle+30, (height/2)-30);

  stroke (colour, 51, 0);
  line (driveBicycle-50, (height/2), driveBicycle+50, (height/2));
  line (driveBicycle-50, (height/2)-100, driveBicycle-50, (height/2)+20);
  line (driveBicycle-50, (height/2)-50, driveBicycle+50, (height/2));

  stroke (0);
  fill (fill);
  ellipse (driveBicycle-50, (height/2)+50, 70, 70);
  ellipse (driveBicycle+70, (height/2)+30, 100, 100);
  ellipse (driveBicycle-50, (height/2)+50, 20, 20);
  ellipse (driveBicycle+70, (height/2)+30, 50, 50);

  strokeWeight (15);
  stroke (255, 51, 0);
  line (driveBicycle-70, (height/2)-100, driveBicycle-10, (height/2)-100);
}