Loading...
Logo
Processing Forum
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?

Copy code
  1. float centerX, centerY;

  2. ArrayList<Tangible> tangibles = new ArrayList<Tangible>();

  3. void setup() {
  4.   size(700, 700); 
  5.   smooth();

  6.   centerX = width/2;
  7.   centerY = height/2;
  8. }

  9. // . . . . . . . . . . . . . . . . . . .


  10. void draw() {
  11.   background(0);

  12.   fill(255);
  13.   noStroke();
  14.   ellipse(centerX, centerY, 10, 10);

  15.   for (int i = 0; i < tangibles.size(); i++) {
  16.     Tangible t = tangibles.get(i);
  17.     ellipse(t.centerX, t.centerY, 15, 15);
  18.   }

  19.  // simpleDraw();
  20.  // advancedDraw();
  21.     advancedDraw2();

  22. }

  23. // . . . . . . . . . . . . . . . . . . .

  24. void advancedDraw() {
  25.   stroke(255);
  26.   if (tangibles.size() >= 2) {
  27.     Tangible t1 = null;
  28.     Tangible t2 = null;
  29.     for (int i = 0; i < tangibles.size()-1; i++) {
  30.       t1 = tangibles.get(i);
  31.       t2 = tangibles.get(i+1);
  32.       //line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
  33.       // radius
  34.       float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
  35.       float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);
  36.       // steps can be based on difference in angle and radius
  37.       // for now use a fixed value
  38.       //float angleDifference = t2.angle - t1.angle;
  39.       
  40.       int steps = (int) max(r1, r2);

  41.       
  42.       for(int s = 0; s < steps; s++) {
  43.         float t = norm(s, 0, steps);
  44.         float r = map(t, 0, 1, r1, r2);
  45.         float a = map(t, 0, 1, t1.angle, t2.angle);
  46.         float x = cos(a) * r;
  47.         float y = sin(a) * r;
  48.         ellipse(centerX+x, centerY+y, 3, 3);
  49.         //println(y);
  50.            
  51.       }
  52.       
  53.     }
  54.     // connect first to last
  55.     t1 = tangibles.get(0);
  56.     //line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
  57.   }
  58. }


  59. // . . . . . . . . . . . . . . . . . . .


  60. void advancedDraw2() {
  61.   stroke(255, 200);
  62.   println("----");
  63.   if (tangibles.size() >= 2) {
  64.     Tangible t1 = null;
  65.     Tangible t2 = null;
  66.     for (int i = 0; i < tangibles.size(); i++) {
  67.       
  68.       if(i == tangibles.size()-1) {
  69.        println("help!"); 
  70.        t1 = tangibles.get(0);
  71.        t2 = tangibles.get(i);
  72.        // disable for now
  73.        return;
  74.       }
  75.       else {
  76.         t1 = tangibles.get(i);
  77.         t2 = tangibles.get(i+1);
  78.       }


  79.       // radius
  80.       float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
  81.       float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);
  82.       
  83.       float rDifference = r2 - r1;
  84.       
  85.       float aDifference = t2.angle - t1.angle;

  86.       // the larger the radius the more steps we use
  87.       int steps = (int) max(r1, r2);
  88.       // steps /= 2;
  89.       
  90.       //println("steps: "+steps);
  91.       
  92.       // previous
  93.       float px = cos(t1.angle) * r1;
  94.       float py = sin(t1.angle) * r1;
  95.       
  96.       
  97.       for(int s = 0; s <= steps; s++) {
  98.         float t = norm(s, 0, steps);
  99.         //float r = map(t, 0, 1, r1, r2);
  100.         float r = r1 + rDifference * t;
  101.         //float a = map(t, 0, 1, t1.angle, t2.angle);
  102.         float a = t1.angle + aDifference * t;
  103.         float x = cos(a) * r;
  104.         float y = sin(a) * r;

  105.         line(px + centerX, py + centerY, x + centerX, y + centerY);
  106.         px = x;
  107.         py = y;           
  108.       }
  109.       
  110.     }
  111.   }
  112. }


  113. // . . . . . . . . . . . . . . . . . . .


  114. void simpleDraw() {
  115.   stroke(255);
  116.   if (tangibles.size() >= 2) {
  117.     Tangible t1 = null;
  118.     Tangible t2 = null;
  119.     for (int i = 0; i < tangibles.size()-1; i++) {
  120.       t1 = tangibles.get(i);
  121.       t2 = tangibles.get(i+1);
  122.       line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
  123.     }
  124.     // connect first to last
  125.     t1 = tangibles.get(0);
  126.     line(t1.centerX, t1.centerY, t2.centerX, t2.centerY);
  127.   }
  128. }

  129. // . . . . . . . . . . . . . . . . . . .


  130. boolean mouseDragged;

  131. void mouseDragged() {

  132.   mouseDragged = true;

  133.   Tangible closest = closestTangible(mouseX, mouseY);
  134.   if (closest != null) {
  135.     float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
  136.     if (d < 20) {
  137.       closest.centerX = mouseX;
  138.       closest.centerY = mouseY;
  139.       closest.calculateAngle(centerX, centerY);
  140.       sortTangibles();
  141.     }
  142.   }
  143. }


  144. // . . . . . . . . . . . . . . . . . . .


  145. void mouseReleased() {

  146.   if (mouseDragged) {
  147.     mouseDragged = false;
  148.     return;
  149.   }

  150.   Tangible closest = closestTangible(mouseX, mouseY);

  151.   if (closest == null) {
  152.     Tangible t = new Tangible(mouseX, mouseY);
  153.     t.calculateAngle(centerX, centerY);
  154.     tangibles.add(t);
  155.   }
  156.   else {
  157.     float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
  158.     if (d < 20) {
  159.       tangibles.remove(closest);
  160.     }  
  161.     else {
  162.       Tangible t = new Tangible(mouseX, mouseY);
  163.       t.calculateAngle(centerX, centerY);
  164.       tangibles.add(t);
  165.     }
  166.   }

  167.   sortTangibles();
  168. }


  169. // . . . . . . . . . . . . . . . . . . .

  170. void sortTangibles() {
  171.   Collections.sort(tangibles, new TangibleComparator());
  172.   println();
  173.   for (int i = 0; i < tangibles.size(); i++) {
  174.     Tangible t = tangibles.get(i);
  175.     println(t.angle);
  176.   }
  177. }

  178. // . . . . . . . . . . . . . . . . . . .



  179. Tangible closestTangible(float x, float y) {
  180.   Tangible closest = null;
  181.   float closestDist = MAX_FLOAT;

  182.   for (int i = 0; i < tangibles.size(); i++) {
  183.     Tangible t = tangibles.get(i);
  184.     float d = dist(t.centerX, t.centerY, x, y);
  185.     if (d < closestDist) {
  186.       closestDist = d;
  187.       closest = t;
  188.     }
  189.   }

  190.   return closest;
  191. }

  192. // . . . . . . . . . . . . . . . . . . .

  193. class Tangible {

  194.   float centerX, centerY;
  195.   float angle;

  196.   Tangible(float centerX, float centerY) {
  197.     this.centerX = centerX;
  198.     this.centerY = centerY;
  199.   }

  200.   void calculateAngle(float x, float y) {
  201.     angle = atan2(centerY -y, centerX-x);
  202.     //angle = atan2(y-centerY, x-centerX);
  203.     if (angle < 0) angle += TWO_PI;
  204.   }
  205. }

  206. // . . . . . . . . . . . . . . . . . . .

  207. class TangibleComparator implements Comparator<Tangible> {

  208.   int compare(Tangible t1, Tangible t2) {
  209.     if (t1.angle > t2.angle) {
  210.       return 1;
  211.     }  
  212.     else if (t1.angle > t2.angle) {
  213.       return -1;
  214.     }
  215.     return 0;
  216.   }
  217. }

