# \[SOLVED\] PolygonCeption Help : Confused screaming

**URL:** <https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062>\
**Category:** Level Help\
**Created:** [June 6, 2020, 1:10pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062 "2020-06-06T13:10:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 6, 2020, 1:10pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/1 "2020-06-06T13:10:22Z")

</div>

I literally don’t get what i should do here. there are no instructions on what variables to use or what guide point i should get. here is my code.

```python
// 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.
hero.setFlowerColor("purple");
// You need a function to convert degrees to radians. Multiply degrees by Math.PI / 180.
function convert(n){
    return Math.PI / 180 * n;
}

// Your polygon function should have 3 inputs: start, end, and sides.
function polygon (start,end,sides){
    var full = Vector.subtract(end , start);
    var mag = full.magnitude();
    if (mag < 2) {
        hero.toggleFlowers(false);
        hero.moveXY(start.x, start.y);
        hero.toggleFlowers(true);
        hero.moveXY(end.x, end.y);
        return;
    }
    
    var fifth = Vector.multiply(full, 1/5);
    var angle = convert(360/sides);
    var A = Vector.add(start ,fifth);
    var rotate = Vector.rotate(fifth,angle);
    var B = Vector.add(rotate,A);
    var C = Vector.add(rotate,B);
    var D = Vector.add(rotate,C);
    polygon(start,A,sides);
    polygon(A,B,sides);
    polygon(B,C,sides);
    polygon(C,D,sides);
    polygon(D,end,sides);
}
// 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.
var startOffset = new Vector(-15, -15);
var endOffset = new Vector(15, -15);
var enemies = hero.findEnemies();
// You need to loop through all the yaks, drawing a polygon for each. Yaks are enemies.
for (var i = 0; i < enemies.length;i++){
    var enemy = enemies[i];
    var sides = enemy.sides;
    var start = Vector.add(enemy.pos, startOffset);
    var end = Vector.add(enemy.pos, endOffset);
    polygon(start,end,sides);
}

```

Note: this code creates a weird shape on the first yak. that’s it

I heard that @Chaboi_3000 has done this level, so i might need to get some tips from him. been trying for days.

ANY help would be appreciated  
Riley

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 20, 2020, 7:03am UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/2 "2020-06-20T07:03:41Z")

</div>

Hi Riley,

Are you still working on this one? I’ve (just) scraped through the level 😆 - it’s a bit of a mystery why it works, but I’m happy to try to help if you still want it.

J.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 25, 2020, 11:55am UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/3 "2020-06-25T11:55:01Z")

</div>

uhh YES. please tell me how i can fix this.

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 25, 2020, 5:48pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/4 "2020-06-25T17:48:25Z")

</div>

Hey, that’s an amazing recursive shape that you manage to draw 😄. I’ll have a go, and @dedreous, @AnSeDra or @Deadpool198 can help when I’m stuck 😉 . I think the bit that needs work is this:

```python
   var fifth = Vector.multiply(full, 1/5);
   var angle = convert(360/sides);
   var A = Vector.add(start ,fifth);
   var rotate = Vector.rotate(fifth,angle);
   var B = Vector.add(rotate,A);
   var C = Vector.add(rotate,B);
   var D = Vector.add(rotate,C);
   polygon(start,A,sides);
   polygon(A,B,sides);
   polygon(B,C,sides);
   polygon(C,D,sides);
   polygon(D,end,sides);

```

You look as though you’ve based your code on assuming that there’s going to be 5 sides in the shape, so you need to make it more generic. Rather than specifying the number of sides, I used a for loop for the whole of this chunk, so the loop repeated ‘sides’ times. Inside the for loop were 5 lines of code:

1. The hero.moveXY(end.x, end.y); term - have this here rather than before the return.
2. Redefine ‘full’ using the Vector.rotate
3. The var fifth, as you’ve got it.
4. The polygon function in here, with fifth as ‘end’
5. Redefine ‘end’

Think you’ll still have some head scratching to do, but hopefully that gets you going in the right direction. Good luck!

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 25, 2020, 5:54pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/5 "2020-06-25T17:54:56Z")

</div>

I will have to go pretty soon, so can you, @dedreous or @Deadpool198, help @Valentino_Artawan with this level?

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 26, 2020, 12:14pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/6 "2020-06-26T12:14:06Z")

</div>

Uhhh

