previous

page 21

next
points()

In the last tute, I showed you a way to gather the points at the corners of any polygon. Perhaps you realised that despite the name, cpolygon, these were not perfectly centred.

This time I'm going use a different approach. Check this out:

function points(n) {
var t=turtle();
home(t); pu(t);
var x=[]; var y=[];
var angle=360/n; var d=350;
for (var i=0;i<n;i++) {
fd(t,d); x.push(t.x); y.push(t.y);
bk(t,d); rt(t,angle);
}
return {x:x, y:y};
}

Note the use of an associative array to return the points and the fact that this turtle will vanish once this function completes.

function test() {
var pts=points(4);
var t=[];
for (var i=0; i<4; i++) {
t[i]=turtle();
t[i].x=pts.x[i];
t[i].y=pts.y[i];
}
for (var i=0; i<4; i++) {
var k=i+1;
if (k==4) k=0;
join(t[i],t[k]);
}
}

previous
next