Search code examples
javaprocessing

making a circle with 3 random points and jointing them togheter to make a triangle


making a circle with 3 random points with small elipses on them and making a triangle with those small elipses/points i get a line or a broken triangle, i found out that the coords on the triangle has to have 1 number that is different like triangle(500, 500, 200 ,200 ,100 , 50); but i cannot find a something that fixes that

int num = 3;
float[] numbers = new float[6];
int count = 0;

void setup(){
    size(880,880);
    translate(width/2,height/2);
    ellipse(0, 0, 512, 512);
    fill(256,100,0);
    ellipse(0, 0, 5, 5);
}

void draw(){
    float r = random(0, 256);
    float s = random(0, 256);
    fill(0,100,256);

    translate(width/2,height/2);
    if (count < 3)
    {
        ellipse(256*cos(TWO_PI/float(1) + r ),256*sin(TWO_PI/float(1) + r),10,10);
        stroke(100,256,0);
        numbers[count] = r;
        count++;
        numbers[count] = s;
    }
    else if (count == num)
    {
        beginShape();
        vertex(256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[0]));
        vertex(256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[1]));
        vertex(256*cos(TWO_PI/float(1) + numbers[4]),256*cos(TWO_PI/float(1) + numbers[5]));
        endShape(CLOSE);
        //triangle  (256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[0]),256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[1]),256*cos(TWO_PI/float(1) + numbers[2]),256*cos(TWO_PI/float(1) + numbers[2]));
        count++;
    }
}

void keyPressed() {
    count = 0;
}

Solution

  • Calculate the points by points of the triangle by an random angle:

    float angle = random(0, 360);
    
    float x = 256*cos(angle);
    float y = 256*sin(angle); 
    

    Store the points of the triangle to an array:

    numbers[count*2] = x;
    numbers[count*2+1] = y;
    count++;
    

    Now a triangle can by drawn with ease:

     triangle(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5] );
    

    Full draw function:

    void draw(){
        
        if (count == 0 )
        {
            for (int i=0; i < 3; ++i ) {
                float angle = random(0, 360);
                numbers[i*2]   = 256*cos(angle);
                numbers[i*2+1] = 256*sin(angle);
            }
            count = 3;           
        }
        
        translate(width/2,height/2);
        
        background(160);
        fill(255);
        ellipse(0, 0, 512, 512);
        fill(255,100,0);
        ellipse(0, 0, 5, 5);
        
        fill(0,100,255);
        ellipse(numbers[0], numbers[1], 10, 10);
        ellipse(numbers[2], numbers[3], 10, 10);
        ellipse(numbers[4], numbers[5], 10, 10);
        
        stroke(100,256,0);
        triangle(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5] );
    }
    

    Demo