How to pass a single element to a function expecting an array

19 views
Skip to first unread message

Prakasam

unread,
Dec 4, 2010, 12:44:05 AM12/4/10
to Processing.js
I wrote the function drawCircle(number, radii); where number is the
number of circles to draw and radii is an array containing radius of
each circle. If i want to draw a single circle it is enough to pass a
single element in a radii. But trying to pass an array with a single
element is not accepted by the function. How to pass a single element
to this array?

drawCircle(number, radii) {
for(i = 0; i<number; i++) {
if(number == radii.length) {
radius = radii[0];
} else {
radius = radii[0];
}

ellipse(20 + radius, 50, radiud, radius);
}
}

Kyle Phillips

unread,
Dec 4, 2010, 2:26:07 AM12/4/10
to proces...@googlegroups.com
You can pass an array with a single element. Here's how I would do, what I think your trying to do in processing.js (w/ java syntax in this example)

int[] radii= {25};
drawCircles(radii);
void drawCircles(int[] radii) {
int number = radii.length;
for(int i=0;i<number;i++){
int radius = radii[i];
ellipse(20+radius,50,radius,radius);
}
}


If it's straight JavaScript it's more like
drawCircles(new Array(25));

- Kyle
http://workofkylephillips.com

> --
> You received this message because you are subscribed to the Google Groups "Processing.js" group.
> To post to this group, send email to proces...@googlegroups.com.
> To unsubscribe from this group, send email to processingjs...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/processingjs?hl=en.
>

Reply all
Reply to author
Forward
0 new messages