如何在特定区域内绘制随机线。(处理)

时间:2015-11-03 16:03:49

标签: processing

我想知道如何在圆圈内绘制随机(正常或弯曲)线条,这意味着线条不会通过圆圈的边界。

以下是绘制圆圈的代码:

size(900, 900);
background(80, 89, 81);
int n = 50; // aantal (element count)
for (int i = 0; i <n; i++) {
  float x1 = 20*i;
  float x2 =-10+20*i;
  float x3 =x2+20;
  float x11 = 10+20*i;
  float x22 = x1;
  float x33= x22+20;
  for (int j = 0; j < n; j++) {
    float y1 = 30+30*j;
    float y2 = 30*j;
    float y3 = y2;
    float y11 = y2;
    float y22 = y1;
    float y33 = y1;
    float a = random(10, 90);
    if (j%2==0) { 
      noStroke();
      fill(0);    
      triangle(x1, y1, x2, y2, x3, y3);
      fill(255, 255, 255, a);
      triangle(x11, y11, x22, y22, x33, y33);
    } else {
      noStroke();
      fill(255, 255, 255, a);    
      triangle(x1, y1, x2, y2, x3, y3);
      fill(0);
      triangle(x11, y11, x22, y22, x33, y33);
    }
  }
}

for (int t=0; t<40; t++) {

  ellipse(450, 450, 800-20*t, 800-20*t);
  fill(0+t*2, 0+t*2, 0+t*2);
}

该代码生成此图像:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您知道圆的中心,并且您知道圆的半径,那么您可以使用基本触发来获取该圆内的点。像这样:

float circleX = 100;
float circleY = 100;
float circleR = 50;

void setup() {
  size(200, 200);
  ellipseMode(RADIUS);
}

void draw() {
  background(0);
  ellipse(circleX, circleY, circleR, circleR); 

  stroke(0);
  for (int i = 0; i < 10; i++) {
    float x1 = circleX + cos(random(2*PI)) * random(circleR);
    float y1 = circleY + sin(random(2*PI)) * random(circleR);
    float x2 = circleX + cos(random(2*PI)) * random(circleR);
    float y2 = circleY + sin(random(2*PI)) * random(circleR);

    line(x1, y1, x2, y2);
  }
}

enter image description here