Morph an ellipse in two (like a virus)
in
Programming Questions
•
3 months ago
Hi everyone,
I tried and searched the way to convert one sphere into two to make a class for a bigger project, I founded nothing and I tryed (in a very draft mode) to make the excersice with an eight point bezierVertex, it's really a draft from another script by Ira Greenberg and my results are very ugly but it's the way I try to do. I don't know if anybody can show me another point of view to continue working cause I think this one it's not the good one, almost all the operations that I made are manual :( here's the code, thanks!
- /**
- * bezierVertex() circle
- * By Ira Greenberg <br />
- * Processing for Flash Developers,
- * Friends of ED, 2009
- */
- int tiempo=0;
- int detail = 8;
- int r;
- PVector[] anchors = new PVector[detail];
- PVector[] controlsRt = new PVector[detail];
- PVector[] controlsLft = new PVector[detail];
- void setup(){
- size(800, 400);
- smooth();
- }
- void draw(){
- background(200);
- translate(width/2, height/2);
- fill(255);
- strokeWeight(3);
- rectMode(CENTER);
- calcCircle(150);
- drawCircle();
- showControls();
- frameRate(1);
- }
- void calcCircle(float radius){
- /* cubic curve internal implementation
- only requires 4 anchor PVectors for circle*/
- // spacing of anchor PVectors
- float anchorTheta = 0;
- // spacing of control PVectors
- float controlTheta = TWO_PI/detail/3;
- // distance of control PVectors
- float controlRadius = radius/cos(controlTheta);
- // for anchors
- float px = 0;
- float py = 0;
- //for controls
- float cx1 = 0;
- float cy1 = 0;
- float cx2 = 0;
- float cy2 = 0;
- for (int i=0; i<detail; i++){
- // anchor PVectors
- px = cos(anchorTheta)*radius;
- py = sin(anchorTheta)*radius;
- anchors[i] = new PVector(px, py);
- // control PVectors
- cx1 = cos(anchorTheta+controlTheta)*controlRadius;
- cy1 = sin(anchorTheta+controlTheta)*controlRadius;
- controlsLft[i] = new PVector(cx1, cy1);
- cx2 = cos(anchorTheta+controlTheta*2)*controlRadius;
- cy2 = sin(anchorTheta+controlTheta*2)*controlRadius;
- controlsRt[i] = new PVector(cx2, cy2);
- // increment theta
- anchorTheta += TWO_PI/detail;
- }
- }
- void drawCircle(){
- beginShape();
- vertex(anchors[0].x+r, anchors[0].y);
- for (int i=0; i<detail; i++){
- if (i<detail-1){ //Todas menos la última
- //Recuerda que las medidas son de los marcadores de los ángulos y por último el del punto
- if (i==0){ //derecho - inferior
- bezierVertex(controlsLft[i].x+r, controlsLft[i].y, controlsRt[i].x+r, controlsRt[i].y, anchors[i+1].x+r, anchors[i+1].y);
- }else if (i==1){ //extremo inferior
- bezierVertex(controlsLft[i].x-r, controlsLft[i].y, controlsRt[i].x, controlsRt[i].y, anchors[i+1].x, anchors[i+1].y-r);
- }else if (i==2){ //izquierdo inferior
- bezierVertex(controlsLft[i].x-r, controlsLft[i].y, controlsRt[i].x-r, controlsRt[i].y, anchors[i+1].x-r, anchors[i+1].y);
- }else if (i==3){ //Extremo izquierdo
- bezierVertex(controlsLft[i].x-r, controlsLft[i].y, controlsRt[i].x-r, controlsRt[i].y, anchors[i+1].x-r, anchors[i+1].y);
- }else if (i==4){ //izquierdo superior
- bezierVertex(controlsLft[i].x-r, controlsLft[i].y, controlsRt[i].x-r, controlsRt[i].y, anchors[i+1].x-r, anchors[i+1].y);
- }else if (i==5){ //Extremo superior
- bezierVertex(controlsLft[i].x-r, controlsLft[i].y, controlsRt[i].x, controlsRt[i].y, anchors[i+1].x, anchors[i+1].y+r);
- }else if (i==6){ //derecho superior
- bezierVertex(controlsLft[i].x, controlsLft[i].y, controlsRt[i].x, controlsRt[i].y, anchors[i+1].x+r, anchors[i+1].y);
- }
- }else{ //extremo derecho
- // close circle
- bezierVertex(controlsLft[i].x+r, controlsLft[i].y, controlsRt[i].x+r, controlsRt[i].y, anchors[0].x+r, anchors[0].y);
- }
- r++;
- }
- endShape();
- }
- void showControls(){
- strokeWeight(1);
- fill(255);
- for (int i=0; i<detail; i++){
- line(controlsLft[i].x, controlsLft[i].y, anchors[i].x, anchors[i].y);
- if (i<detail-1){
- line(controlsRt[i].x, controlsRt[i].y, anchors[i+1].x, anchors[i+1].y);
- }
- else if (i==detail-1){
- line(controlsRt[i].x, controlsRt[i].y, anchors[0].x, anchors[0].y);
- }
- }
- // ensure lines stay underneath anchors/controls
- for (int i=0; i<detail; i++){
- // anchor PVectors and control handles
- fill(220);
- ellipse(controlsLft[i].x, controlsLft[i].y, 8, 8);
- fill(360);
- ellipse(controlsRt[i].x, controlsRt[i].y, 8, 8);
- rect(anchors[i].x, anchors[i].y, 8, 8);
- }
- }
1