Messing with GD::Image

I spent the other day tweaking GD::Image to plot the I quadrant of a cartesian plane for me. I learned some basic things about Perl again, so I thought I would share them.

Plotting the Square Root

So the issue with this was that it was upside down. This is due to GD–and just about every other image creation and manipulation program–considering the top left corner to be (0,0). X corresponds to the width and Y corresponds to the height. We fix this by mapping the Y to the $height – ($x ** 2). The next thing that needed overcome was widening the view of these points so it seems “zoomed in.” This was accomplished by multiplying each point in @x_point by 10 after calculating the @y_point from it.
Before multiplying x by 10
After multiplying x by 10 

Then, I was trying to connect each point with lines. The error I had lied in the fact that the order in which lines are connected matters. The default rule for sort is to sort by character alone. This left me connecting the points in a strange fashion. If the points were ordered by ASCII value…

I was trying to debug this, which generated some interesting pictures. I started by making sure the x coordinates were only going to my mapped origin (0, $height). This made a feather-like fractal I suppose. The next picture is an attempt which always plotted the same y coordinate but changed x coordinates. These were still being sorted in the wrong order. After that, I properly ordered the keys. And then the final product was me allowing the y coordinate to change too.


After that, I started playing with some random points. This was taken from the rand() function. The last two are different zoom factors.


Leave a Reply

Your email address will not be published. Required fields are marked *