Processing入門 - 數(shù)學(xué)公式可視化

Processing簡介

Processing是一種開源編程語言,專門為電子藝術(shù)和視覺交互設(shè)計(jì)而創(chuàng)建,其目的是通過可視化的方式輔助編程教學(xué),并在此基礎(chǔ)之上表達(dá)數(shù)字創(chuàng)意。Processing也指Processing語言的集成開發(fā)環(huán)境。2001年,MIT媒體實(shí)驗(yàn)室的 Casey Reas 和 Benjamin Fry 發(fā)起了此計(jì)劃。

情景 Situation

It is required to develop a program that can evaluate the average speed at any time t, where average speed = ( s(t+△t) - s(t) ) / △t km/h.
In order to do this we take small values of ??? hours and let ??? approach zero. This is an example of what the program needs to do: if the time is ?? = 0.2 hours then the average speed of the car is calculated by adding time intervals, 0.01, 0.001, 0.001 h, ...etc. to the time 0.2 h and substitute the new time in equation 2 :
( s(0.2+0.01) - s(0.2) ) / 0.01 = 30 km/h
( s(0.2+0.001) - s(0.2) ) / 0.001 = 29.806 km/h
( s(0.2+0.0001) - s(0.2) ) / 0.0001 = 29.804 km/h
The solution will be reached when the difference between two consecutive results is very small.

任務(wù) Task

  1. You need to write a Processing program with the sketch size of 500 x 500.
  2. The initial screen of the program should look like (Figure 1) showing all the time
    intervals (t) at the bottom.
  3. If we click on any of these time intervals (value of t), the program should change
    the colour of that button and should show the results similar to the example in
    figure 2 below.
  4. The results should be calculated using the time interval clicked (t) and the value
    of ??? ranging from 0.1 – 0.00001.

行動 Action

打開Processing編譯器,copy以下代碼,點(diǎn)擊??start / 播放按鈕。

int[] xArr = {25,125,225,325,425};
String text = "Average speed at (△t) ";
float[] tArr = {0.1,0.01,0.001,0.0001,0.00001};         
void setup(){
  size(500,500);
  background(255);
  fill(0,0,255);
  textAlign(CENTER);
  text("Click any of these time intervals to show the results",250,50);
  for(int i=0;i<xArr.length;i++){
     rect(xArr[i],400,50,50); 
  }
  fill(255);
  for(int i=0;i<xArr.length;i++){
     text(""+tArr[i],xArr[i]+25,425); 
  }
}
void draw(){}
void mousePressed() {
  if (mouseX>=25 && mouseX<=75 && mouseY>=400 && mouseY<=450) {
      fill(255,0,0);
      rect(25,400,50,50);
      float t = tArr[0];
      text(text+t+" = "+averageSpeed(t),250,100);
      fill(255);
      text(""+t,50,425);
  }
  else if (mouseX>=125 && mouseX<=175 && mouseY>=400 && mouseY<=450) {
      fill(255,0,0);
      rect(125,400,50,50);
      float t = tArr[1];
      text(text+t+" = "+averageSpeed(t),250,150);
      fill(255);
      text(""+t,150,425);
  }
  else if (mouseX>=225 && mouseX<=275 && mouseY>=400 && mouseY<=450) {
      fill(255,0,0);
      rect(225,400,50,50);
      float t = tArr[2];
      text(text+t+" = "+averageSpeed(t),250,200);
      fill(255);
      text(""+t,250,425);
  }
  else if (mouseX>=325 && mouseX<=375 && mouseY>=400 && mouseY<=450) {
      fill(255,0,0);
      rect(325,400,50,50);
      float t = tArr[3];
      text(text+t+" = "+averageSpeed(t),250,250);
      fill(255);
      text(""+t,350,425);
  }
  else if (mouseX>=425 && mouseX<=475 && mouseY>=400 && mouseY<=450) {
      fill(255,0,0);
      rect(425,400,50,50);
      float t = tArr[4];
      text(text+t+" = "+averageSpeed(t),250,300);
      fill(255);
      text(""+t,450,425);
  }
}
float distance(float t){
   return 40*pow(t,3)+20*pow(t,2)+17*t; 
}
float s = distance(0.2);
float averageSpeed(float t){
  return (distance(0.2+t)-s)/t;
}

結(jié)果 Result

Results
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容