-
Notifications
You must be signed in to change notification settings - Fork 0
Home
lincore81 edited this page Sep 27, 2012
·
11 revisions
A visitor? No, it's probably just me again, isn't it? Well, maybe you aren't me, that would be a pleasent surprise, I suppose. Actually, no. This is just my note block, not meant for all the not-me's out there. Not that you aren't allowed to read it, it's public after all, but with the potential risk of receiving an overdose of useless information lurking around every corner these days I'd really ask myself if there's any reason to read on. Still reading? Okay, you must be me after all. Hi me!
/* $turtle/poly.js <sides> <length> <block>
* This script draws a regular polygon.
*/
// get access to Blocks
importPackage(Packages.xde.lincore.mcscript)
// get access to VectorTurtle
importPackage(Packages.xde.lincore.mcscript.edit)
// read script arguments given by the user:
sides = args.getNumber("s(ides)?|1", 3)
sideLength = args.getNumber("l(ength)?|2", 10)
block = Blocks.find(args.get("b(lock)?|3", "light green"))
// make sure we have at least three sides to draw a real polygon
if (sides < 3) sides = 3
circumference = sides * sideLength // calculate the circumference of the polygon
radius = circumference / (2 * Math.PI) // c = 2 * PI * r ==> r = c / (2 * PI)
angle = 360 / sides // calculate the angle to turn after drawing one side of the polygon
center = mc.user.position // the player stands in the center of the polygon
startpos = center.north(radius) // start from a position <radius> blocks north of the center
// create a new VectorTurtle at <startpos>, looking east (0°) and drawing with <block>.
turtle = new VectorTurtle(startpos, block, 0)
// call the function that draws the polygon
drawPolygon()
// here we define the function that does the actual drawing:
function drawPolygon() {
i = 0
// repeat the stuff in curly brackets for as long as i is smaller than the number of sides:
while (i < sides) {
// let the turtle turn right <angle> degrees and then go forward <sideLength> blocks
turtle.rt(angle).fd(sideLength)
i++ // increment i by one
}
}