IntList xarr;; IntList yarr; int cnt=0; void setup() { size(400, 400); background(255); xarr = new IntList(); yarr = new IntList(); } void draw() { if (mousePressed) { xarr.append(mouseX); yarr.append(mouseY); background(255); 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)); } } }
finishボタンつき
import controlP5.*; ControlP5 cp5; IntList xarr; IntList yarr; int cnt=0; boolean isFinished = 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) ; } 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(); println("finish"); } public void Calculate() { println("calc"); } void draw() { if (!isFinished && mousePressed) { xarr.append(mouseX); yarr.append(mouseY); plot(); } } 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)); } }