#!/usr/bin/perl -w

# This program prints a GIF to standard output.  

# On a Unix system, you can redirect this program (call it cardioid)
# to a file as follows:
#
# cardioid > cardioid.gif

use GD;
use constant two_pi => 6.28318530717959;

# Create a 200 x 200 GIF, all white.
#
$image = new GD::Image(200, 200);
$image->colorAllocate(255, 255, 255);

# Define the color for our figure.
#
$black = $image->colorAllocate(0,0,0);

# Increment $theta from 0 to two_pi.
#
for ($theta = 0; $theta <= two_pi; $theta += .001) {
    $r = 50 * (1 + cos($theta));

    # Convert our polar coordinates to Cartesian, and plot.
    #
    $x = $r * cos($theta);
    $y = $r * sin($theta);
    $image->setPixel($x + 50, $y + 100, $black);
}

print $image->gif;
