Java 根据x的值使用值时处理草图毛刺

Java 根据x的值使用值时处理草图毛刺,java,graphics,processing,Java,Graphics,Processing,我的程序包含主要形状,即正方形、矩形、圆形,它们工作良好(对于x2、y2,在这些形状中,我使用绝对值,例如正方形(x、y、5、5)),但在使用三角形形状时, 我的程序中的三角形出现故障 下面是我的程序模块代码 if (vehicleStyle.getVehicleShape().equals(VehicleShape.TRIANGLE)) { processingVisualizer .fill(vehicleStyle.getColor().re

我的程序包含主要形状,即正方形、矩形、圆形,它们工作良好(对于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);

}
if(vehicleStyle.getVehicleShape().equals(VehicleShape.TRIANGLE)){
处理可视化器
.fill(vehicleStyle.getColor().red、,
vehicleStyle.getColor()绿色,
vehicleStyle.getColor().blue);
加工可视化仪器.冲程重量(1*车辆尺寸);
//系统输出println(x+“-”+y);
//##使面朝向运动方向
/*
*          ---------
*/
浮子x2、y2、x3、y3;
如果(x==100){
System.out.println(“x==100”);
x2=x-5;
y2=y+5;
x3=x+5;
y3=y+5;
处理可视化工具。三角形(x,y,x2,y2,x3,y3);
}如果(x==20),则为else{
System.out.println(“x==20”);
x2=x-2;
y2=y-2;
x3=x+2;
y3=y-2;
处理可视化工具。三角形(x,y,x2,y2,x3,y3);
}如果(y==100),则为else{
System.out.println(“y==100”);
x2=x-2;
y2=y-2;
x3=x-2;
y3=y+2;
处理可视化工具。三角形(x,y,x2,y2,x3,y3);
}如果(y==20),则为else{
System.out.println(“y==20”);
x2=x+5;//x-2;
y2=y-5;
x3=x+5;//-2;
y3=y+5;
处理可视化工具。三角形(x,y,x2,y2,x3,y3);
}
}
处理可视化仪器。冲程重量(1);
}

无法测试您的代码,但我的假设是“故障”会发生,因为您仅在满足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);
下面是一个简单的例子:

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();
}

您所说的“毛刺”是什么意思:三角形是否未绘制或绘制错误?(在后者中,描述您看到了什么以及您希望看到什么)还有,
System.out.println()
debug命令按预期调用了吗?@MrGreen\u pk我的解决方案对您有效吗?@GeorgeProfenza谢谢,伙计,它有效了。
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();
}