Announcement

Collapse
No announcement yet.

PHP: Circle Functions

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP: Circle Functions

    Well I got bored this morning as I only have to go into school for exams and I have one today, maths! So I was wondering how I could help myself remember the formula to calculate the area of a circle and the circumference of a circle. So I was browsing this forum and I saw LordDan post a mathematics function to calculate percentage so I thought to myself that I should do this with the formula and create two functions. I ended up writing these functions and I have tested them but I would like to improve them if others are kind enough to give feedback and other ideas.

    Constructive criticism is welcome, flaming is not.

    PHP Code:
    <?php

    ## Created by Karl Ballard
    function area_of_circle($val$radius true$dec_return 3)
    {
        if (!
    is_numeric($val))
        {
            return 
    false;
        }
        
        if (!
    is_numeric($pi))
        {
            
    $pi pi();
        }
        
        if (
    $radius === false)
        {
            
    $val $val 2;
        }
        
        return 
    $dec_return === false $pi $val $val number_format($pi $val $val$dec_return);
    }

    ## Created By Karl Ballard
    function circum_of_circle($val$radius false$dec_return 3)
    {
        if (!
    is_numeric($val))
        {
            return 
    false;
        }
        
        if (!
    is_numeric($pi))
        {
            
    $pi pi();
        }
        
        if (
    $radius === true)
        {
            
    $val $val 2;
        }
        
        return 
    $dec_return === false $pi $val number_format($pi $val$dec_return);
    }
    The best way to make your dreams come true is to wake up.
    - Paul Valery

    Facebook Me? | devianWATCH Me? | Tumblr Me? | Tweet Me?

  • #2
    PHP Code:
    <?php

    function areaOfCircle$radius ){
        return 
    number_formatpi() * $radius $radius);
    }

    echo 
    areaOfCircle10 ); // 314.15

    ?>
    Enjoy
    Edit: Not sure about circumference, never paid attention during that..
    [progressbar=90]Perfecting my Insomnia[/progressbar]

    Comment


    • #3
      I added more than the base code because I wanted to make the function to be used more easily and more customizable without editing the function source code.
      The best way to make your dreams come true is to wake up.
      - Paul Valery

      Facebook Me? | devianWATCH Me? | Tumblr Me? | Tweet Me?

      Comment


      • #4
        The circumference is either Diameter times 3.14 twice, or Radius times 3.14 twice.

        Comment


        • #5
          Originally posted by Vortex23','index.php?page=Thread&postID=171457#pos t171457
          I added more than the base code because I wanted to make the function to be used more easily and more customizable without editing the function source code.
          I know I just posted the one I use for reference.. Obviously, if your project is open source or you have a team it's good to have all the extra checks, but I rarely need to find areas so I left it bare bones :thumbup:

          Originally posted by SolidSnake','index.php?page=Thread&postID=171471#p ost171471
          The circumference is either Diameter times 3.14 twice, or Radius times 3.14 twice.
          Glad someone paid attention in school. ^^
          [progressbar=90]Perfecting my Insomnia[/progressbar]

          Comment


          • #6
            Ahh I see, well if it was 100% personal to myself and no-one else would develop with myself then I would do it almost the same way... This is just so I can limit the amount of digits after the decimal are returned.

            PHP Code:
            ## Created by Karl Ballard
            function area_of_circle($rad$dec_return 2)
            {
                if (!
            is_int($dec_return))
                {
                    
            $dec_return 2;
                }
                
                return 
            $dec_return === false pi() * $rad $rad number_format(pi() * $rad $rad$dec_return);
            }

            ## Created By Karl Ballard
            function circum_of_circle($dia$dec_return 2)
            {
                if (!
            is_int($dec_return))
                {
                    
            $dec_return 2;
                }
                    
                return 
            $dec_return === false pi() * $dia number_format(pi() * $dia$dec_return);

            The best way to make your dreams come true is to wake up.
            - Paul Valery

            Facebook Me? | devianWATCH Me? | Tumblr Me? | Tweet Me?

            Comment

            Working...
            X