前提・実現したいこと
プログラミング初学者です。
「ジェネラティブ・アート―Processingによる実践ガイド」のフラクタルの木をもとに、「上下左右方向に重複しないように点を描く(エイトクイーンのような感じ)」という機能を追加したいのですがうまくいきません。どなたかやり方を教えていただけるとありがたいです。
該当のソースコード
processing
1int _numChildren = 3; // limit on number of branches 2int _maxLevels = 3; // the maximum depth 3int[] pointX = {}; 4int[] pointY = {}; 5 6Branch _trunk; // the trunk of our fractal "tree" 7 8// ======================= init 9 10void setup() { 11 size(750,500); 12 background(255); 13 noFill(); 14 smooth(); 15 newTree(); 16} 17 18 19void newTree() { 20 _trunk = new Branch(1, 0, width / 2, 50); 21 _trunk.drawMe(); 22} 23 24 25 26// ======================= Branch object 27 28class Branch { 29 int level, index; 30 int x, y; 31 int endx, endy; 32 33 Branch [] children = new Branch[0]; // array of children 34 35 Branch(int lev, int ind, int ex, int why) { 36 level = lev; 37 index = ind; 38 39 updateMe(ex, why); 40 // make children 41 if (level < _maxLevels) { 42 children = new Branch[_numChildren]; 43 for (int x=0; x<_numChildren; x++) { 44 children[x] = new Branch(level+1, x, endx, endy); 45 } 46 } 47 } 48 49 void updateMe(int ex, int why) { 50 x = ex; 51 y = why; 52 53 endx = int(random(width)); 54 for (int i = 0; i < pointX.length; i++) { 55 if (endx > pointX[i] - 50 && endx < pointX[i] + 50) { 56 while (endx > pointX[i] - 50 && endx < pointX[i] + 50) { 57 endx = int(random(width)); 58 } 59 } 60 } 61 pointX = (int[])append(pointX,endx); 62 63 endy = int(random(height)); 64 for (int i = 0; i < pointY.length; i++) { 65 if (endy > pointY[i] - 50 && endy < pointY[i] + 50) { 66 while (endy > pointY[i] - 50 && endx < pointY[i] + 50) { 67 endy = int(random(width)); 68 } 69 } 70 } 71 pointY = (int[])append(pointY,endy); 72 73 for (int i=0; i<children.length; i++) { 74 children[i].updateMe(endx, endy); 75 } 76 } 77 78 79 void drawMe() { 80 strokeWeight(_maxLevels - level + 1); 81 line(x, y, endx, endy); 82 ellipse(endx, endy, 5, 5); 83 for (int i=0; i<children.length; i++) { 84 children[i].drawMe(); 85 } 86 } 87}
試したこと
描写した点のX,Y座標を配列に格納→updateMeで次の座標を決めるときに配列を参照し、値が近ければもう一度座標を振りなおす といった方法でできないかと思ったのですが、コード化出来ませんでした。
あなたの回答
tips
プレビュー