Skip to content

Instantly share code, notes, and snippets.

@yoggy
Last active June 9, 2016 07:16
Show Gist options
  • Save yoggy/a9f3182f3cd11aef08949fc245108bf8 to your computer and use it in GitHub Desktop.
Save yoggy/a9f3182f3cd11aef08949fc245108bf8 to your computer and use it in GitHub Desktop.
交流を流したときの電流の位相をアニメーションで簡易表示するスケッチ
//
// 交流位相.pde
//
// 交流を流したときの電流の位相をアニメーションで簡易表示するスケッチ
//
float th = 0.0;
boolean enable = true;
void setup() {
size(600, 300);
background(0);
}
void draw() {
float r = 50;
float i = 30;
if (enable) {
th += 0.05;
}
PVector vv = new PVector();
PVector vr = new PVector();
PVector vi = new PVector();
PVector vz = new PVector();
PVector vy = new PVector();
// 電圧
vv.x = 120 * cos(th); // 値の大きさは適当…
vv.y = 120 * sin(th);
// 抵抗 (実数部)
vr.x = r * cos(th);
vr.y = r * sin(th);
// インダクタンス・リアクタンス (虚数部)
vi.x = i * -sin(th);
vi.y = i * cos(th);
// インピーダンス (Z)
vz.add(vr);
vz.add(vi);
// アドミタンス (インピーダンスの逆数、Y=1/Z)
vy.add(vr); //逆数にすると小さすぎるので、とりあえず長さはそのままで…
vy.sub(vi);
noStroke();
fill(#000000);
rect(300, 0, width, height);
fill(#ffffff);
textSize(16);
text("Z = R + jωL - j(1/ωC)", 336, 20);
text("V = IR", 336, 40);
text("I = V/R = V * 1/Z", 336, 60);
strokeWeight(3);
stroke(#ff0000);
line(366, 23, 382, 23);
stroke(#00ff00);
line(398, 23, 508, 23);
stroke(#0000ff);
line(329, 23, 353, 23);
stroke(#ff00ff);
line(441, 64, 472, 64);
stroke(#ffff00);
line(333, 44, 350, 44);
stroke(#00ffff);
line(333, 64, 350, 64);
pushMatrix();
translate(0, 150);
scale(1, -1);
translate(450, 0);
stroke(#ffff00);
strokeWeight(2);
line(0, 0, vv.x, vv.y);
line(-151, vv.y, vv.x, vv.y);
strokeWeight(5);
stroke(#ff0000);
line(0, 0, vr.x, vr.y);
stroke(#00ff00);
line(0, 0, vi.x, vi.y);
stroke(#0000ff);
line(0, 0, vz.x, vz.y);
stroke(#ff00ff);
line(0, 0, vy.x, vy.y);
strokeWeight(2);
stroke(#00ffff);
line(-151, vy.y, vy.x, vy.y);
popMatrix();
PImage img = get(0, 0, width, height);
image(img, -1, 0);
}
void keyPressed() {
if (keyCode == 0x20) {
enable = !enable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment