connect end to start with interpolation
in
Programming Questions
•
3 months ago
I would like to connect the start to the end:
There is a center ellipse which is a bit smaller. By me it goes the wrong way around when i try to connect the first to the last.
check void advancedDraw2, it happens there.
For now i just return, disable that.
can someone help?
- float centerX, centerY;
- ArrayList<Tangible> tangibles = new ArrayList<Tangible>();
- void setup() {
- size(700, 700);
- smooth();
- centerX = width/2;
- centerY = height/2;
- }
- // . . . . . . . . . . . . . . . . . . .
- void draw() {
- background(0);
- fill(255);
- noStroke();
- ellipse(centerX, centerY, 10, 10);
- for (int i = 0; i < tangibles.size(); i++) {
- Tangible t = tangibles.get(i);
- ellipse(t.centerX, t.centerY, 15, 15);
- }
- // simpleDraw();
- // advancedDraw();
- advancedDraw2();
- }
- // . . . . . . . . . . . . . . . . . . .
- void advancedDraw() {
- stroke(255);
- if (tangibles.size() >= 2) {
- Tangible t1 = null;
- Tangible t2 = null;
- for (int i = 0; i < tangibles.size()-1; i++) {
- t1 = tangibles.get(i);
- t2 = tangibles.get(i+1);
- //line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
- // radius
- float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
- float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);
- // steps can be based on difference in angle and radius
- // for now use a fixed value
- //float angleDifference = t2.angle - t1.angle;
- int steps = (int) max(r1, r2);
- for(int s = 0; s < steps; s++) {
- float t = norm(s, 0, steps);
- float r = map(t, 0, 1, r1, r2);
- float a = map(t, 0, 1, t1.angle, t2.angle);
- float x = cos(a) * r;
- float y = sin(a) * r;
- ellipse(centerX+x, centerY+y, 3, 3);
- //println(y);
- }
- }
- // connect first to last
- t1 = tangibles.get(0);
- //line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- void advancedDraw2() {
- stroke(255, 200);
- println("----");
- if (tangibles.size() >= 2) {
- Tangible t1 = null;
- Tangible t2 = null;
- for (int i = 0; i < tangibles.size(); i++) {
- if(i == tangibles.size()-1) {
- println("help!");
- t1 = tangibles.get(0);
- t2 = tangibles.get(i);
- // disable for now
- return;
- }
- else {
- t1 = tangibles.get(i);
- t2 = tangibles.get(i+1);
- }
- // radius
- float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
- float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);
- float rDifference = r2 - r1;
- float aDifference = t2.angle - t1.angle;
- // the larger the radius the more steps we use
- int steps = (int) max(r1, r2);
- // steps /= 2;
- //println("steps: "+steps);
- // previous
- float px = cos(t1.angle) * r1;
- float py = sin(t1.angle) * r1;
- for(int s = 0; s <= steps; s++) {
- float t = norm(s, 0, steps);
- //float r = map(t, 0, 1, r1, r2);
- float r = r1 + rDifference * t;
- //float a = map(t, 0, 1, t1.angle, t2.angle);
- float a = t1.angle + aDifference * t;
- float x = cos(a) * r;
- float y = sin(a) * r;
- line(px + centerX, py + centerY, x + centerX, y + centerY);
- px = x;
- py = y;
- }
- }
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- void simpleDraw() {
- stroke(255);
- if (tangibles.size() >= 2) {
- Tangible t1 = null;
- Tangible t2 = null;
- for (int i = 0; i < tangibles.size()-1; i++) {
- t1 = tangibles.get(i);
- t2 = tangibles.get(i+1);
- line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
- }
- // connect first to last
- t1 = tangibles.get(0);
- line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- boolean mouseDragged;
- void mouseDragged() {
- mouseDragged = true;
- Tangible closest = closestTangible(mouseX, mouseY);
- if (closest != null) {
- float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
- if (d < 20) {
- closest.centerX = mouseX;
- closest.centerY = mouseY;
- closest.calculateAngle(centerX, centerY);
- sortTangibles();
- }
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- void mouseReleased() {
- if (mouseDragged) {
- mouseDragged = false;
- return;
- }
- Tangible closest = closestTangible(mouseX, mouseY);
- if (closest == null) {
- Tangible t = new Tangible(mouseX, mouseY);
- t.calculateAngle(centerX, centerY);
- tangibles.add(t);
- }
- else {
- float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
- if (d < 20) {
- tangibles.remove(closest);
- }
- else {
- Tangible t = new Tangible(mouseX, mouseY);
- t.calculateAngle(centerX, centerY);
- tangibles.add(t);
- }
- }
- sortTangibles();
- }
- // . . . . . . . . . . . . . . . . . . .
- void sortTangibles() {
- Collections.sort(tangibles, new TangibleComparator());
- println();
- for (int i = 0; i < tangibles.size(); i++) {
- Tangible t = tangibles.get(i);
- println(t.angle);
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- Tangible closestTangible(float x, float y) {
- Tangible closest = null;
- float closestDist = MAX_FLOAT;
- for (int i = 0; i < tangibles.size(); i++) {
- Tangible t = tangibles.get(i);
- float d = dist(t.centerX, t.centerY, x, y);
- if (d < closestDist) {
- closestDist = d;
- closest = t;
- }
- }
- return closest;
- }
- // . . . . . . . . . . . . . . . . . . .
- class Tangible {
- float centerX, centerY;
- float angle;
- Tangible(float centerX, float centerY) {
- this.centerX = centerX;
- this.centerY = centerY;
- }
- void calculateAngle(float x, float y) {
- angle = atan2(centerY -y, centerX-x);
- //angle = atan2(y-centerY, x-centerX);
- if (angle < 0) angle += TWO_PI;
- }
- }
- // . . . . . . . . . . . . . . . . . . .
- class TangibleComparator implements Comparator<Tangible> {
- int compare(Tangible t1, Tangible t2) {
- if (t1.angle > t2.angle) {
- return 1;
- }
- else if (t1.angle > t2.angle) {
- return -1;
- }
- return 0;
- }
- }
1