ProcessingJS 创建一个对象,其中包含随机数量的项目来画花
ProcessingJS Create an Object which holds random number of items to draw flowers
我正在使用 ProcessingJS 绘制一朵郁金香花。
我有如下代码。代码会画一朵带花瓣的郁金香花,x,y,height是踏板位置和高度的偏好。
`/*****************
*Tulip Object Type
******************/
var Tulip = function(x, y, height) {
this.x = x;
this.y = y;
this.height = height;
};
Tulip.prototype.draw = function() {
noStroke();
fill(16, 122, 12);
rect(this.x, this.y, 10, -this.height);
fill(255, 0, 0);
// petals
ellipse(this.x+5, this.y-this.height, 44, 44);
triangle(this.x-16, this.y-this.height,
this.x+20, this.y-this.height,
this.x-20, this.y-this.height-31);
triangle(this.x-14, this.y-this.height,
this.x+24, this.y-this.height,
this.x+3, this.y-this.height-39);
triangle(this.x+-4, this.y-this.height,
this.x+26, this.y-this.height,
this.x+29, this.y-this.height-36);
};
`
要绘制的函数:
void setup()
{
size(400 , 400);
background(125);
}
var tulip = new Tulip (200, 200, 10);
draw = function() {
tulip.draw();
};
我想画随机数的郁金香花,x,y,height的值也分别随机设置。
我正在考虑使用数组,但经过多次尝试仍然无法正常工作。如何根据上面的郁金香对象随机创建花朵?
创建一个随机郁金香数组:
int numTulips = random(10, 20); // between 10 and 20 tulips
Tulip[] tulips = new Tulip[numTulips];
for (int i = 0; i < numTulips; i++) {
tulips[i] = new Tulip (random(30, 370), random(30, 370), random(10, 30));
}
然后画郁金香
draw = function() {
for (int i = 0; i < numTulips; i++) {
tulips[i].draw();
}
};
创建一个郁金香数组。使用 random 设置该数组的长度。之后,循环抛出数组并将郁金香添加到数组中。请记住使用随机来制作每个郁金香的 x、y 和高度。
现在你有了一个包含随机数量的随机郁金香的数组,你可以做任何你想做的事
例如:
var Tulip = function(x, y, height) {
this.x = x;
this.y = y;
this.height = height;
};
var totalTulip = Math.floor(Math.random() * 10) + 1; // from 1 to 10
var tuplips = [];
for (var i = 0; i < totalTulip; i++) {
var x = Math.floor(Math.random() * 20) + 1; // from 1 to 20;
var y = Math.floor(Math.random() * 20) + 1; // from 1 to 20;
var h = Math.floor(Math.random() * 100) + 1; // from 1 to 100;
var tuplip = new Tulip(x, y, h);
tuplips.push(tuplip);
}
P/s: Javascript:D
我正在使用 ProcessingJS 绘制一朵郁金香花。 我有如下代码。代码会画一朵带花瓣的郁金香花,x,y,height是踏板位置和高度的偏好。
`/*****************
*Tulip Object Type
******************/
var Tulip = function(x, y, height) {
this.x = x;
this.y = y;
this.height = height;
};
Tulip.prototype.draw = function() {
noStroke();
fill(16, 122, 12);
rect(this.x, this.y, 10, -this.height);
fill(255, 0, 0);
// petals
ellipse(this.x+5, this.y-this.height, 44, 44);
triangle(this.x-16, this.y-this.height,
this.x+20, this.y-this.height,
this.x-20, this.y-this.height-31);
triangle(this.x-14, this.y-this.height,
this.x+24, this.y-this.height,
this.x+3, this.y-this.height-39);
triangle(this.x+-4, this.y-this.height,
this.x+26, this.y-this.height,
this.x+29, this.y-this.height-36);
};
`
要绘制的函数:
void setup()
{
size(400 , 400);
background(125);
}
var tulip = new Tulip (200, 200, 10);
draw = function() {
tulip.draw();
};
我想画随机数的郁金香花,x,y,height的值也分别随机设置。
我正在考虑使用数组,但经过多次尝试仍然无法正常工作。如何根据上面的郁金香对象随机创建花朵?
创建一个随机郁金香数组:
int numTulips = random(10, 20); // between 10 and 20 tulips
Tulip[] tulips = new Tulip[numTulips];
for (int i = 0; i < numTulips; i++) {
tulips[i] = new Tulip (random(30, 370), random(30, 370), random(10, 30));
}
然后画郁金香
draw = function() {
for (int i = 0; i < numTulips; i++) {
tulips[i].draw();
}
};
创建一个郁金香数组。使用 random 设置该数组的长度。之后,循环抛出数组并将郁金香添加到数组中。请记住使用随机来制作每个郁金香的 x、y 和高度。
现在你有了一个包含随机数量的随机郁金香的数组,你可以做任何你想做的事
例如:
var Tulip = function(x, y, height) {
this.x = x;
this.y = y;
this.height = height;
};
var totalTulip = Math.floor(Math.random() * 10) + 1; // from 1 to 10
var tuplips = [];
for (var i = 0; i < totalTulip; i++) {
var x = Math.floor(Math.random() * 20) + 1; // from 1 to 20;
var y = Math.floor(Math.random() * 20) + 1; // from 1 to 20;
var h = Math.floor(Math.random() * 100) + 1; // from 1 to 100;
var tuplip = new Tulip(x, y, h);
tuplips.push(tuplip);
}
P/s: Javascript:D