学生の作る Processing のプログラムを実行しよう、ということで。
Processing から、 processing-2.2.1-macosx.zip を入手して、 zip を解くと Processing.app が出来る。 それをアプリケーション・フォルダーに移動する。
起動して、Processing → Preference の Editor and Console font で、 Menlo を選択し、 ``Enable complex text input (i.e. Japanese, requires restart of Processing)'' にチェックを入れる。
現象数理学科の学生は、 中村先生の プログラミング演習I で (Processing を使って) プログラミングを学ぶ。
動作チェック用に一つ。
float pi = 4.0 * atan(1.0);
// 5ピクセル分のマージンをつけた500x500のウィンドウ
int XM = 5, YM = 5, rWidth = 500, rHeight = 500;
// [xleft,xright]×[ybottom,ytop] をウィンドウにはめ込む
float xleft = -0.2, xright = 2 * pi + 0.2, ytop = 1.2, ybottom = -1.2;
// 座標変換
float rx, ry;
float scx(float x) {
return XM + rx * (x - xleft);
}
float scy(float y) {
return (YM + rHeight) - ry * (y - ybottom);
}
// 端点をワールド座標で指定して線分を描く
void myline(float x1, float y1, float x2, float y2) {
line(scx(x1), scy(y1), scx(x2), scy(y2));
}
int n=100;
float dx;
float x[] = new float[n+1], y[] = new float[n+1];
void changeWindowSize(int w, int h) {
frame.setSize(w + frame.getInsets().left + frame.getInsets().right,
h + frame.getInsets().top + frame.getInsets().bottom);
size(w, h);
}
void setup() {
int i;
size(500,500); // とりあえず
rx = rWidth / (xright - xleft);
ry = rHeight / (ytop - ybottom);
changeWindowSize(XM*2+rWidth, YM*2+rHeight);
background(255);
pi = 4.0 * atan(1.0);
dx = 2.0 * pi / n;
for (i = 0; i <= n; i++) {
x[i] = i * dx;
y[i] = sin(x[i]);
}
}
void draw() {
int i;
for (i = 0; i < n; i++) {
myline(x[i], y[i], x[i+1], y[i+1]);
}
noLoop();
saveFrame("sin.png");
}
|
桂田 祐史