PGraphics pg1, pg2; PVector Dot_A = new PVector(130, 300); //点Aを定義 PVector Dot_D = new PVector(390, 360); //点Dを定義 PVector Dot_B; PVector Dot_C; float theta_A = 0; //θAを初期化 float theta_B, theta_D, phi_B; float r = 15; float s; void setup() { size(500, 500); background(255); //PGraphicsを生成 pg1 = createGraphics(500, 500); pg2 = createGraphics(500, 500); } void draw() { background(255); update(100, 280, 200); //辺a=100,b=280,c=200 display(); } void update(float a, float b, float c) { //点Bの位置 float x_b = 130 + a * cos(theta_A); float y_b = 300 + a * sin(theta_A); Dot_B = new PVector(x_b, y_b); s = dist(Dot_B.x, Dot_B.y, Dot_D.x, Dot_D.y); //辺BDを計算 theta_D = asin((a * sin(theta_A)) / s); //θDを計算 phi_B = -acos((sq(b)+sq(s)-sq(c)) / (2 * b * s)); //φBを計算 theta_B = phi_B - theta_D; //θBを計算 //点Cの位置 float x_c = Dot_B.x + b * cos(theta_B); float y_c = Dot_B.y + b * sin(theta_B); Dot_C = new PVector(x_c, y_c); theta_A += 0.03; //θAを更新して回転 } void display() { //リンクを生成 stroke(0); strokeWeight(2); line(Dot_A.x, Dot_A.y, Dot_B.x, Dot_B.y); line(Dot_B.x, Dot_B.y, Dot_C.x, Dot_C.y); line(Dot_C.x, Dot_C.y, Dot_D.x, Dot_D.y); //各点を生成 noStroke(); fill(0); ellipse(Dot_A.x, Dot_A.y, r, r); ellipse(Dot_D.x, Dot_D.y, r, r); fill(209, 41, 26); ellipse(Dot_B.x, Dot_B.y, r, r); fill(0, 40, 205); ellipse(Dot_C.x, Dot_C.y, r, r); } boolean showDriver = false; boolean showFollower = false; boolean showLine_s = false; //原動節の軌跡 void Driver() { pg1.beginDraw(); pg1.noStroke(); pg1.fill(209, 41, 26); pg1.ellipse(Dot_B.x, Dot_B.y, 5, 5); pg1.endDraw(); image(pg1, 0, 0); } //従動節の軌跡 void Follower() { pg2.beginDraw(); pg2.noStroke(); pg2.fill(0, 40, 205); pg2.ellipse(Dot_C.x, Dot_C.y, 5, 5); pg2.endDraw(); image(pg2, 0, 0); } void Line_s() { stroke(0); line(Dot_B.x, Dot_B.y, Dot_D.x, Dot_D.y); } void keyPressed() { if (key == 'b') { showDriver = !showDriver; if (!showDriver) showDriver = false; } else if (key == 'c') { showFollower = !showFollower; if (!showFollower) showFollower = false; } else if (key == 's') { showLine_s = !showLine_s; if (!showLine_s) showLine_s = false; } }