# How to create a 3D star?

**URL:** https://discourse.processing.org/t/how-to-create-a-3d-star/39754
**Category:** Beginners
**Created:** [November 17, 2022, 2:16am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754 "2022-11-17T02:16:08Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![timob256](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@timob256](https://discourse.processing.org/u/timob256)
#### Post date: [November 17, 2022, 2:16am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/1 "2022-11-17T02:16:08Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I trying create 3d star but this not good wraiting cone 😞

```
  // звезда 3д модель
 function zvezda ( per_x, per_y, per_z) {
       for (i=0; i <=4; i=i+1) {
	     rotateX (radians(0) ) ; // обнуляю угол что трасляте норм было
    
           translate(per_x, per_y, per_z) ; // уст располож
    
           rotateX ( radians(i* 75));
   
           fill("yellow") ; // цвет
	cone (2 , 10) ; // конус 
	pop() ; // вост сост системы
        push() ; // сохр сост в стек

     }
   }

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 17, 2022, 3:49am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/2 "2022-11-17T03:49:04Z")

</div>

push is directly after the for loop command line

pop can stay

So both surround the part with rotate/ translate/ cone

Try it please

---

<div class="post-metadata">

### Author: ![timob256](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@timob256](https://discourse.processing.org/u/timob256)
#### Post date: [November 17, 2022, 11:58am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/3 "2022-11-17T11:58:03Z")

</div>

![Screenshot_20221117-145410](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/d/5d4bf9e815c5b88bfd107040368ed90b913a7674.jpeg)  
This not work ☹

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 17, 2022, 12:02pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/4 "2022-11-17T12:02:34Z")

</div>

Can’t you draw lines from the center to the  
outside like this: `*`

with i from 0 to 360 with step +10

```auto
float x1=cos(radians(i)) * 40 + width/2 ;
float y1=sin(radians(i)) * 40 + height/2 ;

line (width/2, height/2,
  x1,y1);

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [November 17, 2022, 3:17pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/5 "2022-11-17T15:17:36Z")

</div>

Hi

> **[Reference](https://processing.org/reference/push_.html)**
>
> The push() function saves the current drawing style settings and transformations, while pop() restores these settings. Note that these functions are always used together. They allow you …

> **[Reference](https://processing.org/reference/pop_.html)**
>
> The pop() function restores the previous drawing style settings and transformations after push() has changed them. Note that these functions are always used together. They allow you to c…

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 18, 2022, 3:00am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/6 "2022-11-18T03:00:12Z")

</div>

Hello @timob256 ,

Try this with _ **one** _ cone first:

- push
- rotateZ
- translate \< Translate from center… try one axis at a time to see the changes
- cone \< large enough to see
- pop

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/d/ed0d91ab20b8cd494aa5bc56105aeee8da4463fd.png)

After you have translated the cone to where you want it then try a for loop.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/7/1787ca09a824a991e2e3ed650e3a9b159f2e4aac.png)

Note:  
360/5 = 72

References:

> **[learn | p5.js](https://p5js.org/learn/)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

`:)`

---

<div class="post-metadata">

### Author: ![timob256](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@timob256](https://discourse.processing.org/u/timob256)
#### Post date: [November 19, 2022, 11:32am UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/7 "2022-11-19T11:32:17Z")

</div>

Hello @glv .

Please show the code from the given example. I still don’t understand how to set the angle correctly.

```
 // звезда 3д модель
 function zvezda ( per_x, per_y, per_z) {
       // луч 1
       push();
       fill("yellow") ; // цвет
       //rotateX(PI/2);
       translate(per_x, per_y+10, per_z) ; // уст располож
       rotateX ( radians(0));
       cone(w=2, h=10);
       pop();
       // луч2
       push();
       fill("yellow") ; // цвет
       translate(per_x, per_y+5, per_z+5) ; // уст располож
       rotateX ( radians(72));
       cone(w=2, h=10);
       pop();
       // луч3
       push();
       fill("yellow") ; // цвет
       translate(per_x, per_y, per_z+5) ; // уст располож
       rotateX ( radians(144));
       cone(w=2, h=10);
       pop();
       // луч4
       push();
       fill("yellow") ; // цвет
       translate(per_x, per_y, per_z-5) ; // уст располож
       rotateX ( radians(216));
       cone(w=2, h=10);
       pop();
       // луч5
       push();
       fill("yellow") ; // цвет
       translate(per_x, per_y+5, per_z-5) ; // уст располож
       rotateX ( radians(288));
       cone(w=2, h=10);
       pop();
    }

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 19, 2022, 12:16pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/8 "2022-11-19T12:16:27Z")

</div>

Hello @timob256,

> [@timob256](#):
>
> Please show the code from the given example. I still don’t understand how to set the angle correctly.

You need to progress in steps.

This is what I see with your code:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/8/48d38f8a3ec6d1ad908f06414acb1ddd1bce8a5f.png)

> [@glv](#):
>
> cone \< large enough to see

With a larger cone (10x bigger):

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/6/a6e368235349043d9c148e54189b558d7d7c4405.png)

Using `rotate()` which is the same as `rotateZ()`:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/6/3632032c7022c855856a8ec891d5b09d8379c0f6.png)

I suggest that you try working with one cone and see how the translation and rotation work.

The cone is initially centered and you need to:

- translate()
- rotate()
- [rinse and repeat](https://www.urbandictionary.com/define.php?term=Rinse%20And%20Repeat)

There is a Processing JAVA tutorial here and the concepts apply to p5.js:

> **[2D Transformations](https://processing.org/tutorials/transform2d/)**
>
> Learn how to translate, rotate, and scale shapes using 2D transformations.

`:)`

---

<div class="post-metadata">

### Author: ![timob256](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@timob256](https://discourse.processing.org/u/timob256)
#### Post date: [November 19, 2022, 3:27pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/9 "2022-11-19T15:27:37Z")

</div>

```
 // рисую зведу
 function setup() {
     createCanvas( 200, 200, WEBGL); //3д
     angle =0; // угол
     noStroke(); // делаем рëбра фигур невидимыми
     scale(1) ; // можно менять маштаб
     push() ; // сохраняем состояние в стек
  }

