平移水平倒置的四边形

时间:2019-01-09 16:56:33

标签: rotation processing translate-animation

几天前,我向question询问了Processing中的平移和旋转情况。

我想:

  • 多次平移,反转和旋转单个四边形(PShape对象)
  • 然后更改其2个顶部顶点之一的高度

所以整个东西就像一根可弯曲的手臂,可以向右或向左弯曲。

enter image description here

感谢@ Rabbid76的帮助,我能够实现此效果,但是现在我在翻译最后5个水平倒置的四边形时遇到另一个问题。

enter image description here

弯曲对象时,前3个四边形与后5个和分开。而且弯曲的腿弯曲得越多,它们分开得越远。

如果有人能帮助我修复翻译部分(从65行到68行),以便使四边形彼此保持连接,而对弯曲的强度有多大,我将不胜感激。

enter image description here

对此事的任何建议也将不胜感激。

脚本

int W = 40;
int H = 40;
int nQuads = 8;
int xOffset = 27;

float[] p0 = {-W/2 + xOffset, -H/2};
float[] p1 = {-W/2,  H/2};
float[] p2 = {W/2, H/2};
float[] p3 = {W/2, -H/2};

PShape object;


void setup(){
    size(600, 600, P2D);
    smooth(8);
}

void draw(){

    background(255);

    // Bending to the left
    float bending = sin(frameCount*.05) * .1;
    p0[1] -= bending;

    pushMatrix();
    translate(width/2, height/2);


    float minX = min( min(p0[0], p3[0]), min(p2[0], p1[0]) );
    float maxX = max( max(p0[0], p3[0]), max(p2[0], p1[0]) );

    float cptX = (minX+maxX)/2;

    //Rotation Angle
    float angle = atan2(p3[1]-p0[1], p3[0]-p0[0]);

    //Pivot Height
    float PH = p0[1] + (p3[1]-p0[1]) * (cptX-p0[0])/(p3[0]-p0[0]);


    for (int i = 0; i < nQuads; i++){

        float PivotHeight  = (i % 2 == 1) ? PH : H/2; 

        //Height translation
        if (i > 0){
          translate(0, PivotHeight);
        }

        //Rotate once every 2 quads
        if (i%2 == 1){
          rotate(angle*2);
        }

        //Height translation
        //Flip all quads except 1st one
        if (i > 0){
          translate(0, PivotHeight);
          scale(1, -1);
        }

         //NOT working --> Flipping horizontally the last 5 top QUADS
         if (i == 3){
           scale(-1, 1);
           translate(- xOffset, 0); //trying to align the quads on the X axis. Y translation is missing
           rotate(-angle*2);
         }

        object();
    }
    popMatrix();
}

void object() {

    beginShape(QUADS);
    vertex(p0[0], p0[1]);
    vertex(p1[0], p1[1]);
    vertex(p2[0], p2[1]);
    vertex(p3[0], p3[1]);
    endShape();
}

1 个答案:

答案 0 :(得分:1)

仅提供针对我自己问题的解决方法,但由于我不太了解自己在做什么,因此可能不会将其作为有效答案,这可能不是最有效的解决方案。

int W = 40;
int H = 40;
int nQuads = 8;
int xOffset = 27;

float[] p0 = {-W/2 + xOffset, -H/2};
float[] p1 = {-W/2,  H/2};
float[] p2 = {W/2, H/2};
float[] p3 = {W/2, -H/2};

PShape object;


void setup(){
    size(600, 600, P2D);
    smooth(8);
}

void draw(){

    background(255);

    // Bending to the left
    float bending = sin(frameCount*.05) * .3;
    p0[1] -= bending;

    pushMatrix();
    translate(width/2, height/2);


    float minX = min( min(p0[0], p3[0]), min(p2[0], p1[0]) );
    float maxX = max( max(p0[0], p3[0]), max(p2[0], p1[0]) );

    float cptX = (minX+maxX)/2;

    //Rotation Angle
    float angle = atan2(p3[1]-p0[1], p3[0]-p0[0]);

    //Pivot Height
    float PH = p0[1] + (p3[1]-p0[1]) * (cptX-p0[0])/(p3[0]-p0[0]);


    for (int i = 0; i < nQuads; i++){

         float PivotHeight  = (i % 2 == 1) ? PH : H/2; 

        //Height translation
        if (i > 0){
          translate(0, PivotHeight);
        }

        //Rotate once every 2 quads
        if (i%2 == 1){
          rotate(angle*2);
        }

        //Height translation
        //Flip all quads except 1st one
        if (i > 0){
          translate(0, PivotHeight);
          scale(1, -1);
       }

         //Flipping horizontally the last 5 top QUADS
         if (i == 3){
           scale(-1, 1);
           translate(0, PivotHeight); 
           rotate(-angle*2);
           translate(0, PivotHeight);
           translate(-xOffset , H/2 - p0[1]);
         }

        object();
    }
    popMatrix();
}

void object() {

    beginShape(QUADS);
    vertex(p0[0], p0[1]);
    vertex(p1[0], p1[1]);
    vertex(p2[0], p2[1]);
    vertex(p3[0], p3[1]);
    endShape();
}

enter image description here