Replies(4)

I have a solution, try this :

Copy code
  1. void advancedDraw2() {
  2.   stroke(255, 200);
  3.   println("----");
  4.   if (tangibles.size() >= 2) {
  5.     Tangible t1 = null;
  6.     Tangible t2 = null;
  7.     for (int i = 0; i < tangibles.size(); i++) {
  8.       float aDifference;
  9.       if (i == tangibles.size()-1) {
  10.         println("help!"); 
  11.         t1 = tangibles.get(0);
  12.         t2 = tangibles.get(i);

  13.         aDifference = t2.angle - t1.angle - TWO_PI;
  14.       }
  15.       else {
  16.         t1 = tangibles.get(i);
  17.         t2 = tangibles.get(i+1);
  18.         aDifference = t2.angle - t1.angle;
  19.       }

  20.       // radius
  21.       float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
  22.       float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);

  23.       float rDifference = r2 - r1;

  24.       // the larger the radius the more steps we use
  25.       int steps = (int) max(r1, r2);
  26.       // steps /= 2;

  27.       //println("steps: "+steps);

  28.       // previous
  29.       float px = cos(t1.angle) * r1;
  30.       float py = sin(t1.angle) * r1;


  31.       for (int s = 0; s <= steps; s++) {
  32.         float t = norm(s, 0, steps);
  33.         //float r = map(t, 0, 1, r1, r2);
  34.         float r = r1 + rDifference * t;
  35.         //float a = map(t, 0, 1, t1.angle, t2.angle);
  36.         float a = t1.angle + aDifference * t;
  37.         float x = cos(a) * r;
  38.         float y = sin(a) * r;

  39.         line(px + centerX, py + centerY, x + centerX, y + centerY);
  40.         px = x;
  41.         py = y;
  42.       }
  43.     }
  44.   }
  45. }
