0

我的程序包含主要形状,即正方形、矩形、圆形,它们效果很好(对于我使用绝对值的那些形状中的 x2,y2,例如 square(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 回答