Murchu Posted February 18, 2013 Share Posted February 18, 2013 Hi friends basic question for the community I want to generate a checkered flag but im unsure how to achieve this with for loops ? each box is of side 40 and width over all = $n any help would be great thanks $height = $n * 40; $width = $height; $im = imagecreatetruecolor($width, $height); $white = imagecolorallocate($im, 255, 255, 255); $red = imagecolorallocate($im, 255, 0, 0); for($i = 0; $i <= $width; $i += 40) { for($j = 0; $j <= $n; $j += $n) { if(( $i + $j ) % 2 == 0) { imagefilledrectangle ( $im , 0, $j, 0 , $width , $red ); } else { imagefilledrectangle ( $im , $j, $j , 0 , $width , $white ); } } } header('Content-type: image/png'); imagepng($im); imagedestroy($im); Quote Link to comment https://forums.phpfreaks.com/topic/274609-looping-png-coordinates/ Share on other sites More sharing options...
Murchu Posted February 18, 2013 Author Share Posted February 18, 2013 overall = $n * $n sorry Quote Link to comment https://forums.phpfreaks.com/topic/274609-looping-png-coordinates/#findComment-1413035 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 try $n = 6; $height = $n * 40; $width = $height; $im = imagecreatetruecolor($width, $height); $white = imagecolorallocate($im, 255, 255, 255); $red = imagecolorallocate($im, 255, 0, 0); for($i = 0; $i < $n; $i++) { for($j = 0; $j < $n; $j++) { $c = ( $i + $j ) % 2 == 0 ? $red : $white; imagefilledrectangle ( $im , $i*40, $j*40, ($i+1)*40 , ($j+1)*40 , $c ); } } header('Content-type: image/png'); imagepng($im); imagedestroy($im); Quote Link to comment https://forums.phpfreaks.com/topic/274609-looping-png-coordinates/#findComment-1413041 Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 Why not do one big rectangle of one color, then you only have to do the other color's squares? Quote Link to comment https://forums.phpfreaks.com/topic/274609-looping-png-coordinates/#findComment-1413044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.