
ランダムな線
LINE-0500 ランダムな水平線を描いてみます。0〜height未満の間でランダムなY座標を生成します。
|
1 2 3 4 5 6 7 8 9 10 11 |
//=========================================== // 水平線をランダムに //=========================================== // CreationDate: 2025年2月24日 月曜日10:07:35 //=================【環境設定】================ size(400, 400); //画面サイズ //==================メイン==================== for (int i=0; i<50; i++) { float ry =random(0, height);//ランダムなY座標を生成 line(0, ry, width, ry); //直線を描画 } |

乱数を生成
| 構文 | random(high) random(low, high) | 擬似乱数を生成します。パラメータが一つの場合は0〜highまでの値を返します。2つのパラメータの場合はlow以上high未満までの値を返します。 |
| パラメータ | low | (float) 下限 |
| high | (float) 上限 | |
| 戻り値 | float | 浮動小数点値 |
LINE-0501 ランダムな垂直線も同じように0〜width未満の間で生成します。
|
1 2 3 4 5 6 7 8 9 10 11 |
//=========================================== // 垂直線をランダムに //=========================================== // CreationDate: 2025年2月24日 月曜日10:16:08 //=================【環境設定】================ size(400, 400); //画面サイズ //==================メイン==================== for (int i=0; i<50; i++) { float rx =random(0, width); line(rx, 0, rx, height); } |

LINE-0502 線幅をランダムにしてみましょう。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
//=========================================== // 水平線をランダムに02 //=========================================== // CreationDate: 2025年2月24日 月曜日10:07:35 //=================【環境設定】================ size(400, 400); //画面サイズ //==================メイン==================== for (int i=0; i<50; i++) { float ry =random(0, height);//ランダムなY座標を生成 strokeWeight(random(1,5)); line(0, ry, width, ry); //直線を描画 } |

LINE-0503 これにランダムな色を付けてみましょう。効果的に見えるよう繰り返し回数と線幅、背景色を変更しています。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//=========================================== // 水平線をランダムに03 //=========================================== // CreationDate: 2025年2月24日 月曜日10:07:35 //=================【環境設定】================ size(400, 400); //画面サイズ background(255); //==================メイン==================== for (int i=0; i<100; i++) { float ry =random(0, height);//ランダムなY座標を生成 strokeWeight(random(1,10)); stroke(random(255), random(255), random(255)); line(0, ry, width, ry); //直線を描画 } |

LINE-0504 X軸を4分割して、それぞれにランダムな線を描きます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//=========================================== // 水平線をランダムに 応用 //=========================================== // CreationDate: 2025年2月24日 月曜日10:07:35 //=================【環境設定】================ size(400, 400); //画面サイズ colorMode(HSB,100); background(50,30,80); //=================【初期設定】================ strokeCap(SQUARE); int dt=0; //==================メイン==================== for (int i=1; i<=4; i++) { for (int k=0; k<100; k++) { float ry = random(0, height); //ランダムなY座標 strokeWeight(random(1, 10));//ランダムな線幅 stroke(random(50), random(30), random(100)); //ランダムな色 line(0+dt, ry, 100+dt, ry); //線の描画 } dt=i*100;//始点と終点を移動 } |

LINE-0505 破線のようなランダムな長さの線を描きます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//=========================================== // ランダムな長さの線 //=========================================== // CreationDate: 2025年2月24日 月曜日18:13:13 //=================【環境設定】================ size(400, 400); //画面サイズ background(124,180,175); //=================【初期設定】================ int dt=0; //移動量 stroke(255); //==================メイン==================== for (int i=0; i<6; i++) { for (int y=0; y<height; y+=10) { float rx = random(-20, 30); float rx2 = random(60, 80); line(rx+dt, y, rx2+dt, y); } dt+=75; //移動する } |

LINE-0506 線幅、線の色をランダムにしてみます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//=========================================== // ランダムな長さの線 //=========================================== // CreationDate: 2025年2月24日 月曜日19:01:50 //=================【環境設定】================ size(400, 400); //画面サイズ colorMode(HSB,100); background(0); //=================【初期設定】================ int dt=0; //移動量 strokeCap(SQUARE); //==================メイン==================== for (int i=0; i<6; i++) { for (int y=0; y<height; y+=10) { strokeWeight(random(1, 5)); stroke(random(80,100), 100, 60); float rx = random(-20, 30); float rx2 = random(60, 80); line(rx+dt, y, rx2+dt, y); } dt+=75; //移動する } |

