Okay, so I have done the Coding Challenge #93 by Daniel Shiffman yesterday. It is a model to draw a line created by a double pendulum swing.
Now I want to plot the result with my DIY pen plotter but I can’t for the life of me figure out how to export the entire result as an SVG. I once got it to save only a single frame or animation but now I can’t even get that to work.
Now I believe this is due to how the script uses image() and PGraphics.
The best way of generating a line would probably be to create one long single line since the pen plotter works best with that.
Would anyone have a clue how I could convert this script so that it outputs both one long curvy line and makes SVG export possible?
float r1 = 200;
float r2 = 200;
float m1 = 40;
float m2 = 40;
float a1 = PI/2;
float a2 = PI/2;
float a1_v = 0;
float a2_v = 0;
float g = 1;
float px2 = -1;
float py2 = -1;
float cx, cy;
import processing.svg.*;
boolean record;
PGraphics canvas;
void setup() {
size(900, 800);
cx = width/2;
cy = 350;
canvas = createGraphics(width, height);
canvas.beginDraw();
canvas.background(255);
canvas.endDraw();
}
void draw() {
if (record) {
//save to file with same name as the sketch + timestamp
beginRecord(SVG, getClass().getName()+day()+month()+year()+hour()+minute()+second()+".svg");
}
float num1 = -g * (2 * m1 + m2) * sin(a1);
float num2 = -m2 * g * sin(a1 - 2 * a2);
float num3 = -2 * sin(a1 - a2) * m2;
float num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1-a2);
float den = r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2));
float a1_a = (num1 + num2 + num3 * num4) / den;
num1 = 2 * sin(a1 - a2);
num2 = (a1_v * a1_v * r1 * (m1 + m2));
num3 = g * (m1 + m2) * cos(a1);
num4 = a2_v * a2_v * r2 * m2 * cos(a1-a2);
den = r2 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2));
float a2_a = (num1 * (num2 + num3 + num4)) / den;
image(canvas, 0, 0);
stroke(0);
strokeWeight(2);
translate(cx, cy);
float x1 = r1 * sin(a1);
float y1 = r1 * cos(a1);
float x2 = x1 + r2 * sin(a2);
float y2 = y1 + r2 * cos(a2);
a1_v += a1_a;
a2_v += a2_a;
a1 += a1_v;
a2 += a2_v;
canvas.beginDraw();
canvas.translate(cx, cy);
canvas.strokeWeight(1);
canvas.stroke(0);
if (frameCount >1) {
canvas.line(px2, py2, x2, y2);
}
canvas.endDraw();
px2 = x2;
py2 = y2;
//saveFrame("output/card_####.png");
//enable filesave for svg
if (record == true) {
endRecord();
record = false;
}
}
//press the 'r' key to save a frame
void keyPressed() {
if (key == 'r') {
record = true;
}
}
I know the solution probably lies in how the drawing of the image is done by
image(canvas, 0, 0);
and
canvas.beginDraw();
canvas.translate(cx, cy);
canvas.strokeWeight(1);
canvas.stroke(0);
if (frameCount >1) {
canvas.line(px2, py2, x2, y2);
}
canvas.endDraw();
But I do not understand how to create a solution.
Also canvas.line(px2, py2, x2, y2); probably has to be substituted with curveVertex(); somehow in order to create one lone curvy line.
I hope the solution is simple, it is mostly me lacking the experience and knowledge and hope someone knows what to change in order to make SVG export work.
Thanks for your time,
Marinus