How do I move a triangle around the corners of the screen using java?

1

I'm pretty new to java and I'm working on the project so I have to draw a triangle (which I have to call alice) which is located in the upper left corner of the screen and it has to move straight to the corner top right, then move to the lower right, then to the lower left and back to the upper left uppercase and he has to walk around the corners of the screen until I close and window.

I'm having problem with my if structures. I do not know why it leaves the starting point (upper right), goes to the upper left and then when it descends to the lower left, it does not stop at the edge of the screen, and from there I do not know if it does not continue its trajectory.

If someone can help me, I thank them.

My code looks like this:

import processing.core.PApplet;

public class Alice{
    int colour;
    float xSpeed;
    PApplet parent;
    float x1Pos;
    float y1Pos;
    float x2Pos;
    float y2Pos;
    float x3Pos;
    float y3Pos;

    public Alice(PApplet p, int colour, float x1Pos, float y1Pos, float x2Pos, float y2Pos, float x3Pos, float y3Pos, float xSpeed){
    parent = p;
        this.colour = colour;
        this.x1Pos = x1Pos;
        this.y1Pos = y1Pos;
        this.x2Pos = x2Pos;
        this.y2Pos = y2Pos;
        this.x3Pos = x3Pos;
        this.y3Pos = y3Pos;
        this.xSpeed = xSpeed;
    }

    public void display(){
        parent.rectMode(parent.CENTER);
        parent.fill(colour);
        parent.triangle(x1Pos, y1Pos, x2Pos, y2Pos, x3Pos, y3Pos);
    }

    private void goLeft(){
        this.x1Pos = this.x1Pos - this.xSpeed;
        this.x2Pos = this.x2Pos - this.xSpeed;
        this.x3Pos = this.x3Pos - this.xSpeed;
    }

    private void goRight(){
        this.x1Pos = this.x1Pos + xSpeed;
        this.x2Pos = this.x2Pos + xSpeed;
        this.x3Pos = this.x3Pos + xSpeed;
    }

    private void goUp(){
        this.y1Pos = this.y1Pos - xSpeed;
        this.y2Pos = this.y2Pos - xSpeed;
        this.y3Pos = this.y3Pos - xSpeed;
    }

    private void goDown(){
        this.y1Pos = this.y1Pos + xSpeed;
        this.y2Pos = this.y2Pos + xSpeed;
        this.y3Pos = this.y3Pos + xSpeed;
    }

    public void driveCorner(){
        if(x1Pos <= parent.width - 30){
            goRight();
        }
        if(x1Pos >= parent.width - 30){
            goDown();
        }
        if(y1Pos <= parent.height - 20){
            goLeft();
        }
        if(x1Pos <= 0){ 
         goUp();
        }
    }
    
asked by anonymous 05.02.2017 / 17:52

1 answer

1

I think that by tracing in variables on which edge the triangle is or is not, it is much easier. Also, it is best if all three vertices are treated equally in this way without it being necessary to know in advance the width or height of the triangle (in their original code they were 20 and 30).

In this way, the code looks like this:

import processing.core.PApplet;

public class Alice {
    private int colour;
    private float xSpeed;
    private PApplet parent;
    private float x1Pos;
    private float y1Pos;
    private float x2Pos;
    private float y2Pos;
    private float x3Pos;
    private float y3Pos;

    public Alice(PApplet p, int colour, float x1Pos, float y1Pos, float x2Pos, float y2Pos, float x3Pos, float y3Pos, float xSpeed) {
        parent = p;
        this.colour = colour;
        this.x1Pos = x1Pos;
        this.y1Pos = y1Pos;
        this.x2Pos = x2Pos;
        this.y2Pos = y2Pos;
        this.x3Pos = x3Pos;
        this.y3Pos = y3Pos;
        this.xSpeed = xSpeed;
    }

    public void display() {
        parent.rectMode(parent.CENTER);
        parent.fill(colour);
        parent.triangle(x1Pos, y1Pos, x2Pos, y2Pos, x3Pos, y3Pos);
    }

    private void goLeft() {
        this.x1Pos -= xSpeed;
        this.x2Pos -= xSpeed;
        this.x3Pos -= xSpeed;
    }

    private void goRight() {
        this.x1Pos += xSpeed;
        this.x2Pos += xSpeed;
        this.x3Pos += xSpeed;
    }

    private void goUp() {
        this.y1Pos -= xSpeed;
        this.y2Pos -= xSpeed;
        this.y3Pos -= xSpeed;
    }

    private void goDown() {
        this.y1Pos += xSpeed;
        this.y2Pos += xSpeed;
        this.y3Pos += xSpeed;
    }

    public void driveCorner() {
        boolean naBordaSuperior = y1Pos <= 0 || y2Pos <= 0 || y3Pos <= 0;
        boolean naBordaInferior = y1Pos >= parent.height - 1 || y2Pos >= parent.height - 1 || y3Pos >= parent.height - 1;
        boolean naBordaEsquerda = x1Pos <= 0 || x2Pos <= 0 || x3Pos <= 0;
        boolean naBordaDireita = x1Pos >= parent.width - 1 || x2Pos >= parent.width - 1 || x3Pos >= parent.width - 1;

        if (naBordaSuperior && !naBordaDireita) {
            goRight();
        } else if (naBordaDireita && !naBordaInferior) {
            goDown();
        } else if (naBordaInferior && !naBordaEsquerda) {
            goLeft();
        } else if (naBordaEsquerda && !naBordaSuperior) { 
            goUp();

        // No caso em que o triângulo está no meio e afastado de qualquer borda, vai para a direita.
        } else if (!naBordaSuperior && !naBordaInferior && !naBordaEsquerda && !naBordaDireita) {
            goRight();

        // Este caso só acontece se estiver nas quatro bordas.
        // Ou seja, ou o triângulo é muito grande, ou a tela muito pequena.
        // Portanto, não se move.
        } else {
            // ...
        }
    }
    
05.02.2017 / 18:17