Results 1 to 8 of 8

Thread: Draw a Polygon on the fly?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Draw a Polygon on the fly?

    If I'm given a number of vertices/edges, how can I dynamically draw a polygon?

    Code:
    function drawPolygon($edges) {
        imagepolygon(/* ... */);
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Draw a Polygon on the fly?

    You will need to have the GD extension / enabled or installed to do this. The function you need is simiar to the one in VB.

    http://uk2.php.net/manual/en/functio...ledpolygon.php

    or

    http://uk2.php.net/manual/en/function.imagepolygon.php
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Draw a Polygon on the fly?

    Quote Originally Posted by visualAd
    You will need to have the GD extension / enabled or installed to do this. The function you need is simiar to the one in VB.

    http://uk2.php.net/manual/en/functio...ledpolygon.php

    or

    http://uk2.php.net/manual/en/function.imagepolygon.php
    I know how to use the function, that's not the problem. What is the problem is that I'm going to have an unknown number of edges.

    The function I outlined above, takes $edges as a parameter, which is an integer, 2 to...say 15, or whatever. This number will tell how many edges the polygon I have to draw is. Rather than hard coding the (x,y) coords for each possibile polygon, is there a way to do this dynamically?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Draw a Polygon on the fly?

    I don't see that being a problem. As long as the array containing the points, contains only the co-ordintates for the for the polygon, you can obtain the number of points by using the array count:
    PHP Code:
    $points = (int) (count($pointarray) / 2); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Draw a Polygon on the fly?

    Quote Originally Posted by visualAd
    I don't see that being a problem. As long as the array containing the points, contains only the co-ordintates for the for the polygon, you can obtain the number of points by using the array count:
    PHP Code:
    $points = (int) (count($pointarray) / 2); 
    You're not understanding me. I don't have an array containing the points. I need to build that array based on how many points I'll have.

    Ie, all I know is how many points/edges.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Draw a Polygon on the fly?

    You'll need to know one other thing, the shape of the polygon. Given the first point and the basic shape, you can work out the other points relative to the first.

    I.e: If you shape has 4 points / sides, how will you know where to put these? If however you know that it has 4 sides and is a diamond, you can work out where to place the other points from the first and build the array appropriatley.

    This will draw a diamond:
    PHP Code:
    <?php
        
    function draw_diamond($x$y$length$image$color)
        {
            
    $point_array = Array($x$y,
                                 
    $x $length$y $length,
                                 
    $x$y + ($length),
                                 
    $x $length$y $length);
            
            
    imagepolygon($image$point_array4$color);
        }
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Draw a Polygon on the fly?

    That's what I was wondering. Whether or not I'd have to write a function for each number. That'd be a lot of functions. Triangle, diamond, pentagon, etc. What if the number is 22? I don't even know the name for that.

    I was hoping for a super cool method that could take care of it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Draw a Polygon on the fly?

    I played about with something like this before but never got very far with it. Heres the code I had, its not brilliant but it might give you an idea?

    PHP Code:
      $Image_Width 450;
      
    $Image_Height 450;
      
      
    $temp = @imagecreate($Image_Width$Image_Height);//create our image
      
      
    $background_color imagecolorallocate($temp 255255255);//fill the background
      
    $color imagecolorallocate($temp 000);//create our pencolour
      
      
    $point_array GetPoints(5);
      
      
    imagepolygon($temp$point_arraycount($point_array)/2$color);
      
      
    imagejpeg($temp,'',100);
      
    imagedestroy($temp);
      
      
      
      function 
    GetPoints($sides)
      {
          
    $d 360 $sides;
          
    $d 180 $d;
      
          
    $y 100;
          
    $x 100;
      
          
    $points[]=$x;
          
    $points[]=$y;
      
         
    $sidelength 100;
      
          
    $rotation $d;
          for(
    $i 1$i $sides;$i++)
          {
          
              
    $x Cos(DegToRad($rotation ))* $sidelength  $x;
              
    $points[] = $x;
              
    $ySin(DegToRad($rotation ))* $sidelength  $y;
              
    $points[] =$y;
      
              
    $rotation += $d;
          }
          return 
    $points;
      }
      function 
    DegToRad($var)
      {
          return 
    M_PI $var180.0;
      } 
    Hope it at least gives you an idea of how i was thinking of doing it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width