Jump to content

filled polygon is not renders 129th time in same image


Swan

Recommended Posts

I have a function which creates a polygon of blue color. I call this function inside a loop

of 128 iterations. For every iteration, i change the value of x and y so that my polygon is

rendered on new location. This goes fine and i get 128 new polygons on my image after

running the script. But if change the number to 129, the 129th polygon is transparent or

what i cannot guess.It  just does not display or render at all. Is this a bug or some

mistake with my code? ???

I am using php version 5.1.4 that came with WAMP5 Version 1.6.3

 

my code is -

 

<?php

$im = imagecreate(600, 400);

$x = 20;

$y = 350;

for($i=1;$i<=128;$i++) //create a polygon 128 times

{

drawPoly($x,$y,$im);

$x = $x+2;

$y = $y-2;

}

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

function drawPoly($xCoord,$yCoord,$im)

{

$x1 = $x2 = $xCoord;

$x3 = $x4 = $xCoord + 70;

$y1 = $y4 = $yCoord;

$y2 = $y3 = $yCoord + 30;

$Poly = array(0  => $x1,1  => $y1,2  => $x2,3  => $y2,4  => $x3,5  => $y3,6

 

=> $x4,7  => $y4);

$bg  = imagecolorallocate($im, 150, 150, 150);

$blue = imagecolorallocate($im, 0, 0, 255);

imagefilledpolygon($im, $Poly, 4, $blue);

}

?>

 

Please help me

well i am working on something where i need to create number of polygons depending on the array passed by the user. If the array count is 5, then 5 polygons should be created, if array count is 129, 129 polygons should be created. Then in each polygone, i am displaying the value of the array.

The issue is solved. It was the problem with the code.

 

i took this line  $bg  = imagecolorallocate($im, 150, 150, 150); outside the function drawPoly(), and everything worked as expected.

 

the new updated code is -

 

<?php

$im = imagecreate(600, 400);

$bg  = imagecolorallocate($im, 150, 150, 150);

$x = 20;

$y = 350;

for($i=1;$i<=150;$i++) //create a polygon 128 times

{

drawPoly($x,$y,$im);

$x = $x+2;

$y = $y-2;

}

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

function drawPoly($xCoord,$yCoord,$im)

{

$x1 = $x2 = $xCoord;

$x3 = $x4 = $xCoord + 70;

$y1 = $y4 = $yCoord;

$y2 = $y3 = $yCoord + 30;

$Poly = array(0  => $x1,1  => $y1,2  => $x2,3  => $y2,4  => $x3,5  => $y3,6  => $x4,7  => $y4);

$blue = imagecolorallocate($im, 0, 0, 255);

imagefilledpolygon($im, $Poly, 4, $blue);

}

?>

 

But i still want to know why this happened and why it worked when i took that line outside the function?

 

When you create an image with imagecreate() it creates a paletted image with a 256 color pallette.

 

If each time you you allocate a background and foregraound color you run out after 128.

 

Define the the colors once, instead of defining blue 128 times, and use them for each polygon and you can have as many as you want.

and still my question stays ? :D

I mean when the direction of the polygon is hard-cored inside the the function $x+2 and $y-2 to alocate 128 polygons when u can make the loop to get the last points so u can draw 1 polygon  ?

 

What are you talking about? They want to draw 129 polygons on an image, not 1.

ok i will explain even more .

 

whit that function whit hard-cored increment it doest mather if you draw 128 polygons ...

You need 4 point to draw the polygon so you have the 2 points from the start where you havent started the loop

and you cant get the other two needed after looping and saving the end points .

example: whit 128 polygons :

-

  -

    -

    -

result

-----

and drawing 1 polygon after the loop

-----

i didn thied it but this s more logical...

   

ok i will explain even more .

 

whit that function whit hard-cored increment it doest mather if you draw 128 polygons ...

You need 4 point to draw the polygon so you have the 2 points from the start where you havent started the loop

and you cant get the other two needed after looping and saving the end points .

example: whit 128 polygons :

-

  -

    -

    -

result

-----

and drawing 1 polygon after the loop

-----

i didn thied it but this s more logical...

   

 

He is using a function to draw the polygons inside the loop. You may just be a little confused because his function comes after his loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.