Codedninja-PHP Posted November 23, 2008 Posted November 23, 2008 Well I was challenge by my teacher in school to a program off. I got the challenge done but, I see a little flaw. Well the example is at http://www.codedninja.com/programs/binary....binary=10110110 basically you put the binary you wanna show at the of the binary var. If you see it, if a number is doubled it has the extra line like 11001100 see how it has double 1's and 0's well I can fix it but I don't want the code to be really long. I would like it to be short simple. Well here is the code. <?php if (isset($_GET['binary'])) { header("Content-type: image/png"); $im = @imagecreate(500, 500) or die("Cannot Initialize new GD image stream"); $bgcolor = imagecolorallocate($im, 255, 255, 255); $color = imagecolorallocate($im, 0, 0, 0); imageline($im, 3, 3, 3, 497, $color); imageline($im, 3, 497, 497, 497, $color); $bits = str_split($_GET['binary']); $c = 20; foreach ($bits as $digit) { if ($digit == 0) { imageline($im, $c, 40, $c + 20, 40, $color); $c = $c + 20; } elseif ($digit == 1) { imageline($im, $c, 40, $c, 20, $color); imageline($im, $c, 20, $c + 20, 20, $color); imageline($im, $c + 20, 40, $c + 20, 20, $color); $c = $c + 20; } } imagepng($im); imagedestroy($im); } else { echo "Sorry, you didn't give the Binary var"; } ?> real waveform image: http://poscope.itss.com.co/images/ExtSampling-full.jpg Quote
Apache Posted November 24, 2008 Posted November 24, 2008 I'm not really that good with php script but one thing I see instantly is that in the image, you're treating binary as a 3bit system. It's like you have 0 as your base line, 1 as 1 and 0 as -1 and then returning to the base line after each bit. It would be better to have your baseline as 0 and only move up to 1 when you need to, rather than moving down to -1. For a binary model it make it look as though there are 3 values. Here's my two cent's worth anyway. Pseudocode: if bit is 0 and previous bit was nothing or 0 move line one step forward If bit is 0 and previous bit was 1 move line one step down then one step forward if bit is 1 and previous bit was nothing or 0 move line one step up then one step forward if bit is 1 and previous bit was 1 move line one step forward Simplifying this would then become: for bits if bit is 1 and previous bit was nothing or 0 move line one step up else if bit is 0 and previous bit was 1 move line one step down endif move line one step forward next I hope you can follow what I mean by this, I may not have explained it very well. Quote
adamzap Posted November 24, 2008 Posted November 24, 2008 The problem is that you are drawing vertical lines on each side of a '1' bit even if there is another '1' bit on the side of it. You have to check around the current position for a '0' bit before drawing a vertical line. This is a fixed version of your code. Hope that helps! This (code) is a more flexible version I wrote that might impress your teacher more ;) (reload mine a few times, the extra lines are random) Cool exercise! Quote
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.