I continued on the code and i have a problem in a simular fashion.
I tried multiple things inspired from the solution above but it seems a bit more tricky now.
Run the code and you will get what i mean.

here a image:

Copy code

    Copy code
    1. float centerX, centerY;

    2. ArrayList<Tangible> tangibles = new ArrayList<Tangible>();

    3. void setup() {
    4.   size(700, 700); 
    5.   smooth();
    6.   frameRate(60);

    7.   centerX = width/2;
    8.   centerY = height/2;
    9. }

    10. // . . . . . . . . . . . . . . . . . . .


    11. void draw() {
    12.   background(0);

    13.   fill(255);
    14.   noStroke();
    15.   ellipse(centerX, centerY, 10, 10);

    16.   for (int i = 0; i < tangibles.size(); i++) {
    17.     Tangible t = tangibles.get(i);
    18.     ellipse(t.centerX, t.centerY, 15, 15);
    19.   }

    20.   synthLineDraw();

    21.   float a = atan2(mouseY-centerY, mouseX - centerX);
    22.   if (a < 0) a += TWO_PI;
    23.   //println(a);
    24.   pushMatrix();
    25.   translate(centerX, centerY);
    26.   rotate(a);
    27.   line(200, 0, 0, 0);
    28.   popMatrix();

    29.   Tangible previous = previousTangibleFromAngle(a);
    30.   Tangible next = nextTangibleFromAngle(a);


    31.   if (previous != null) {
    32.     stroke(255, 0, 0);
    33.     noFill();
    34.     ellipse(previous.centerX, previous.centerY, 25, 25);

    35.     stroke(0, 255, 0);
    36.     noFill();
    37.     ellipse(next.centerX, next.centerY, 25, 25);

    38.     float t = norm(a, previous.angle, next.angle);

    39.     // radius
    40.     float r1, r2;
    41.     float rDifference;
    42.     
    43.     if(previous.angle < next.angle) {
    44.      r1 = dist(previous.centerX, previous.centerY, centerX, centerY);
    45.      r2 = dist(next.centerX, next.centerY, centerX, centerY);
    46.      rDifference = r2 - r1;
    47.     }
    48.     else {
    49.      r2 = dist(previous.centerX, previous.centerY, centerX, centerY);
    50.      r1 = dist(next.centerX, next.centerY, centerX, centerY);
    51.      rDifference = r2 - r1 - TWO_PI;
    52.     }
    53.     
    54.     
    55.     float r = r1 + rDifference * t;

    56.     pushMatrix();
    57.     translate(centerX, centerY);
    58.     rotate(a);
    59.     ellipse(r, 0, 10, 10);
    60.     popMatrix();
    61.   }




    62.   fill(255);
    63.   text(frameRate, 50, 50);
    64. }

    65. // . . . . . . . . . . . . . . . . . . .

    66. // assumes tangibles are sorted
    67. float interpolatedTangibleRadius(float a, Tangible t1, Tangible t2) {
    68.   return -1;
    69. }

    70. // . . . . . . . . . . . . . . . . . . .

    71. // assumes tangibles are sorted
    72. Tangible previousTangibleFromAngle(float a) {
    73.   if (tangibles.size() > 0) {
    74.     Tangible t = tangibles.get(0);
    75.     if (a < t.angle) {
    76.       // last one in list
    77.       return  tangibles.get(tangibles.size()-1);
    78.     }
    79.     else {

    80.       int index = tangibles.size()-1;

    81.       do {
    82.         t = tangibles.get(index);
    83.         index--;
    84.       }
    85.       while (a < t.angle);

    86.       return t;
    87.     }
    88.   }
    89.   return null;
    90. }


    91. // assumes tangibles are sorted
    92. Tangible nextTangibleFromAngle(float a) {
    93.   if (tangibles.size() > 0) {
    94.     Tangible t = tangibles.get(tangibles.size()-1);
    95.     if (a > t.angle) {
    96.       // last one in list
    97.       return  tangibles.get(0);
    98.     }
    99.     else {

    100.       int index = 0;

    101.       do {
    102.         t = tangibles.get(index);
    103.         index++;
    104.       }
    105.       while (a > t.angle);

    106.       return t;
    107.     }
    108.   }
    109.   return null;
    110. }



    111. // . . . . . . . . . . . . . . . . . . .



    112. void synthLineDraw() {
    113.   stroke(255, 200);

    114.   if (tangibles.size() >= 2) {
    115.     Tangible t1 = null;
    116.     Tangible t2 = null;
    117.     for (int i = 0; i < tangibles.size(); i++) {
    118.       float aDifference;
    119.       if (i == tangibles.size()-1) {
    120.         t1 = tangibles.get(0);
    121.         t2 = tangibles.get(i);

    122.         aDifference = t2.angle - t1.angle - TWO_PI;
    123.       }
    124.       else {
    125.         t1 = tangibles.get(i);
    126.         t2 = tangibles.get(i+1);
    127.         aDifference = t2.angle - t1.angle;
    128.       }

    129.       // radius
    130.       float r1 = dist(t1.centerX, t1.centerY, centerX, centerY);
    131.       float r2 = dist(t2.centerX, t2.centerY, centerX, centerY);

    132.       float rDifference = r2 - r1;

    133.       // the larger the radius the more steps we use
    134.       int steps = (int) max(r1, r2);
    135.       // steps /= 2;

    136.       //println("steps: "+steps);

    137.       // previous
    138.       float px = cos(t1.angle) * r1;
    139.       float py = sin(t1.angle) * r1;


    140.       for (int s = 0; s <= steps; s++) {
    141.         float t = norm(s, 0, steps);
    142.         //float r = map(t, 0, 1, r1, r2);
    143.         float r = r1 + rDifference * t;
    144.         //float a = map(t, 0, 1, t1.angle, t2.angle);
    145.         float a = t1.angle + aDifference * t;
    146.         float x = cos(a) * r;
    147.         float y = sin(a) * r;

    148.         line(px + centerX, py + centerY, x + centerX, y + centerY);
    149.         px = x;
    150.         py = y;
    151.       }
    152.     }
    153.   }
    154. }




    155. boolean mouseDragged;

    156. void mouseDragged() {

    157.   mouseDragged = true;

    158.   Tangible closest = closestTangible(mouseX, mouseY);
    159.   if (closest != null) {
    160.     float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
    161.     if (d < 40) {
    162.       closest.centerX = mouseX;
    163.       closest.centerY = mouseY;
    164.       closest.calculateAngle(centerX, centerY);
    165.       sortTangibles();
    166.     }
    167.   }
    168. }


    169. // . . . . . . . . . . . . . . . . . . .


    170. void mouseReleased() {

    171.   if (mouseDragged) {
    172.     mouseDragged = false;
    173.     return;
    174.   }

    175.   Tangible closest = closestTangible(mouseX, mouseY);

    176.   if (closest == null) {
    177.     Tangible t = new Tangible(mouseX, mouseY);
    178.     t.calculateAngle(centerX, centerY);
    179.     tangibles.add(t);
    180.   }
    181.   else {
    182.     float d = dist(mouseX, mouseY, closest.centerX, closest.centerY);
    183.     if (d < 20) {
    184.       tangibles.remove(closest);
    185.     }  
    186.     else {
    187.       Tangible t = new Tangible(mouseX, mouseY);
    188.       t.calculateAngle(centerX, centerY);
    189.       tangibles.add(t);
    190.     }
    191.   }

    192.   sortTangibles();
    193. }


    194. // . . . . . . . . . . . . . . . . . . .

    195. void sortTangibles() {
    196.   Collections.sort(tangibles, new TangibleComparator());
    197.   println();
    198.   for (int i = 0; i < tangibles.size(); i++) {
    199.     Tangible t = tangibles.get(i);
    200.     println(t.angle);
    201.   }
    202. }

    203. // . . . . . . . . . . . . . . . . . . .



    204. Tangible closestTangible(float x, float y) {
    205.   Tangible closest = null;
    206.   float closestDist = MAX_FLOAT;

    207.   for (int i = 0; i < tangibles.size(); i++) {
    208.     Tangible t = tangibles.get(i);
    209.     float d = dist(t.centerX, t.centerY, x, y);
    210.     if (d < closestDist) {
    211.       closestDist = d;
    212.       closest = t;
    213.     }
    214.   }

    215.   return closest;
    216. }

    217. // . . . . . . . . . . . . . . . . . . .

    218. class Tangible {

    219.   float centerX, centerY;
    220.   float angle;

    221.   Tangible(float centerX, float centerY) {
    222.     this.centerX = centerX;
    223.     this.centerY = centerY;
    224.   }

    225.   void calculateAngle(float x, float y) {
    226.     angle = atan2(centerY -y, centerX-x);
    227.     //angle = atan2(y-centerY, x-centerX);
    228.     if (angle < 0) angle += TWO_PI;
    229.   }
    230. }

    231. // . . . . . . . . . . . . . . . . . . .

    232. class TangibleComparator implements Comparator<Tangible> {

    233.   int compare(Tangible t1, Tangible t2) {
    234.     if (t1.angle > t2.angle) {
    235.       return 1;
    236.     }  
    237.     else if (t1.angle > t2.angle) {
    238.       return -1;
    239.     }
    240.     return 0;
    241.   }
    242. }
    Here's my solution. It feels a bit long but it works.
    I just rotate till the previous one is at TWO_PI and then i make 0 of it.

    Copy code
    1.      if(previous.angle < next.angle) {
    2.        t = norm(a, previous.angle, next.angle);
    3.      }
    4.      else {
    5.        float angleTillTwoPi = TWO_PI - previous.angle;
    6.        float pa = 0;
    7.        float na = next.angle + angleTillTwoPi;
    8.        float a2;
    9.       if(a >=  previous.angle) {
    10.         a2 = a - previous.angle;
    11.       }
    12.       else {
    13.        a2 = a + angleTillTwoPi; 
    14.       }
    15.       t = norm(a2, pa, na);
    16.      }