Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Polygonception - someone can help?

#1

Who can help?every time only two yaks are successfully encircled,and I can’t pass this level

# You are on your own this time, I hope you have learned what you need from the previous fractal levels. Check the guide for help with what you need to do and with the math required for polygons.

# You need a function to convert degrees to radians.  Multiply degrees by Math.PI / 180.
def degreesToRadians(angle):
    return Math.PI / 180 * angle
    

# Your polygon function should have 3 inputs: start, end, and sides.
def line(start,end,n,k):
    side = Vector.subtract(end,start)
    distance = side.magnitude()
    if distance < k / n + 1:
        hero.toggleFlowers(False)
        hero.moveXY(start.x, start.y)
        hero.toggleFlowers(True)
        hero.moveXY(end.x, end.y)
        return
    fenxing = Vector.divide(side, n)
    for i in range(n):
        rotate = Vector.rotate(fenxing, degreesToRadians(360 * i / n))
        A = Vector.add(rotate,start)
        line(start,A,n,k)
        start = A
# Remember to make your polygon recursive, drawing extra polygons at every corner.
# To get the start and end position for each polygon, add startOffset and endOffset to the yak's position.
def flake(a,b,n):
    side = Vector.subtract(b,a)
    k = side.magnitude()
    for i in range(n):
        line(a,b,n,k)
        hero.toggleFlowers(True)
        hero.moveXY(b.x, b.y)
        side = Vector.rotate(side,degreesToRadians(360 / n))
        a = b
        b = Vector.add(b,side)

startOffset = Vector(-15, -15)
endOffset = Vector(15, -15)

# You need to loop through all the yaks, drawing a polygon for each.  Yaks are enemies.
enemies = hero.findEnemies()
for enemy in enemies:
    sides = enemy.sides
    vecEnemy = Vector(enemy.pos.x,enemy.pos.y)
    start = Vector.add(vecEnemy,startOffset)
    end = Vector.add(vecEnemy,endOffset)
    flake(start,end,sides)

automatically bumped #2

Can someone help this fella here?
-@Chaboi_3000

#3

Sorry @jxcwzk I’m terrible at recursion. I know @Chaboi_3000 has done this level.
:lion:

1 Like
#4

I’ll get to this topic once I’m back from my trip. :+1:

#5

I was fainted and This level does afflict people

#6

Just be patient, and we’ll work on this when we have the time.

#7

@jxcwzk Were you able to complete this level? I changed two lines of code on yours and did get all four yaks. I can’t necessarily explain why it worked exactly, but it did.

#8

not yet…and i studying Raspberry Pi

#9

The hints say to have the distance less than 2, but I can’t seem to get that to work. It ends up adding extra recursive and I saw another post commenting on the same thing.

The two lines I changed to make it work:

Spoiler linked to original code

In the line function.

if distance < k / n + 1: # changed to < 10:
# and
fenxing = Vector.divide(side, n) # changed n to 5 since it is supposed to be a 5th of the size

I’d be interested to see some other code to learn how to create a recursive with side length of 2.