LINE-0507 Y座標もランダムにしてみます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//=========================================== // ランダムな長さの線 //=========================================== // CreationDate: 2025年2月24日 月曜日19:09:58 //=================【環境設定】================ size(400, 400); //画面サイズ colorMode(HSB,100); background(0); //=================【初期設定】================ int dt=0; //移動量 strokeCap(SQUARE); //==================メイン==================== for (int i=0; i<6; i++) { for (int y=0; y<height; y+=10) { strokeWeight(random(1, 6)); stroke(random(10,30), 100, 60); float rx = random(-20, 30); float rx2 = random(60, 80); float ry = random(20); float ry2 = random(20); line(rx+dt, y+ry, rx2+dt, y+ry2); } dt+=75; //移動する } |

LINE-0508 ランダムな長さの垂直線を描いてみます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//=========================================== // ランダムな長さの線 //=========================================== // CreationDate: 2025年2月25日 火曜日9:22:28 //=================【環境設定】================ size(400, 400); //画面サイズ background(255); //背景を白 //=================【初期設定】================ colorMode(HSB,100); float w = width/40; //線幅 float xdt; //X軸移動量 strokeWeight(w); //==================メイン==================== for (int i=0; i<width; i++) { xdt=i*w+w/2; float ry =random(250, 351); stroke(random(50,70), 70, 80); line(xdt, 0, xdt, ry); } |

LINE-0509 線を太くしてみましょう。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//=========================================== // ランダムな長さの線 //=========================================== // CreationDate: 2025年2月24日 月曜日16:58:41 //=================【環境設定】================ size(400, 400); //画面サイズ background(255); //背景を白 //=================【初期設定】================ colorMode(HSB); float w = width/10; //線幅 float xdt; //X軸移動量 strokeWeight(w); //==================メイン==================== for (int i=0; i<width; i++) { xdt=i*w+w/2; float ry =random(250, 351); stroke(random(200), 100, 180); line(xdt, 0, xdt, ry); } |

LINE-0510 左右の辺をランダムに結ぶ線を描いてみましょう。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
//=========================================== // Y軸上の線をランダムに結ぶ //=========================================== // CreationDate: 2025年2月24日 月曜日10:23:01 //=================【環境設定】================ size(400, 400); //画面サイズ //==================メイン==================== for (int i=0; i<50; i++) { float ry =random(0, height); float ry2 =random(0, height); line(0, ry, width, ry2); } |

LINE-0511 今度は辺に接しないランダムな線を描いてみましょう。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//=========================================== // ランダムな線 //=========================================== // CreationDate: 2025年2月24日 月曜日11:19:46 //=================【環境設定】================ size(400, 400); //画面サイズ //==================メイン==================== for (int i=0; i<100; i++) { float rx =random(10, 391);//ランダムなX始点を生成 float ry =random(10, 391);//ランダムなY始点を生成 float rx2 =random(10, 391);//ランダムなX終点を生成 float ry2 =random(10, 391);//ランダムなY終点を生成 line(rx, ry, rx2, ry2); //直線を描画 } |

LINE-0512 線幅、カラーを付けてみましょう。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//=========================================== // ランダムな線02 //=========================================== // CreationDate: 2025年2月24日 月曜日11:27:24 //=================【環境設定】================ size(400, 400); //画面サイズ background(255,0,0); //==================メイン==================== for (int i=0; i<100; i++) { float rx =random(10, 391);//ランダムなX始点を生成 float ry =random(10, 391);//ランダムなY始点を生成 float rx2 =random(10, 391);//ランダムなX終点を生成 float ry2 =random(10, 391);//ランダムなY終点を生成 strokeWeight(random(1,10)); stroke(random(220), random(180), random(256)); line(rx, ry, rx2, ry2); //直線を描画 } |

LINE-0513 繋げたランダムな線を描きます。vertex()を使います。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//=========================================== // ランダムな線を繋げる //=========================================== // CreationDate:2025年2月24日 月曜日21:21:13 //=================【環境設定】================ size(400, 400); //画面サイズ //=================【初期設定】================ noFill(); //塗りつぶしなし //==================メイン==================== beginShape(); for (int i=0; i<300; i++) { float rx=random(10, 390); float ry=random(10, 390); vertex(rx, ry); } endShape(); |

LINE-0514 線に色を付けてみましょう。上記のままでは線が全部繋がっているので1色しか色を付けられません。そこである程度線を描いたら区切って新たに線を描くことで、色を付けています。そのため上記にfor文を追加しています。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//=========================================== // ランダムな線を繋げる //=========================================== // CreationDate:2025年2月24日 月曜日21:31:28 //=================【環境設定】================ size(400, 400); //画面サイズ //=================【初期設定】================ noFill(); //塗りつぶしなし colorMode(HSB, 100); background(0); //==================メイン==================== for (int k=0; k<30; k++) { beginShape(); stroke(random(100), 100, 100); for (int i=0; i<10; i++) { float rx=random(10, 390); float ry=random(10, 390); vertex(rx, ry); } endShape(); } |


コメント