我從 numberphile觀看了這個很棒的視頻 https://www.youtube.com/watch?v=JbfhzlMk2eY&ab_channel=Numberphile并想使用 Processing 重新創建它。概念是這樣的:用 2 個二進制數陣列創建隨機模式,第一組可能是句子中的元音和輔音,下一個可能是像 phi 這樣的長數中的偶數和奇數。
所以我的問題是,我如何遍歷我的陣列,然后根據數字的結果繪制帶有或不帶有初始偏移量的線條,首先垂直然后水平或其他方式。我得到了一些,但為什么我的代碼沒有繪制所有的垂直線?有人對這個有趣的小問題有意見嗎?
fill(0);
int y = 0;
int x = 0;
size(500, 500);
background(102);
noStroke();
int name[] = {1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,1,0};
int pi[] = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,3,2,3};
IntList oddEven;
oddEven = new IntList();
for (int i = 0; i < pi.length; i ) {
if (pi[i] % 2 == 0) {
oddEven.append(0);
}
else {
oddEven.append(1);
}
}
for(int g = 0; g < 17; g ) {
for(int l = 0; l < 17; l ) {
rect(x, y, 25, 1);
x =50;
}
if (oddEven.get(g) % 2 == 0)
{
x=25;
y =25;
}
else{
x= 0;
y =25;
}
}
for(int xxx = 0; xxx < 14; xxx ) {
for(int l = 0; l < 3; l ) {
rect(x, y, 1, 25);
y =50;
}
if (oddEven.get(xxx) % 2 == 0)
{
x=25;
y =25;
}
else{
y= 0;
x =25;
}
}
[1]: https://www.youtube.com/watch?v=JbfhzlMk2eY&ab_channel=Numberphile
uj5u.com熱心網友回復:
我添加了 setup() 和 draw() 并修改了繪制垂直線的第二個回圈。
int y = 0;
int x = 0;
IntList oddEven;
int name[] = {1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0};
int pi[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 3, 2, 3};
void setup() {
size(850, 425);
background(102);
//noStroke();
fill(0);
oddEven = new IntList();
for (int i = 0; i < pi.length; i ) {
if (pi[i] % 2 == 0) {
oddEven.append(0);
} else {
oddEven.append(1);
}
}
}
void draw() {
// Horizontal lines
for (int m = 0; m < name.length; m ) {
for (int n = 0; n < name.length; n ) {
rect(x, y, 25, 1);
x =50;
}
if (oddEven.get(m) % 2 == 0) {
x=25;
y =25;
} else {
x= 0;
y =25;
}
}
// Vertical lines
for (int a = 0; a < 34; a ) {
for (int b = 0; b < 9; b ) {
rect(x, y, 1, 25);
y =50;
}
x =25;
y= 0;
}
}
修訂版取消了 IntList,而是使用兩個 1 和 0 陣列。垂直線也使用第二個陣列的奇數/偶數檢查。演示被修改為使用線而不是矩形作為針跡;因此,使用了兩種線網格,一種用于水平針跡,一種用于垂直針跡。XOffset 是每行第一個水平針跡偏移的距離,yOffset 是每列第一個垂直針跡偏移的距離;兩者都是根據各自的陣列值設定的。請注意,垂直針跡陣列(cols)是水平針跡陣列(行)大小的兩倍,因為每個水平針跡都有兩端。反過來,
final int _horzRows = 17;
final int _horzCols = 17;
final int _vertRows = 8;
final int _vertCols = 34;
int rows[] = {1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0};
int cols[] = {1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0};
int x = 0;
int y = 0;
int xOffset = 0;
int yOffset = 0;
void horzGrid(int l, int t, int stitchLength) {
for (int j = 0; j < _horzCols; j ) {
for (int k = 0; k < _horzRows; k ) {
x = l j*(stitchLength*2); // stitch skip
y = t k*(stitchLength);
if (rows[k] == 1) {
xOffset = stitchLength;
} else {
xOffset = 0;
}
line(x xOffset, y, x xOffset stitchLength, y);
}
}
}
void vertGrid(int l, int t, int stitchLength) {
for (int m = 0; m < _vertCols; m ) {
for (int n = 0; n < _vertRows; n ) {
x = l m*(stitchLength);
y = t n*(stitchLength*2); // stitch skip
if (cols[m] == 1) {
yOffset = stitchLength;
} else {
yOffset = 0;
}
line(x, y yOffset, x, y yOffset stitchLength);
}
}
}
void setup() {
size(920, 480);
background(255);
strokeWeight(2);
horzGrid(30, 40, 25);
vertGrid(30, 40, 25);
}
void draw() {
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395358.html
上一篇:為什么Math.min()從[ 0,0,-0]回傳-0
下一篇:如何填充半圓的上部?
