p5.js | cylinder() Function

The cylinder() function in p5.js is used to draw a cylinder with given radius and height.

Syntax:

cylinder( radius, height, detailX, detailY, bottomCap, topCap )

Parameters: The function accepts six parameters as mentioned above and described below:

  • radius: This parameter stores the radius of the surface.
  • height: This parameter stores the height of the surface.
  • detailX: This parameter stores the number of segments in x-dimension.
  • detailY: This parameter stores the number of segments in y-dimension.
  • bottomCap: This parameter stores the boolean value whether to draw bottom base of cylinder.
  • topCap: This parameter stores the boolean value whether to draw top base of cylinder.
  • Below programs illustrate the cylinder() function in p5.js:



    Example 1: This example uses cylinder() function to draw a cylinder with given radius and height.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function setup() {
        
        // Create Canvas of size 600*600
        createCanvas(600, 600, WEBGL);
    }
       
    function draw() {
          
        // Set background color
        background(200);
         
        // Set fill color of cylinder
        fill('green');
         
        // Call to cylinder function
        cylinder(100, 85, 24, 16, true, false);
    }

    chevron_right

    
    

    Output:

    Example 2: This example uses cylinder() function to draw a cylinder with given radius and height.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    function setup() {
        
        // Create Canvas of size 600*600
        createCanvas(600, 600, WEBGL);
    }
       
    function draw() {
          
        // Set background color
        background(200);
         
        // Set fill color of cylinder
        fill('yellow');
         
        // Rotate 
        rotateX(frameCount * 0.01);
        rotate(frameCount*0.03);
         
        // Call to cylinder function
        cylinder(140, 205, 24, 16, true);
    }

    chevron_right

    
    

    Output:

    Reference: https://p5js.org/reference/#/p5/cylinder



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.