用 3 个随机点制作一个圆圈并将它们连接在一起形成一个三角形

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

用 3 个带有小椭圆的随机点制作一个圆,然后用这些小椭圆制作一个三角形 elipses/points 我得到一条线或一个破损的三角形,我发现三角形上的坐标必须有 1 个不同的数字,如 triangle(500, 500, 200 ,200 ,100 , 50); 但我找不到解决问题的方法

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

按随机角度计算三角形的点数:

float angle = random(0, 360);

float x = 256*cos(angle);
float y = 256*sin(angle); 

将三角形的点存储到数组中:

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

现在可以轻松绘制三角形了:

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

完整 draw 功能:

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