import controlP5.*;
ControlP5 cp5;
IntList xarr;
IntList yarr;
IntList vxArr;
IntList vyArr;
int cnt=0;
boolean isFinished = false;
int cursor = 0;
boolean drawVelocityEnabled = false;
void setup() {
size(800, 400);
cp5 = new ControlP5(this);
setupButton(cp5);
background(255);
xarr = new IntList();
yarr = new IntList();
rect(50, 50, 300, 300);
}
void setupButton(ControlP5 cp5) {
cp5.addButton("Finish")
.setPosition(10, 10)
.setSize(100, 19)
;
cp5.addButton("Calculate")
.setPosition(150, 10)
.setSize(100, 19)
;
cp5.addButton("Reset")
.setPosition(300, 10)
.setSize(100, 19)
;
}
public void Finish() {
isFinished = true;
int xLastIdx = xarr.size()-1;
int xLastElem = xarr.get(xLastIdx);
int xLastLastElem = xarr.get(xLastIdx-1);
int yLastIdx = yarr.size()-1;
int yLastElem = yarr.get(yLastIdx);
int yLastLastElem = yarr.get(yLastIdx-1);
xarr.remove(xLastIdx);
yarr.remove(yLastIdx);
while (true) {
if (xLastElem==xLastLastElem && yLastElem==yLastLastElem) {
xLastIdx = xLastIdx-1;
yLastIdx = yLastIdx-1;
xarr.remove(xLastIdx);
yarr.remove(yLastIdx);
xLastElem = xLastLastElem;
xLastLastElem = xarr.get(xLastIdx-1);
yLastElem = yLastLastElem;
yLastLastElem = yarr.get(yLastIdx-1);
} else {
break;
}
}
plot();
}
public void Calculate() {
int vx;
int vy;
vxArr = new IntList();
vyArr = new IntList();
for (int i=0; i<xarr.size()-2; i++) {
vx = xarr.get(i+1)-xarr.get(i);
vy = yarr.get(i+1)-yarr.get(i);
vxArr.append(vx);
vyArr.append(vy);
}
drawVelocityEnabled = true;
}
public void Reset(){
xarr = new IntList();
yarr = new IntList();
vxArr = new IntList();
vyArr = new IntList();
isFinished = false;
cursor = 0;
plot();
}
void drawArrow(int cx, int cy, int len, float angle) {
plot();
pushMatrix();
translate(cx, cy);
rotate(radians(angle));
line(0, 0, len, 0);
line(len, 0, len - 8, -8);
line(len, 0, len - 8, 8);
popMatrix();
}
int rad = 0;
void draw() {
if (!isFinished && mousePressed) {
xarr.append(mouseX);
yarr.append(mouseY);
plot();
}
if (drawVelocityEnabled) {
drawVelocityArrow();
}
}
void drawVelocityArrow() {
int cx = 600;
int cy = 200;
int vx = vxArr.get(cursor);
int vy = vyArr.get(cursor);
int len = int(sqrt(vx*vx+vy*vy));
float angle = degrees(atan2(vy, vx));
print("vector: ");
print(len);
print(", ");
println(angle);
drawArrow(cx, cy, len*5, angle);
if (cursor==vxArr.size()-1) {
drawVelocityEnabled = false;
} else {
cursor++;
}
delay(10);
}
void plot() {
background(255);
rect(50, 50, 300, 300);
for (int i=0; i<xarr.size()-1; i++) {
point(xarr.get(i), yarr.get(i));
line(xarr.get(i), yarr.get(i),
xarr.get(i+1), yarr.get(i+1));
}
}
View post on imgur.com
imgur.com