Skip to content

Instantly share code, notes, and snippets.

@ulrichzwingli
Created December 2, 2017 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulrichzwingli/6f34d7acac683399a16a4d6b02ae2ce6 to your computer and use it in GitHub Desktop.
Save ulrichzwingli/6f34d7acac683399a16a4d6b02ae2ce6 to your computer and use it in GitHub Desktop.
FYF_Processing_code_controlling_winch_servo_and_shadow
PImage shadow;
import processing.serial.*;
PVector a = new PVector(150, 0);
PVector b = new PVector(0, 150);
float angle;
PVector v1;
PVector v2;
float q;
float len;
float root;
Serial myPort;
void setup() {
size(600, 400);
shadow = loadImage("shadow.png");
String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255);
pushMatrix();
translate(width/2, height/2);
fill(0);
line(0, 0, a.x, a.y);
line(0, 0, b.x, b.y);
b.set(mouseX-width/2, mouseY-height/2, 0);
//if (mousePressed) {
// a.set(mouseX-width/2, mouseY-height/2, 0);
// }
angle = angle(a, b);
drawArc();
//ellipse(0, 0, 50, 50);
fill(255);
text(int(degrees(angle)), 0, 0);
q = degrees(angle);
text(int(q), 50, 50);
myPort.write(int(degrees(angle))/4);
popMatrix();
translate(width/2, height/2);
rotate(radians(q+90));
len = sq(mouseX-300) + sq(mouseY-200);
root = sqrt(len);
println(root);
image(shadow, -50, 0, 100, root);
}
void drawArc() {
float angleStart = angle(new PVector(1, 0), a);
float angleEnd = angle(new PVector(1, 0), b);
//arc(0, 0, 150, 150, angleStart, angleEnd);
//if (angleEnd < angleStart) {
// arc(0, 0, 150, 150, angleStart, TWO_PI);
//arc(0, 0, 150, 150, 0, angleEnd);
}
//}
float angle(PVector v1, PVector v2) {
float a = atan2(v2.y, v2.x) - atan2(v1.y, v1.x);
if (a < 0) a += TWO_PI;
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment