0

私のプログラムには、正方形、四角形、円などの主要な形状が含まれています (これらの形状の x2、y2 には、正方形 ( x、y、5、5) などの絶対値を使用しました) が、三角形の形状で作業する場合 、私のプログラムの三角形不具合

以下は私のプログラムモジュールのコードです、

if (vehicleStyle.getVehicleShape().equals(VehicleShape.TRIANGLE)) {
        processingVisualizer
                .fill(vehicleStyle.getColor().red,
                        vehicleStyle.getColor().green,
                        vehicleStyle.getColor().blue);
        processingVisualizer.strokeWeight(1 * vehicleSize);
        //System.out.println(x + "-" + y);

        //## to place face toward movement direction
        /*
         *          -----<|----
         *          |         |
         *         \/         /\
         *          |         |
         *          -----|>----
         */
        float x2, y2, x3, y3;
        if (x == 100) {
            System.out.println( "x==100");
            x2 = x - 5;
            y2 = y + 5;
            x3 = x + 5;
            y3 = y + 5;
            processingVisualizer.triangle(x, y, x2, y2, x3, y3);

        } else if (x == 20) {
            System.out.println( "x==20");
            x2 = x - 2;
            y2 = y - 2;
            x3 = x + 2;
            y3 = y - 2;
            processingVisualizer.triangle(x, y, x2, y2, x3, y3);
        } else if (y == 100) {
            System.out.println( "y ==100");
            x2 = x - 2;
            y2 = y - 2;
            x3 = x - 2;
            y3 = y + 2;
            processingVisualizer.triangle(x, y, x2, y2, x3, y3);
        } else if (y == 20) {
            System.out.println( "y ==20");
            x2 = x+5;//x - 2;
            y2 = y-5;
            x3 = x+5 ;//- 2;
            y3 = y+5;
            processingVisualizer.triangle(x, y, x2, y2, x3, y3);
        }
    }
    processingVisualizer.strokeWeight(1);

}
4

1 に答える 1

0

コードをテストすることはできませんが、4 つの条件が満たされた場合にのみ三角形をレンダリングしているため、「グリッチ」が発生すると思います。必要に応じて、条件に基づいて三角形の角の位置を更新する必要がありますが、位置が古くなっている場合でも、常に三角形をレンダリングする必要があります。

if (vehicleStyle.getVehicleShape().equals(VehicleShape.TRIANGLE)) {
        processingVisualizer
                .fill(vehicleStyle.getColor().red,
                        vehicleStyle.getColor().green,
                        vehicleStyle.getColor().blue);
        processingVisualizer.strokeWeight(1 * vehicleSize);
        //System.out.println(x + "-" + y);

        //## to place face toward movement direction
        /*
         *          -----<|----
         *          |         |
         *         \/         /\
         *          |         |
         *          -----|>----
         */


float x2, y2, x3, y3;
    if (x == 100) {
        System.out.println( "x==100");
        x2 = x - 5;
        y2 = y + 5;
        x3 = x + 5;
        y3 = y + 5;

    } else if (x == 20) {
        System.out.println( "x==20");
        x2 = x - 2;
        y2 = y - 2;
        x3 = x + 2;
        y3 = y - 2;
    } else if (y == 100) {
        System.out.println( "y ==100");
        x2 = x - 2;
        y2 = y - 2;
        x3 = x - 2;
        y3 = y + 2;
    } else if (y == 20) {
        System.out.println( "y ==20");
        x2 = x+5;//x - 2;
        y2 = y-5;
        x3 = x+5 ;//- 2;
        y3 = y+5;
    }
    processingVisualizer.triangle(x, y, x2, y2, x3, y3);
}
processingVisualizer.strokeWeight(1);

}

また、逆正接関数 ( atan2() )を使用して前の位置を保存すると、動きの方向を簡単に計算できます。

float angle = atan2(currentY-previousY,currentX-previousX);

簡単な例を次に示します。

float cx,cy,px,py;//current x,y, previous x,y
float len = 15;

void setup(){
  size(200,200);
  background(255);
}
void draw(){
  //update position - chase mouse with a bit of easing
  cx -= (cx - mouseX) * .035;
  cy -= (cy - mouseY) * .035;
  //find direction of movement based on the current position
  float angle = atan2(cy-py,cx-px);
  //store previous position
  px = cx;
  py = cy;
  //render
  fill(255,10);noStroke();
  rect(0,0,width,height);
  fill(127,32);stroke(0);
  pushMatrix();
  translate(cx,cy);
    pushMatrix();
    rotate(angle);
    triangle(len,0,-len,-len,-len,len);
    line(0,0,len,0);
    popMatrix();
  popMatrix();
}

矢印 - 動きの方向

于 2013-09-05T00:48:55.847 に答える