2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

processing > 数値をグラフプロットしてみる / 折れ線グラフを描く

Last updated at Posted at 2016-07-13
動作確認
Processing 3.1.1
Windows 8.1 pro (64bit)

数値をプロット

参考 http://yoppa.org/tau_bmaw13/4790.html

map()とellipse()を使って数値をプロットしてみる。

void setup()
{
  size(800, 300);
  frameRate(2);
  background(255);
}

float vals[] = { 
3, 1, 4, 1, 5,
9, 2, 6, 5, 3, 
5, 8, 9, 7, 9, 
3, 2, 3, 8, 4, 
6, 2, 6 
};

int pos=0;

void draw()
{
   fill(0);
   if (pos < 23) {
     float tx = map(pos, 0, 23, 0, width);
     float ty = map(vals[pos], 0, 10, height, 0);
     ellipse(tx, ty, 4, 4);
     pos++;
   }
}

frameRate(2)としているため、0.5秒ごとにプロットが増える。

qiita.png

上記はopenprocessingでも動く。
http://www.openprocessing.org/sketch/377113

折れ線グラフ

  • line()を使う
  • 前の値から今の値まで線を引く
void setup()
{
  size(800, 300);
  frameRate(2);
  background(255);
}

float vals[] = { 
3, 1, 4, 1, 5,
9, 2, 6, 5, 3, 
5, 8, 9, 7, 9, 
3, 2, 3, 8, 4, 
6, 2, 6 
};

int pos=0;

void draw()
{
   if (pos == 0) { // skip 1st
     pos++;
   }
  
   fill(0);
   if (pos < 23) {
     float stx = map(pos-1, 0, 23, 0, width);
     float sty = map(vals[pos-1], 0, 10, height, 0);
     float etx = map(pos, 0, 23, 0, width);
     float ety = map(vals[pos], 0, 10, height, 0);
     line(stx, sty, etx, ety);
     pos++;
   }
}

qiita.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?