1/** 2 * Reach 1. 3 * Based on code from Keith Peters (www.bit-101.com) 4 * 5 * The arm follows the position of the mouse by 6 * calculating the angles with atan2(). 7 */ 8 9 10float x = 100; 11float y = 100; 12float x2 = 100; 13float y2 = 100; 14float segLength = 30; 15 16void setup() { 17 size(200, 200); 18 smooth(); 19 strokeWeight(20.0); 20 stroke(0, 100); 21} 22 23void draw() { 24 background(226); 25 26 float dx = mouseX - x; 27 float dy = mouseY - y; 28 float angle1 = atan2(dy, dx); 29 30 float tx = mouseX - cos(angle1) * segLength; 31 float ty = mouseY - sin(angle1) * segLength; 32 dx = tx - x2; 33 dy = ty - y2; 34 float angle2 = atan2(dy, dx); 35 x = x2 + cos(angle2) * segLength; 36 y = y2 + sin(angle2) * segLength; 37 38 segment(x, y, angle1); 39 segment(x2, y2, angle2); 40} 41 42void segment(float x, float y, float a) { 43 pushMatrix(); 44 translate(x, y); 45 rotate(a); 46 line(0, 0, segLength, 0); 47 popMatrix(); 48} 49 50