center point for trianlge? Why does triangle need all points to move?

For a rectangle you have a center point rectMode(CENTER); Does a triangle have the same thing? & Triangle needs three points to increment for movement to occur circleX, top, rightBottom? A rectangle & ellipse only uses when point circleX.

    float circleX = 0;
    float speed = 3;
    color bg_color = color(0);
    boolean a = true;

    float top = 25;
    float rightBottom = 50;

    float speedtop = 3;
    float speedBottom = 3;

    final int CIRCLE=0;
    final int SQUARE=1;
    final int TRIANGLE=2;

    int currentShape=TRIANGLE;

    void setup() {
      size(640, 360);
    }

    void draw() {



      //DRAWING definition
      background(bg_color);
      fill(255);

      if (currentShape==CIRCLE) {
        ellipse(circleX, 180, 24, 24);
      }

      if (currentShape==SQUARE) {
        rectMode(CENTER);
        //rect just needs one point increments for motion to happen, (circleX)
        rect(circleX, 180, 24, 24);
      }
      //does triangle need three points increments for movement to happen? (circleX, top, rightBottom)
      if (currentShape==TRIANGLE) {
        triangle(circleX, 180, top, 150, rightBottom, 180);


      }



      //MOTION definition
      if (currentShape==CIRCLE) {
        circleX = circleX + speed;


        if (circleX > width) {
          speed--;
          bg_color = color(255, 0, 0);
          a = false;
        }

        if (circleX < 0) {
          speed++; 
          bg_color = color(0, 250, 0);
          a = true;
        }
      }

      if (currentShape==SQUARE) {
        circleX = circleX + speed;

        if (circleX > width) {
          speed--;
          bg_color = color(255, 0, 0);
          a = false;
        }

        if (circleX < 0) {
          speed++; 
          bg_color = color(0, 250, 0);
          a = true;
        }
      }

      if (currentShape==TRIANGLE) {
        circleX = circleX + speed;
        top = top + speedtop;
        rightBottom = rightBottom + speedBottom;

        if (circleX > width) {
          speed--;
          speedtop--;
          speedBottom--;
          bg_color = color(0, 0, 255);
          a = false;
        }

        if (circleX < 0) {
          speed++; 
          speedtop++;
          speedBottom++;
          bg_color = color(0, 250, 0);
          a = true;
        }
      }
    }




    void mouseReleased() {
      currentShape++;
      currentShape=currentShape%3;
    }
Tagged:

Answers

  • REMARK: Make sure that the variables you use in your program have good names. You are trying to reuse code by, for example, using circleX to define some feature of your circle, your rectangle and triangle. It makes sense for circles, but not for the other shapes and this practice should be discourage. Instead, try to use better names as for example: anchorX. A better practice is to use classes so you can encapsulate your shape's parameters and you will address those shapes by defined functions. A demo is below describing this approach.


    Related to your question: Processing sort of support rectangle (also squares) and ellipses (also circles) using different modes. However, when it comes to other shapes, even lines or shapes made of more vertices, you have to come up with other strategies.

    For example, in the case of a triangle, you can move all the points defining the triangle at the same time. Another approach is to draw the triangle in a PGraphics, a second graphics buffer, and then you draw the PGraphics following the options provided the imageMode. A third option is to create your own class called MyTriangle, and defined the vertices of the triangle based on a center point. Demonstration below.

    Kf

    MyTriangle tri;
    
    void setup(){
      size(400,600);
      tri=new MyTriangle(0,height,width/10);
      frameRate(4);
    }
    
    void draw(){
       background(0);
       tri.horizontalMotion(5);
       tri.draw();
    }
    
    class MyTriangle{
      float x,y,hLen;
    
      //Center point and side length
      MyTriangle(float cx, float cy,float length){
        x=cx;
        y=cy;
        hLen=legth/2;
      }
    
      void draw(){
        triangle(x-hLen, y-hLen, x+hLen, y-hLen, x, y+hLen);  
      }
    
     void horizontalMotion(float valx){
         x=x+valx;
     }
    }
    
Sign In or Register to comment.