Graph Aray
void setup(){size(400,300);
}
int[]x = {70,120,170,220,270};
int[]y = {120,90,150,100,170};
float total = 0;
void draw(){
background(142,226,255);
strokeWeight(5);
line(30,30,30,252);
line(30,252,350,252);
strokeWeight(1);
draw_graph();
fill(0);
text('0',20,270);
stroke(255,0,0);
strokeWeight(2);
line(250,280,270,280);
text("Average line",280,280);
average();
text("average ",320,50);
text("is",340,70);
text(total,310,90);
stroke(0);
fill(255,0,0);
rect(50,270,10,10);
text("Max",70,280);
fill(0,0,255);
rect(150,270,10,10);
text("Min",170,280);
}
void draw_graph(){
for(int i = 0; i<x.length; i++){
fill(0);
text(y[i],x[i],height-(y[i]+60));
textSize(15);
if(mousePressed && (mouseButton == LEFT) && mouseX>x[i] && mouseX<x[i]+20){
y[i] = 250-mouseY ;
}
if(y[i]==max(y)){
fill(255,0,0);
text(y[i],x[i],height-(y[i]+60));
}
else if(y[i]==min(y)){
fill(0,0,255);
text(y[i],x[i],height-(y[i]+60));
}
else{fill(200);}
rect(x[i],250,20,-y[i]);
}
}
void average(){
for(int j = 0; j < y.length; j++){
total = total +y[j] ;
}
total = total/5;
line(30,height-total,350,height-total);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น