```python
function polygon (start,end,sides){
    var full = Vector.subtract(end , start);
    var mag = full.magnitude();
    if (mag < 2) {
        hero.toggleFlowers(false);
        hero.moveXY(start.x, start.y);
        return;
        
    }
    for (var i = 0; i < sides; i++){
        hero.toggleFlowers(true);
        hero.moveXY(end.x, end.y);
        var angle = convert(360/sides);
        var fifth = Vector.multiply(full, 1/5);
        full = Vector.rotate(full,angle);
        polygon(full,fifth,sides);
        fifth = Vector.multiply(full, 1/5);
    }
}

```

wat???

no i literally don’t get this XD

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 26, 2020, 12:44pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/7 "2020-06-26T12:44:14Z")

</div>

You’re close!

```python
        var fifth = Vector.multiply(full, 1/5);
        full = Vector.rotate(full,angle);

```

Swap these over.

```python
        polygon(full,fifth,sides);

```

I missed out a step here. Rather than ‘fifth’, this should be ‘fifth’ added to ‘end’. Also, the start point of the polygon should be ‘end’.

```python
  fifth = Vector.multiply(full, 1/5);

```

Not quite for this line. You need to redefine ‘end’ by adding ‘full’ to it.

Keep going 😃.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 26, 2020, 12:51pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/8 "2020-06-26T12:51:05Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/3/3e68975aef6c24e7400a4fe88d8f895705faee72.png)

something’s wrong, but where?

P.S : i… i think we made it backwards…

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [June 26, 2020, 2:28pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/9 "2020-06-26T14:28:59Z")

</div>

Let’s see a fresh (recent) copy of your code.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 27, 2020, 8:30am UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/10 "2020-06-27T08:30:12Z")

</div>

here you go

```python
// 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.
hero.setFlowerColor("purple");
// You need a function to convert degrees to radians. Multiply degrees by Math.PI / 180.
function convert(n){
    return Math.PI / 180 * n;
}

// Your polygon function should have 3 inputs: start, end, and sides.
function polygon (start,end,sides){
    var full = Vector.subtract(end , start);
    var mag = full.magnitude();
    if (mag < 2) {
        hero.toggleFlowers(false);
        hero.moveXY(start.x, start.y);
        return;
        
    }
    for (var i = 0; i < sides; i++){
        hero.toggleFlowers(true);
        hero.moveXY(end.x, end.y);
        var angle = convert(360/sides);
        full = Vector.rotate(full,angle);
        var fifth = Vector.multiply(full, 1/5);
        var end2 = Vector.add(fifth,end);
        polygon(end,end2,sides);
        end = Vector.add(end,full);
    }
}
// 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 yaks position.
var startOffset = new Vector(-15, -15);
var endOffset = new Vector(15, -15);
var enemies = hero.findEnemies();
// You need to loop through all the yaks, drawing a polygon for each. Yaks are enemies.
for (var i = 0; i < enemies.length;i++){
    var enemy = enemies[i];
    var sides = enemy.sides;
    var start = Vector.add(enemy.pos, startOffset);
    var end = Vector.add(enemy.pos, endOffset);
    polygon(start,end,sides);
}

```

ok, so i think something needs to be changed. something minimal.

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 27, 2020, 3:27pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/11 "2020-06-27T15:27:19Z")

</div>

It’s working when I run your code, just the toggling the flowers on & off isn’t happening at quite the right times. I’ll leave you to play with that.

For the

```python
if (mag < 2) {

```

I needed to change the 2 to a 5 (I know the instructions say 2) to make it work.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [June 27, 2020, 3:46pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/12 "2020-06-27T15:46:22Z")

</div>

heh…ya beat me to it! 😜 However, the ‘mag \< 2’ worked perfectly, once I fixed the toggling.

In a nutshell, I simply added two additional lines.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [July 4, 2020, 12:02pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/13 "2020-07-04T12:02:35Z")

</div>

What exactly? i’ve been tinkering and still no luck

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [July 4, 2020, 12:06pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/14 "2020-07-04T12:06:42Z")

</div>

nevermind. found the two lines. finished the level. THX guys! 😃

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [July 4, 2020, 2:48pm UTC](https://discourse.codecombat.com/t/solved-polygonception-help-confused-screaming/22062/15 "2020-07-04T14:48:27Z")

</div>

Well done! 😃  
(20 characters)