// звезда 3д модель
 function zvezda ( per_x, per_y, per_z) {

 // луч 1
  push();
   fill("yellow") ; // цвет
  //translate(per_x, per_y+10, per_z) ; // уст располож
  translate(per_x, per_y+cos(radians(0)) * 40, per_z+ sin(radians(0))*40); // уст располож
 rotateX ( radians(0));
  cone(w=20, h=100);
  pop();
  // луч2
  push();
   fill("yellow") ; // цвет
   //translate(per_x, per_y+5, per_z+5) ; // уст располож
   translate(per_x, per_y+cos(radians(72)) * 40, per_z+ sin(radians(72))*40); // уст располож
   rotateX ( radians(72));
   cone(w=20, h=100);
   pop();
   // луч3
  push();
   fill("yellow") ; // цвет
   //translate(per_x, per_y, per_z+5) ; // уст располож
   translate(per_x, per_y+cos(radians(144)) * 40, per_z+ sin(radians(144))*40); // уст располож
   rotateX ( radians(144));
   cone(w=20, h=100);
   pop();
   // луч4
    push();
   fill("yellow") ; // цвет
   //translate(per_x, per_y, per_z-5) ; // уст располож
   translate(per_x, per_y+cos(radians(216)) * 40, per_z+ sin(radians(216))*40); // уст располож
   rotateX ( radians(216));
   cone(w=20, h=100);
   pop();
   // луч5
   push();
   fill("yellow") ; // цвет
    //translate(per_x, per_y+5, per_z-5) ; // уст располож
   translate(per_x, per_y+cos(radians(288)) * 40, per_z+ sin(radians(288))*40); // уст располож
   rotateX ( radians(288));
    cone(w=20, h=100);
    pop();
}
     
 function my_rotate() {
     	//rotateX(angle);
      rotateY(angle);
    	// rotateZ(_angle);

   angle += 0.01;
    
    }
      	
     function draw() {
  	  background(220,220,220, 5);
            	
    my_rotate(); // крутим
         rotateX ( radians(180)); // поварачиваем звезду 
         zvezda(0, 0, 0) ;
  }

```

 ![Screenshot_20221119-181952](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/f/9fd51307a8d28e731617bc6d2509bd1f00f68091.jpeg)

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [November 19, 2022, 3:47pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/10 "2022-11-19T15:47:04Z")

</div>

Hello @timob256! 🙂  
This is such a great question!  
I hope you don’t mind but I changed the title of your topic

from:  
“How create star?”  
to:  
“How to create a 3D star?”

**This better reflects the complexity of your question and will make it more search-friendly for others with the same issue in the future!**  
Of course, if you feel this new title does not accurately represent your topic, please feel free to write a new title with searchable keywords. 🙂  
🤓

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 19, 2022, 6:28pm UTC](https://discourse.processing.org/t/how-to-create-a-3d-star/39754/11 "2022-11-19T18:28:06Z")

</div>

Nice work!

Consider adding some finishing touches:

- lights() \< This revealed some geometry in the middle from overlapping cones
- sphere() \< This covered that geometry

Reference:  
[https://p5js.org/reference/#/p5/lights](https://p5js.org/reference/#/p5/lights) \< I changed all the 128 defaults to 255  
[https://p5js.org/reference/#/p5/sphere](https://p5js.org/reference/#/p5/sphere)

`:)`
