The Nature of Code(PDF版)からPVectorについて取り上げます。その中でもベクトルの減法について書きます。Processingでプログラムを書いて、動作を確認します。動作を確認できるところがProcessingの楽しいところです。
ベクトルの減法
ベクトルa引くベクトルb、は以下のようになります。
PVector a = new PVector(10, 10); PVector b = new PVector(2, 6); a.sub(b); println(a.x + ", " + a.y); //8, 4と出力される
ベクトルの減法の例
以下は、ベクトルの減法の参考例です。
//Vector subtraction void setup(){ size(200, 200); } void draw(){ background(255); PVector mouse = new PVector(mouseX, mouseY); PVector center = new PVector(width/2, height/2); mouse.sub(center); translate(width/2, height/2); line(0, 0, mouse.x, mouse.y); }
ベクトルmouse(マウスのx成分, マウスのy成分)からベクトルcenter(ウィンドウの中心のx成分, ウィンドウの中心のy成分)を引いています。これにより、ウィンドウの中心からマウスの位置までのベクトルが求められます。
まとめ
The Nature of Code(PDF版)からPVectorについて取り上げました。その中でもベクトルの減法について書きました。引き続き、The Nature of Code(PDF版)の内容を勉強します。