How do I nest this for loop?

gurung

I am trying to create an image with a pattern of a repeated circle. I am doing this in PHP with GD. So far I have been able to tile the circle in horizontal manner (x-axis) but am unable to tile it in the vertical(y-axis). Here is an example image.

enter image description here

Below is the code that created the above image :

$width = 1000; 
$height = 500;

$image_p = imagecreatetruecolor($width, $height); 
$color = imagecolorallocate($image_p, 0, 255, 0); 

    for ($i = 0; $i <= 10; $i++){
        if ($i % 2 !== 0){ //only if odd numbers
        imagefilledellipse ($image_p, 50 * $i, 50, 100, 100, $color);
        }   

    }

imagejpeg($image_p, uniqid() .'.jpg');

My guess is that in order to tile each circle in a vertical manner it just needs another nested for loop and it would be similar to one already there except the change in y-axis like so :

imagefilledellipse ($image_p, 50, 50 * $i, 100, 100, $color);

I have tried a lot of nesting variation but could not get it to work. Please help.

Tim

The function imagefilledellipse has the following signature (I suppose):

imagefilledellipse(image, x, y, width, height, color)

Which means that you are drawing for every i in 0 < i < 10 a circle with a different x position.

Swap it with the y parameter to draw vertical circles:

$width = 1000; 
$height = 500;

$image_p = imagecreatetruecolor($width, $height); 
$color = imagecolorallocate($image_p, 0, 255, 0); 

    for ($i = 0; $i <= 10; $i++){
        if ($i % 2 !== 0){ //only if odd numbers
        imagefilledellipse ($image_p, 50, 50 * $i, 100, 100, $color);
        }   

    }

imagejpeg($image_p, uniqid() .'.jpg');

In order to draw both horizontal and vertical circles you will need indeed, as you said, a nested for-loop:

$width = 1000; 
$height = 500;

$image_p = imagecreatetruecolor($width, $height); 
$color = imagecolorallocate($image_p, 0, 255, 0); 

    for ($i = 0; $i <= 10; $i++){
        for ($j = 0; $j <= 10; $j++) {
            if ($i % 2 !== 0 && $j % 2 !== 0) { //only if odd numbers
                imagefilledellipse ($image_p, 50 * $i, 50 * $j, 100, 100, $color);
            }   
        }
    }

imagejpeg($image_p, uniqid() .'.jpg');

Also, you do not need to check for odd numbers if you would change the scale from i * 50 to 50 + i * 100, like this:

imagefilledellipse ($image_p, 50 + 100 * $i, 50 + 100 * $j, 100, 100, $color);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I nest syntax highlighting in Vim?

From Dev

How do I nest this LINQ query?

From Dev

How Do I Nest Meteor Spacebars Templates?

From Dev

How do I nest quotes four deep?

From Dev

How do I nest a JSON array?

From Dev

PHP - How can I nest a while loop inside an if isset condition?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

How do I nest and/or statements in a CSS media query?

From Dev

How do I property roll up and/or combinations in NEST?

From Dev

How Do I View the REST JSON created by the NEST API?

From Dev

How do I specify an index for Elasticsearch using NEST?

From Dev

how do i add dynamic highlight fields in nest for elasticsearch?

From Dev

How do I properly nest serializers in Django REST Framework?

From Dev

How do I nest polymer layouts to fill complete page?

From Dev

How do I nest two controllers in an AngularJS page?

From Dev

AngularJS : How do I nest a view within a parent state with parameters?

From Dev

How do I properly nest while and for loops in javascript

From Dev

How do I nest and/or statements in a CSS media query?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

ElasticSearch and NEST - How do I construct a simple OR query?

From Dev

AngularJS : How do I nest a view within a parent state with parameters?

From Dev

How do I accurately represent this ElasticSearch query using NEST?

From Dev

How do I nest products & change the display in Woocommerce?

From Dev

How do I nest a Greensock timeline in multiple Greensock timelines?

From Dev

How do I nest parameter expansions for uppercasing and substitution in Bash?

From Dev

How do I nest multiple ng-if properly?

From Java

How to nest a "for loop" n times?

From Dev

How do I refactor this loop?

From Dev

How do I refactor this loop?

Related Related

  1. 1

    How do I nest syntax highlighting in Vim?

  2. 2

    How do I nest this LINQ query?

  3. 3

    How Do I Nest Meteor Spacebars Templates?

  4. 4

    How do I nest quotes four deep?

  5. 5

    How do I nest a JSON array?

  6. 6

    PHP - How can I nest a while loop inside an if isset condition?

  7. 7

    How do I nest an array of items dividing them by their first value?

  8. 8

    How do I nest and/or statements in a CSS media query?

  9. 9

    How do I property roll up and/or combinations in NEST?

  10. 10

    How Do I View the REST JSON created by the NEST API?

  11. 11

    How do I specify an index for Elasticsearch using NEST?

  12. 12

    how do i add dynamic highlight fields in nest for elasticsearch?

  13. 13

    How do I properly nest serializers in Django REST Framework?

  14. 14

    How do I nest polymer layouts to fill complete page?

  15. 15

    How do I nest two controllers in an AngularJS page?

  16. 16

    AngularJS : How do I nest a view within a parent state with parameters?

  17. 17

    How do I properly nest while and for loops in javascript

  18. 18

    How do I nest and/or statements in a CSS media query?

  19. 19

    How do I nest an array of items dividing them by their first value?

  20. 20

    ElasticSearch and NEST - How do I construct a simple OR query?

  21. 21

    AngularJS : How do I nest a view within a parent state with parameters?

  22. 22

    How do I accurately represent this ElasticSearch query using NEST?

  23. 23

    How do I nest products & change the display in Woocommerce?

  24. 24

    How do I nest a Greensock timeline in multiple Greensock timelines?

  25. 25

    How do I nest parameter expansions for uppercasing and substitution in Bash?

  26. 26

    How do I nest multiple ng-if properly?

  27. 27

    How to nest a "for loop" n times?

  28. 28

    How do I refactor this loop?

  29. 29

    How do I refactor this loop?

HotTag

Archive