def setup():
print("Summary of prime numbers = ",sum_prime(10))
def sum_prime(number):
first = 2
sum = 0
while (first <= number):
if (prime_number(first)):
sum += first
first += 1
return sum
def prime_number(p):
count = 2
if (p==1):
return False
while (count<p):
if (p%count == 0):
return False
count += 1
return True
setup()
วันพุธที่ 16 กันยายน พ.ศ. 2558
Lab 4x Summary of integers
def setup():
count=20
print("summary = ",sum_int(count))
def sum_int(count):
n = 1
sum = 0
while (n <= count):
sum = sum+n
n = n+1
return sum
setup()
count=20
print("summary = ",sum_int(count))
def sum_int(count):
n = 1
sum = 0
while (n <= count):
sum = sum+n
n = n+1
return sum
setup()
Lab 4x : Multiplication tables
def setup():
n = 1
m = 12
x = 18
print("Multiplication of ",x)
while (n<=m):
sum = x*n
print(x," * ",n," = ",sum)
n = n+1
setup()
n = 1
m = 12
x = 18
print("Multiplication of ",x)
while (n<=m):
sum = x*n
print(x," * ",n," = ",sum)
n = n+1
setup()
Lab 4x : Leap year
def setup():
print("Leap Year Calculator")
calculate_year()
def calculate_year ():
year = 1995
mod4 = year%4
mod100 = year%100
mod400 = year%400
if (mod4 == 0):
if (mod100 != 0):
print(year," : Leap Year")
elif (mod100 == 0):
if (mod400 != 0):
print(year," : Not A Leap Year")
elif (mod400 == 0):
print(year," : Leap Year")
elif (mod4 != 0):
print(year," : Not A Leap Year")
setup()
print("Leap Year Calculator")
calculate_year()
def calculate_year ():
year = 1995
mod4 = year%4
mod100 = year%100
mod400 = year%400
if (mod4 == 0):
if (mod100 != 0):
print(year," : Leap Year")
elif (mod100 == 0):
if (mod400 != 0):
print(year," : Not A Leap Year")
elif (mod400 == 0):
print(year," : Leap Year")
elif (mod4 != 0):
print(year," : Not A Leap Year")
setup()
Lab 4x : Grade
def setup():
score=65.8
show_score(score)
calculate_grade(score)
def show_score(score):
print("YOUR SCORE : ",score)
def calculate_grade(score):
if (score >= 80 and score <= 100):
print("YOU GET A")
elif (score >= 70 and score <= 79):
print("YOU GET B")
elif (score >= 60 and score <= 69):
print("YOU GET C")
elif (score >= 50 and score <= 69):
print("YOU GET D")
elif (score >= 0 and score <= 49):
print("YOU GET F")
else:
print("NOT WORKING...")
setup()
score=65.8
show_score(score)
calculate_grade(score)
def show_score(score):
print("YOUR SCORE : ",score)
def calculate_grade(score):
if (score >= 80 and score <= 100):
print("YOU GET A")
elif (score >= 70 and score <= 79):
print("YOU GET B")
elif (score >= 60 and score <= 69):
print("YOU GET C")
elif (score >= 50 and score <= 69):
print("YOU GET D")
elif (score >= 0 and score <= 49):
print("YOU GET F")
else:
print("NOT WORKING...")
setup()
Lab 4x : Calculate circumference and area of a circle
def calculate_circle():
dia=50
rad=dia/2
area=3.14*rad*rad
cir=3.14*dia
print("Diameter : ",dia)
print("Area : ",area)
print("Circumference : ",cir)
calculate_circle()
dia=50
rad=dia/2
area=3.14*rad*rad
cir=3.14*dia
print("Diameter : ",dia)
print("Area : ",area)
print("Circumference : ",cir)
calculate_circle()
Lab 4x : Calculate body mass index (BMI)
def calculate_bmi():
weight=39 #weight-kilogram
Hcm=151 #height-centimeter
Hm=Hcm/100
bmi=weight/(Hm*Hm)
print("Weight : ",weight)
print("Height(centimeter) : ",Hcm)
print("BMI : ",bmi)
calculate_bmi()
weight=39 #weight-kilogram
Hcm=151 #height-centimeter
Hm=Hcm/100
bmi=weight/(Hm*Hm)
print("Weight : ",weight)
print("Height(centimeter) : ",Hcm)
print("BMI : ",bmi)
calculate_bmi()
วันอาทิตย์ที่ 13 กันยายน พ.ศ. 2558
Lab 4 : Syntax error
1. the local variable "space" may not have been initialized.
cause of problem : forgot to set a value of space
cause of problem : forgot a ; after y=(y+10)%height
cause of problem : forgot a } after return sum;
cause of problem : forgot to set a value of space
solution : set a value to space ---> int space=0;
2. Error on "::IdentifierOrNew"
expecting SEMI, found 'draw_balloon'
cause of problem : forgot a ; after y=(y+10)%height
solution : put a ; after that
3. Found one too many { characters without a } to match it
cause of problem : forgot a } after return sum;
solution : put a } after that
วันเสาร์ที่ 12 กันยายน พ.ศ. 2558
Lab 4 : Loan payment
void setup() {
loan_payment(5000, 0.05, 12);
}
void loan_payment(float amount, float rate, int month) {
float rate_permonth = rate/month;
float pay_permonth = amount*(rate_permonth/(1-pow(1+rate_permonth, -month)));
float principal = pay_permonth;
float remain = amount;
int x = 1;
println("No. Beginning Balance Interest Principal Ending Balance");
while (x <= month) {
print(nf(x, 2)); //Num
print(" "+nf(remain, 4, 2)); //remain
principal = pay_permonth-(rate_permonth*remain);
remain -= principal;
if (remain < 0) {
remain = 0;
}
print(" "+nf(rate_permonth*remain, 2, 2)); //Principal
print(" "+nf(principal, 3, 2)); //Unpaid
print(" "+nf(remain, 4, 2)); //Total
x++;
}
}
loan_payment(5000, 0.05, 12);
}
void loan_payment(float amount, float rate, int month) {
float rate_permonth = rate/month;
float pay_permonth = amount*(rate_permonth/(1-pow(1+rate_permonth, -month)));
float principal = pay_permonth;
float remain = amount;
int x = 1;
println("No. Beginning Balance Interest Principal Ending Balance");
while (x <= month) {
print(nf(x, 2)); //Num
print(" "+nf(remain, 4, 2)); //remain
principal = pay_permonth-(rate_permonth*remain);
remain -= principal;
if (remain < 0) {
remain = 0;
}
print(" "+nf(rate_permonth*remain, 2, 2)); //Principal
print(" "+nf(principal, 3, 2)); //Unpaid
print(" "+nf(remain, 4, 2)); //Total
x++;
}
}
Lab 4 : Summary of prime numbers
void setup() {
println("Summary of prime numbers = "+sum_prime(3));
}
int sum_prime(int number) {
int first_prime = 2;
int sum = 0;
while (first_prime<=number) {
if (prime_number(first_prime)) {
sum += first_prime;
}
first_prime++;
}
return sum;
}
boolean prime_number(int prime) {
boolean result = true;
int n=2;
while (n<prime) {
if (prime%n == 0) {
result = false;
}
n++;
}
if(prime == 1) {
result = false;
}
return result;
}
println("Summary of prime numbers = "+sum_prime(3));
}
int sum_prime(int number) {
int first_prime = 2;
int sum = 0;
while (first_prime<=number) {
if (prime_number(first_prime)) {
sum += first_prime;
}
first_prime++;
}
return sum;
}
boolean prime_number(int prime) {
boolean result = true;
int n=2;
while (n<prime) {
if (prime%n == 0) {
result = false;
}
n++;
}
if(prime == 1) {
result = false;
}
return result;
}
Lab 4 : Multiplication tables
void setup() {
int n = 1;
int m = 12;
int x = 18;
println("Multiplication of "+x);
while (n<=m) {
int sum = x*n;
println(+x+" * "+n+" = "+sum);
n++;
}
}
int n = 1;
int m = 12;
int x = 18;
println("Multiplication of "+x);
while (n<=m) {
int sum = x*n;
println(+x+" * "+n+" = "+sum);
n++;
}
}
Lab 4 : Summary of integers
void setup() {
println("summary = "+sum_int(20));
}
int sum_int(int count) {
int n = 0;
int sum = 0;
while (n <= count) {
sum += n;
n++;
}
return sum;
}
println("summary = "+sum_int(20));
}
int sum_int(int count) {
int n = 0;
int sum = 0;
while (n <= count) {
sum += n;
n++;
}
return sum;
}
Lab 4 : favorite song (loop)
int colour1;
int colour2;
int colour3;
int colour4;
int colour_passenger;
int colour_LetHerGo;
void setup() {
size(600, 360);
}
void draw() {
background(#B7F8FF);
text_PASSENGER(140, 60);
text_LetHerGo(200, 170);
draw_loop();
}
void draw_FerrisWheel(int x, int y) {
//Ferris wheel
noFill();
stroke(255);
strokeWeight(3);
//wheel_1
ellipse(100+x, 100+y, 200, 200);
//wheel_2
ellipse(100+x, 100+y, 180, 180);
//wheel_3
ellipse(100+x, 100+y, 100, 100);
//wheel_4
fill(255);
ellipse(100+x, 100+y, 30, 30);
//pole
stroke(255);
strokeWeight(6);
//poleL
line(85+x, 100+y, 0+x, 350+y);
//poleR
line(115+x, 100+y, 200+x, 350+y);
}
void draw_bench(int x, int y) {
//bench
noStroke();
//bench-1
fill(colour1);
ellipse(50+x, 20+y, 10, 10);
ellipse(50+x, 40+y, 40, 40);
//bench-2
fill(colour2);
ellipse(180+x, 50+y, 10, 10);
ellipse(180+x, 70+y, 40, 40);
//bench-3
fill(colour3);
ellipse(160+x, 175+y, 10, 10);
ellipse(160+x, 195+y, 40, 40);
//bench-4
fill(colour4);
ellipse(20+x, 150+y, 10, 10);
ellipse(20+x, 170+y, 40, 40);
}
void text_PASSENGER(int x, int y) {
//text"PASSENGER"
fill(colour_passenger);
textSize(60);
text("PASSENGER", x, y);
}
void text_LetHerGo(int x, int y) {
fill(colour_LetHerGo);
textSize(100);
//text"LET"
text("LET", x+10, y);
//text"HER"
text("HER", x, 80+y);
//text"GO"
text("GO", x+20, 160+y);
}
void keyPressed() {
colour1 = color(random(0, 225), random(0, 225), random(0, 225));
colour2 = color(random(0, 225), random(0, 225), random(0, 225));
colour3 = color(random(0, 225), random(0, 225), random(0, 225));
colour4 = color(random(0, 225), random(0, 225), random(0, 225));
colour_passenger = color(random(0, 225), random(0, 225), random(0, 225));
colour_LetHerGo = color(random(0, 225), random(0, 225), random(0, 225));
}
void draw_loop() {
int n=0;
int m=0;
while (n<3) {
draw_FerrisWheel(mouseX+m, mouseY);
draw_bench(mouseX+m, mouseY);
n++;
m+=200;
}
}
Lab 4 : favorite book (loop)
int wide=0;
int high=0;
int colour;
void setup() {
size(350, 450);
}
void draw() {
int posX=mouseX;
int posY=mouseY;
draw_wolf(posX,posY);
draw_text();
}
void draw_wolf(int x,int y) {
int n=0;
int m=0;
background(#a19d8c);
while(n<3){
//wolf : begin at left to right
fill(colour);
noStroke();
beginShape();
vertex(x+wide, 165+y+m); //01-no move
//over
vertex(70+x+wide, 96+y+m); //02
vertex(164+x+wide, 74+y+m); //03
vertex(188+x+wide, 50+y+m); //04
vertex(179+x+wide, 18+y+m); //05
vertex(195+x+wide, 30+y+m); //06
vertex(190+x+wide, 17+y+m); //07
vertex(205+x+wide, 30+y+m); //08
vertex(239+x+wide, y+m); //09
//under
vertex(243+x+wide, 10+y+high+m); //10
vertex(235+x+wide, 40+y+high+m); //11
vertex(247+x+wide, 27+y+high+m); //12
vertex(250+x+wide, 35+y+high+m); //13
vertex(230+x+wide, 60+y+high+m); //14
vertex(195+x+wide, 142+y+high+m); //15
vertex(198+x+wide, 175+y+high+m); //16
vertex(215+x+wide, 188+y+high+m); //17
vertex(200+x+wide, 190+y+high+m); //18
vertex(187+x+wide, 168+y+high+m); //19
vertex(190+x+wide, 190+y+high+m); //20
vertex(205+x+wide, 205+y+high+m); //21
vertex(190+x+wide, 205+y+high+m); //22
vertex(165+x+wide, 145+y+high+m); //23
vertex(105+x+wide, 135+y+high+m); //24
vertex(85+x+wide, 150+y+high+m); //25
vertex(85+x+wide, 167+y+high+m); //26
vertex(105+x+wide, 180+y+high+m); //27
vertex(85+x+wide, 180+y+high+m); //28
vertex(75+x+wide, 150+y+high+m); //29
vertex(60+x+wide, 160+y+high+m); //30
vertex(55+x+wide, 185+y+high+m); //31
vertex(70+x+wide, 197+y+high+m); //32
vertex(55+x+wide, 198+y+high+m); //33
vertex(45+x+wide, 160+y+high+m); //34
vertex(50+x+wide, 150+y+high+m); //35
endShape();
n++;
m -= 200;
}
}
void draw_text() {
fill(#2f4940);
textSize(80);
text("WHITE", 40, 75);
text("FANG", 60, 150);
textSize(30);
text("JACK LONDON", 70, 425);
}
void mouseClicked() {
colour = color(random(0, 225), random(0, 225), random(0, 225));
}
Lab 4 : favorite movie (loop)
int swW=0;
int swH=0;
void setup() {
size(450, 300);
}
void draw() {
int n=0;
int m=0;
background(#ffd800);
text_BILL();
while(n<5) {
draw_sword(mouseX+m,mouseY);
n++;
m-=100;
}
text_KILL();
}
void draw_sword(int x,int y) {
//sword
noStroke();
fill(0);
//black0-1
rect(350+x, 0+y, 50+swW, 140+swH);
//black0-2
rect(348+x, 140+y+swH, 54+swW, 30+swH);
//black0-3
rect(330+x, 170+y+swH, 90+swW, 8+swH);
//black0-4
rect(348+x, 178+y+swH, 54+swW, 3+swH);
//white0-5
fill(255);
rect(350+x, 181+y+swH+swH, 50+swW, 10+swH);
//black0-6
fill(0);
rect(348+x, 191+y+swH, 54+swW, 3+swH);
//black0-7
rect(350+x, 194+y+swH, 50+swW, 106+swH);
//shadow
//white1-1
fill(255);
rect(350+x, 0+y, 30+swW, 140+swH);
//white1-2
triangle(360+x, 194+swH+y, 390+swW+x, 194+swH+y, 375+x+(swW/2), 204+swH+y);
//white1-3
quad(360+x, 219+swH+y, 375+(swW/2)+x, 209+swH+y, 390+swW+x, 219+swH+y, 375+x, 229+swH+y);
//white1-4
quad(360+x, 244+swH+y, 375+(swW/2)+x, 234+swH+y, 390+swW+x, 244+swH+y, 375+x, 254+swH+y);
//white1-5
quad(360+x, 269+swH+y, 375+(swW/2)+x, 259+swH+y, 390+swW+x, 269+swH+y, 375+x, 279+swH+y);
//white1-6
quad(360+x, 294+swH+y, 375+(swW/2)+x, 284+swH+y, 390+swW+x, 294+swH+y, 375+x, 304+swH+y);
}
void text_KILL() {
textSize(150);
//text-KILL
fill(#ca0101);
text("KILL", 10, 140);
}
void text_BILL() {
textSize(150);
fill(0);
text("BILL", 10, 270);
//cut
stroke(#ffd800);
strokeWeight(3);
line(0, 260, 330, 180);
}
void draw_cut() {
fill(255);
strokeWeight(10);
line(random(0,width),0, random(0,width),300);
}
void keyPressed(){
frameRate(10);
fill(0,0,0,10);
rect(0,0,width,height);
fill(#ca0101);
ellipse(random(0,width), random(0,height), 150, 150);
}
Lab 4 : flocks of birds
int birdSize=50;
int colour=250;
int wingMove;
void setup(){
size(300,300);
}
void draw() {
background(#BBEEFA);
//flying_bird();
draw_flocks(mouseX, mouseY);
flying_bird();
}
void draw_bird(int x, int y) {
strokeWeight(0);
//body
fill(colour);
ellipse(x,y,birdSize,birdSize);
//eyes
fill(#675143);
ellipse(x-(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
ellipse(x+(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
//mouth
fill(#FFFF00);
triangle(x-(birdSize/4), y+(birdSize/6), x+(birdSize/4), y+(birdSize/6), x, y+birdSize/3);
//wings
strokeWeight(10);
stroke(colour);
line(x-(birdSize/2), y, x-birdSize, wingMove);
line(x+(birdSize/2), y, x+birdSize, wingMove);
}
void flying_bird() {
wingMove = mouseY;
if (frameCount%30 > 15) {
wingMove += birdSize/3;
} else {
wingMove -= birdSize/3;
}
}
void draw_flocks(int x,int y) {
int n=0;
int posX=0;
while (n<3) {
fill(colour);
draw_bird(mouseX+posX,mouseY);
n++;
posX += 100;
}
}
void keyPressed() {
colour = color(random(0, 225), random(0, 225), random(0, 225));
}
Lab 4 : balloons (loop)
int radius = 50;
int string_length = radius*2;
int posY=0;
int colour;
void setup() {
size(300, 300);
}
void draw() {
background(#BBEEFA);
frameRate(20);
int posX = radius/2;
draw_balloon(posX, 300-posX);
posY=(posY-10)%(height+125);
}
void draw_balloon(int x, int y) {
//int radius = 50;
int n=0;
int space=0;
while (n<7) {
fill(colour);
ellipse(x+space, y+posY, radius, radius);
stroke(0);
line(x+space, y+(radius/2)+posY, x+space, y + string_length+posY);
n++;
space+=radius;
}
}
void mouseMoved() {
colour = color(random(0, 225), random(0, 225), random(0, 225));
}
Lab 3 : favorite song (change colour)
int moveX=0;
int moveY=0;
int count=2;
int result;
int colour1;
int colour2;
int colour3;
int colour4;
void setup() {
size(600, 360);
}
void draw() {
frameRate(2);
result=count%2;
count=count+1;
draw_FerrisWheel();
draw_bench();
if (result==0) {
fill(0);
text_PASSENGER();
fill(#7a92be);
text_LetHerGo();
}
if (result==1) {
fill(#7a92be);
text_PASSENGER();
fill(0);
text_LetHerGo();
}
}
void draw_FerrisWheel() {
background(#7a92be);
//Ferris wheel
fill(#7a92be);
stroke(255);
strokeWeight(3);
//wheel_1
ellipse(100+moveX, 100+moveY, 200, 200);
//wheel_2
ellipse(100+moveX, 100+moveY, 180, 180);
//wheel_3
ellipse(100+moveX, 100+moveY, 100, 100);
//wheel_4
fill(255);
ellipse(100+moveX, 100+moveY, 30, 30);
//pole
stroke(255);
strokeWeight(6);
//poleL
line(85+moveX, 100+moveY, 0+moveX, 350+moveY);
//poleR
line(115+moveX, 100+moveY, 200+moveX, 350+moveY);
}
void text_PASSENGER() {
//text"PASSENGER"
textSize(60);
text("PASSENGER", 210+moveX, 70+moveY);
}
void text_LetHerGo() {
textSize(100);
//text"LET"
text("LET", 400+moveX, 170+moveY);
//text"HER"
text("HER", 300+moveX, 250+moveY);
//text"GO"
text("GO", 200+moveX, 330+moveY);
}
void draw_bench(){
//bench
noStroke();
//bench-1
fill(colour1);
ellipse(50+moveX, 20+moveY, 10, 10);
ellipse(50+moveX, 40+moveY, 40, 40);
//bench-2
fill(colour2);
ellipse(180+moveX, 50+moveY, 10, 10);
ellipse(180+moveX, 70+moveY, 40, 40);
//bench-3
fill(colour3);
ellipse(160+moveX, 175+moveY, 10, 10);
ellipse(160+moveX, 195+moveY, 40, 40);
//bench-4
fill(colour4);
ellipse(20+moveX, 150+moveY, 10, 10);
ellipse(20+moveX, 170+moveY, 40, 40);
}
void keyPressed(){
colour1 = color(random(0, 225), random(0, 225), random(0, 225));
colour2 = color(random(0, 225), random(0, 225), random(0, 225));
colour3 = color(random(0, 225), random(0, 225), random(0, 225));
colour4 = color(random(0, 225), random(0, 225), random(0, 225));
}
lab 3 : favorite movie (mouseMoved)
int swW=0;
int swH=0;
int moveX=0;
int moveY=0;
void setup() {
size(450, 300);
}
void draw() {
background(#ffd800);
text_BILL();
draw_sword();
text_KILL();
frameRate(30);
moveX=(moveX-10)%width;
}
void draw_sword() {
//sword
noStroke();
fill(0);
//black0-1
rect(350+moveX, 0+moveY, 50+swW, 140+swH);
//black0-2
rect(348+moveX, 140+moveY+swH, 54+swW, 30+swH);
//black0-3
rect(330+moveX, 170+moveY+swH, 90+swW, 8+swH);
//black0-4
rect(348+moveX, 178+moveY+swH, 54+swW, 3+swH);
//white0-5
fill(255);
rect(350+moveX, 181+moveY+swH+swH, 50+swW, 10+swH);
//black0-6
fill(0);
rect(348+moveX, 191+moveY+swH, 54+swW, 3+swH);
//black0-7
rect(350+moveX, 194+moveY+swH, 50+swW, 106+swH);
//shadow
//white1-1
fill(255);
rect(350+moveX, 0+moveY, 30+swW, 140+swH);
//white1-2
triangle(360+moveX, 194+swH+moveY, 390+swW+moveX, 194+swH+moveY, 375+moveX+(swW/2), 204+swH+moveY);
//white1-3
quad(360+moveX, 219+swH+moveY, 375+(swW/2)+moveX, 209+swH+moveY, 390+swW+moveX, 219+swH+moveY, 375+moveX, 229+swH+moveY);
//white1-4
quad(360+moveX, 244+swH+moveY, 375+(swW/2)+moveX, 234+swH+moveY, 390+swW+moveX, 244+swH+moveY, 375+moveX, 254+swH+moveY);
//white1-5
quad(360+moveX, 269+swH+moveY, 375+(swW/2)+moveX, 259+swH+moveY, 390+swW+moveX, 269+swH+moveY, 375+moveX, 279+swH+moveY);
//white1-6
quad(360+moveX, 294+swH+moveY, 375+(swW/2)+moveX, 284+swH+moveY, 390+swW+moveX, 294+swH+moveY, 375+moveX, 304+swH+moveY);
}
void text_KILL() {
textSize(150);
//text-KILL
fill(#ca0101);
text("KILL", 10, 140);
}
void text_BILL() {
textSize(150);
fill(0);
text("BILL", 10, 270);
//cut
stroke(#ffd800);
strokeWeight(3);
line(0, 260, 330, 180);
}
void draw_cut() {
fill(255);
strokeWeight(10);
line(random(0,width),0, random(0,width),300);
}
void mouseMoved(){
frameRate(5);
fill(0,0,0,10);
rect(0,0,width,height);
fill(#ca0101);
ellipse(random(0,width), random(0,height), 100, 100);
}
lab 3 : favorite book (change colour)
int wide=0;
int high=0;
int colour;
void setup() {
size(350, 450);
}
void draw() {
draw_wolf(0-243,180);
frameRate(10);
wide=(wide+10)%(width+250);
draw_text();
}
void draw_wolf(int x,int y) {
background(#a19d8c);
//wolf : begin at left to right
fill(colour);
noStroke();
beginShape();
vertex(x+wide, 165+y); //01-no move
//over
vertex(70+x+wide, 96+y); //02
vertex(164+x+wide, 74+y); //03
vertex(188+x+wide, 50+y); //04
vertex(179+x+wide, 18+y); //05
vertex(195+x+wide, 30+y); //06
vertex(190+x+wide, 17+y); //07
vertex(205+x+wide, 30+y); //08
vertex(239+x+wide, y); //09
//under
vertex(243+x+wide, 10+y+high); //10
vertex(235+x+wide, 40+y+high); //11
vertex(247+x+wide, 27+y+high); //12
vertex(250+x+wide, 35+y+high); //13
vertex(230+x+wide, 60+y+high); //14
vertex(195+x+wide, 142+y+high); //15
vertex(198+x+wide, 175+y+high); //16
vertex(215+x+wide, 188+y+high); //17
vertex(200+x+wide, 190+y+high); //18
vertex(187+x+wide, 168+y+high); //19
vertex(190+x+wide, 190+y+high); //20
vertex(205+x+wide, 205+y+high); //21
vertex(190+x+wide, 205+y+high); //22
vertex(165+x+wide, 145+y+high); //23
vertex(105+x+wide, 135+y+high); //24
vertex(85+x+wide, 150+y+high); //25
vertex(85+x+wide, 167+y+high); //26
vertex(105+x+wide, 180+y+high); //27
vertex(85+x+wide, 180+y+high); //28
vertex(75+x+wide, 150+y+high); //29
vertex(60+x+wide, 160+y+high); //30
vertex(55+x+wide, 185+y+high); //31
vertex(70+x+wide, 197+y+high); //32
vertex(55+x+wide, 198+y+high); //33
vertex(45+x+wide, 160+y+high); //34
vertex(50+x+wide, 150+y+high); //35
endShape();
}
void draw_text() {
fill(#2f4940);
textSize(80);
text("WHITE", 40, 75);
text("FANG", 60, 150);
textSize(30);
text("JACK LONDON", 70, 425);
}
void mouseClicked() {
colour = color(random(0, 225), random(0, 225), random(0, 225));
}
Lab 3 : flying bird
int birdSize=100;
int x=30;
int colour=250;
int wingMove;
void setup(){
size(300,300);
}
void draw() {
background(#BBEEFA);
wingMove = mouseY;
if (frameCount%x > x/2) {
wingMove += birdSize/3;
} else {
wingMove -= birdSize/3;
}
draw_bird(mouseX, mouseY);
}
void draw_bird(int x, int y) {
strokeWeight(0);
//body
fill(colour);
ellipse(x,y,birdSize,birdSize);
//eyes
fill(#675143);
ellipse(x-(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
ellipse(x+(birdSize/5), y-(birdSize/6), birdSize/6, birdSize/6);
//mouth
fill(#FFFF00);
triangle(x-(birdSize/4), y+(birdSize/6), x+(birdSize/4), y+(birdSize/6), x, y+birdSize/3);
//wings
strokeWeight(10);
stroke(colour);
line(x-(birdSize/2), y, x-birdSize, wingMove);
line(x+(birdSize/2), y, x+birdSize, wingMove);
}
void keyPressed() {
colour = color(random(0, 225), random(0, 225), random(0, 225));
}
วันอาทิตย์ที่ 6 กันยายน พ.ศ. 2558
Lab 3 : Delivery charge
// letter(oz)-->(1) || box(pound)-->(2)
int packaging = 2;
// Next Day Priority(1) || Next Day Standard(2) || 2-Day(3)
int service = 3;
// Weight
float weight = 3 ;
float charge;
void setup() {
size(300, 300);
}
void draw() {
background(255);
fill(0);
textSize(25);
text("DELIVERY SERVICE", 45, 20);
textSize(15);
text("Types of packaging", 10, 60);
text("letter || box", 10, 80);
text("Types of service", 10, 120);
textSize(12);
text("Next Day Priority || Next Day Standard || 2-Day", 10, 140);
//call funftion
textSize(20);
calculate_charge();
text("CHARGE : $ "+charge, 20, 260);
}
void calculate_charge() {
// *****letter***** //
if (packaging == 1) {
text("PACKAGING : letter", 20, 180);
text("WEIGHT : "+weight+" oz", 20, 240);
// *****Next Day Priority***** //
if (service == 1) {
text("SERVICE : Next Day Priority", 20, 200);
if (weight >= 8) {
charge = 12;
} else {
charge = 0;
}
}
// *****Next Day Standard***** //
if (service == 2) {
text("SERVICE : Next Day Standard", 20, 200);
if (weight >= 8) {
charge = 10.5;
} else {
charge = 0;
}
}
// *****2-Day***** //
if (service == 3) {
text("SERVICE : 2-Day", 20, 200);
text("Not available", 20, 290);
}
}
// *****box***** //
if (packaging == 2) {
text("PACKAGING : box", 20, 180);
text("WEIGHT : "+weight+" pound", 20, 240);
// *****Next Day Priority***** //
if (service == 1) {
text("SERVICE : Next Day Priority", 20, 200);
if (weight == 1) {
charge = 15.75;
} else if (weight>=2) {
charge = 15.75+((weight-1)*1.25);
} else {
charge = 0;
}
}
// *****Next Day Standard***** //
if (service == 2) {
text("SERVICE : Next Day Standard", 20, 200);
if (weight == 1) {
charge = 13.75;
} else if (weight>=2) {
charge = 13.75+((weight-1)*1);
} else {
charge = 0;
}
}
// *****2-Day***** //
if (service == 3) {
text("SERVICE : 2-Day", 20, 200);
if (weight == 1) {
charge = 7;
} else if (weight>=2) {
charge = 7+((weight-1)*0.5);
} else {
charge = 0;
}
}
}
}
int packaging = 2;
// Next Day Priority(1) || Next Day Standard(2) || 2-Day(3)
int service = 3;
// Weight
float weight = 3 ;
float charge;
void setup() {
size(300, 300);
}
void draw() {
background(255);
fill(0);
textSize(25);
text("DELIVERY SERVICE", 45, 20);
textSize(15);
text("Types of packaging", 10, 60);
text("letter || box", 10, 80);
text("Types of service", 10, 120);
textSize(12);
text("Next Day Priority || Next Day Standard || 2-Day", 10, 140);
//call funftion
textSize(20);
calculate_charge();
text("CHARGE : $ "+charge, 20, 260);
}
void calculate_charge() {
// *****letter***** //
if (packaging == 1) {
text("PACKAGING : letter", 20, 180);
text("WEIGHT : "+weight+" oz", 20, 240);
// *****Next Day Priority***** //
if (service == 1) {
text("SERVICE : Next Day Priority", 20, 200);
if (weight >= 8) {
charge = 12;
} else {
charge = 0;
}
}
// *****Next Day Standard***** //
if (service == 2) {
text("SERVICE : Next Day Standard", 20, 200);
if (weight >= 8) {
charge = 10.5;
} else {
charge = 0;
}
}
// *****2-Day***** //
if (service == 3) {
text("SERVICE : 2-Day", 20, 200);
text("Not available", 20, 290);
}
}
// *****box***** //
if (packaging == 2) {
text("PACKAGING : box", 20, 180);
text("WEIGHT : "+weight+" pound", 20, 240);
// *****Next Day Priority***** //
if (service == 1) {
text("SERVICE : Next Day Priority", 20, 200);
if (weight == 1) {
charge = 15.75;
} else if (weight>=2) {
charge = 15.75+((weight-1)*1.25);
} else {
charge = 0;
}
}
// *****Next Day Standard***** //
if (service == 2) {
text("SERVICE : Next Day Standard", 20, 200);
if (weight == 1) {
charge = 13.75;
} else if (weight>=2) {
charge = 13.75+((weight-1)*1);
} else {
charge = 0;
}
}
// *****2-Day***** //
if (service == 3) {
text("SERVICE : 2-Day", 20, 200);
if (weight == 1) {
charge = 7;
} else if (weight>=2) {
charge = 7+((weight-1)*0.5);
} else {
charge = 0;
}
}
}
}
lab 3 : Leap years
int year = 1995;
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(1);
fill(0);
textSize(30);
text("Leap Year Calculator", 0, 50);
year=year+1;
calculate_year();
textSize(50);
text("Year : "+year, 5, 150);
}
void calculate_year () {
int mod4 = year%4;
int mod100 = year%100;
int mod400 = year%400;
textSize(35);
if (mod4 == 0) {
if (mod100 != 0) {
fill(84, 129, 230);
text("Leap Year", 60, 200);
} else if (mod100 == 0) {
if (mod400 != 0) {
fill(241, 95, 116);
text("Not A Leap Year", 10, 200);
} else if (mod400 == 0) {
fill(84, 129, 230);
text("Leap Year", 60, 200);
}
}
} else if (mod4 != 0) {
fill(241, 95, 116);
text("Not A Leap Year", 10, 200);
}
}
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(1);
fill(0);
textSize(30);
text("Leap Year Calculator", 0, 50);
year=year+1;
calculate_year();
textSize(50);
text("Year : "+year, 5, 150);
}
void calculate_year () {
int mod4 = year%4;
int mod100 = year%100;
int mod400 = year%400;
textSize(35);
if (mod4 == 0) {
if (mod100 != 0) {
fill(84, 129, 230);
text("Leap Year", 60, 200);
} else if (mod100 == 0) {
if (mod400 != 0) {
fill(241, 95, 116);
text("Not A Leap Year", 10, 200);
} else if (mod400 == 0) {
fill(84, 129, 230);
text("Leap Year", 60, 200);
}
}
} else if (mod4 != 0) {
fill(241, 95, 116);
text("Not A Leap Year", 10, 200);
}
}
Lab 3 : Power of 10
int power;
String number;
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(1);
power_of_ten(5, 100);
display_message(5, 200);
}
void power_of_ten(int x, int y) {
fill(0);
textSize(35);
text("Power of 10 : "+power, x, y);
}
void display_message(int x, int y) {
fill(0);
textSize(18);
text("Number : "+number, x, y);
power=power+1;
if (power==6) {
number="Million";
} else if (power==9) {
number="Billion";
} else if (power==12) {
number="Thillion";
} else if (power==15) {
number="Quadrillion";
} else if (power==18) {
number="Quintillion";
} else if (power==21) {
number="Sextillion";
} else if (power==30) {
number="Nonillion";
} else if (power==100) {
number="Googol";
} else {
number="No corresponding word";
}
}
String number;
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(1);
power_of_ten(5, 100);
display_message(5, 200);
}
void power_of_ten(int x, int y) {
fill(0);
textSize(35);
text("Power of 10 : "+power, x, y);
}
void display_message(int x, int y) {
fill(0);
textSize(18);
text("Number : "+number, x, y);
power=power+1;
if (power==6) {
number="Million";
} else if (power==9) {
number="Billion";
} else if (power==12) {
number="Thillion";
} else if (power==15) {
number="Quadrillion";
} else if (power==18) {
number="Quintillion";
} else if (power==21) {
number="Sextillion";
} else if (power==30) {
number="Nonillion";
} else if (power==100) {
number="Googol";
} else {
number="No corresponding word";
}
}
Lab 3 : Battery charged
int scale;
int energy=1;
int colour=0;
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(10);
//call function
draw_battery(65, 80);
draw_positive(245, 95);
draw_minus(5, 110);
draw_energy();
percent_battery(100, 250);
}
void draw_battery(int x, int y) {
int batW = 160;
int batH = batW/2;
//battery
fill(255);
rect(x, y, batW, batH);
//battery's polar
fill(0);
rect(batW+x, (batH/3)+y, 10, batH/3);
//energy
fill(colour);
rect(x, y, scale, batH);
}
void draw_positive(int x, int y) {
float sizePosi = 50;
//positive
fill(0);
//vertical
rect(x+(sizePosi/3), y, sizePosi/3, sizePosi);
//horizon
rect(x, y+(sizePosi/3), sizePosi, sizePosi/3);
}
void draw_minus(int x, int y ) {
int sizeMinus = 50;
fill(0);
rect(x, y, sizeMinus, sizeMinus/3);
}
void text_full(int x, int y) {
fill(#00FF00);
textSize(30);
text("FULL BATTERY", x, y);
}
void text_low(int x, int y) {
fill(#DD0000);
textSize(30);
text("LOW BATTERY", x, y);
}
void draw_energy () {
//energy
scale = scale+energy;
if (scale==160) {
energy = 0;
energy = energy - 1;
text_full(60, 40);
}
if (scale == 0 && energy < 0) {
energy = 0;
energy = energy + 1;
text_low(60, 40);
}
}
void percent_battery(int x, int y) {
//percent battery
int perBat = (scale*100)/160;
if (perBat >= 15) {
colour = #00FF00;
} else if (perBat <= 5) {
colour = #DD0000;
}
fill(0);
textSize(50);
text((int)(perBat)+"%", x, y);
}
int energy=1;
int colour=0;
void setup() {
size(300, 300);
}
void draw() {
background(255);
frameRate(10);
//call function
draw_battery(65, 80);
draw_positive(245, 95);
draw_minus(5, 110);
draw_energy();
percent_battery(100, 250);
}
void draw_battery(int x, int y) {
int batW = 160;
int batH = batW/2;
//battery
fill(255);
rect(x, y, batW, batH);
//battery's polar
fill(0);
rect(batW+x, (batH/3)+y, 10, batH/3);
//energy
fill(colour);
rect(x, y, scale, batH);
}
void draw_positive(int x, int y) {
float sizePosi = 50;
//positive
fill(0);
//vertical
rect(x+(sizePosi/3), y, sizePosi/3, sizePosi);
//horizon
rect(x, y+(sizePosi/3), sizePosi, sizePosi/3);
}
void draw_minus(int x, int y ) {
int sizeMinus = 50;
fill(0);
rect(x, y, sizeMinus, sizeMinus/3);
}
void text_full(int x, int y) {
fill(#00FF00);
textSize(30);
text("FULL BATTERY", x, y);
}
void text_low(int x, int y) {
fill(#DD0000);
textSize(30);
text("LOW BATTERY", x, y);
}
void draw_energy () {
//energy
scale = scale+energy;
if (scale==160) {
energy = 0;
energy = energy - 1;
text_full(60, 40);
}
if (scale == 0 && energy < 0) {
energy = 0;
energy = energy + 1;
text_low(60, 40);
}
}
void percent_battery(int x, int y) {
//percent battery
int perBat = (scale*100)/160;
if (perBat >= 15) {
colour = #00FF00;
} else if (perBat <= 5) {
colour = #DD0000;
}
fill(0);
textSize(50);
text((int)(perBat)+"%", x, y);
}
สมัครสมาชิก:
ความคิดเห็น (Atom)


