Created
February 2, 2017 18:58
-
-
Save 8q/43f068a490a6b9c1041bf7a2a632efc4 to your computer and use it in GitHub Desktop.
ベジェ曲線
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ListIterator; | |
import java.util.ArrayList; | |
List<PVector> plist = new ArrayList<PVector>(); | |
void setup(){ | |
size(700, 700); | |
noFill(); | |
background(0); | |
textSize(14); | |
} | |
void draw(){ | |
} | |
void reset(){ | |
fill(0); | |
stroke(0); | |
rect(0,0, width, height); | |
noFill(); | |
} | |
void mouseReleased() { | |
plist.add(new PVector(mouseX, mouseY)); | |
reset(); | |
bezierCurve(plist, 2000); | |
} | |
void bezierCurve(List<PVector> plist, int n){ | |
stroke( #00ff00 ); | |
for(int i = 0; i < plist.size(); i++){ | |
PVector p = plist.get(i); | |
ellipse(p.x, p.y, 10,10); | |
fill(#ffffff); | |
text(i + "", p.x + 10, p.y - 10); | |
noFill(); | |
} | |
stroke( #ff0000 ); | |
for(int i = 1; i < n; i++){ | |
float t = map(i, 0, n, 0, 1); | |
calcBezierPoint(plist, t); | |
} | |
} | |
void calcBezierPoint(List<PVector> plist, float t){ | |
if(plist.size() <= 0) return; | |
else if(plist.size() == 1){ | |
PVector p = plist.get(0); | |
point(p.x, p.y); | |
return; | |
} | |
else { | |
List<PVector> newplist = new ArrayList<PVector>(); | |
ListIterator<PVector> iter = plist.listIterator(); | |
PVector p0, p1, p; | |
p0 = iter.next(); | |
while(iter.hasNext()){ | |
p1 = iter.next(); | |
p = new PVector(map(t, 0, 1, p0.x, p1.x), map(t, 0, 1, p0.y, p1.y)); | |
newplist.add(p); | |
p0 = p1; | |
} | |
calcBezierPoint(newplist, t); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment