I originally wrote this in July 2006 for PHP image generation practice, but decided to repost it. Basically, it’s code written in the shape of a heart, that draws a heart.
Going more in-depth, the code uses polar coordinates to draw a cardioid-like figure, which are converted into cartesian coordinates via the cfp_x() and cfp_y() functions. The new coordinates are then used in PHP’s imageFilledPolygon() function to draw the shape.
The formula to create the heart is simply done by setting the radius equal to the angle, rotated 90 degrees so that the shape is right-side up. So, at 0, the coordinates are at the origin – the sharp valley of the heart. At 180, the coordinates are at the bottom point of the heart.
View the output here.
function cfp_x($ox,
$a,$d) {return ($ox+ round((cos (deg2rad($a
))*$d)));}function cfp_y($oy, $a, $d){return ($oy+
round((sin(deg2rad($a)) * $d)));} $crd=array();$ct=0;
$im=imageCreateTrueColor(250,250);$c=imageColorAllocate
($im,255,0,0);header("Content-type: image/png");for($i
=-180;$i<180;$i++){$crd[]=cfp_x(125,$i -90,abs($i));
$crd[] = cfp_y(50, $i - 90, abs($i) ); $ct++;}
imageFilledPolygon($im, $crd,$ct,$c);
imagePNG($im);imageDestroy
(($im));
?>
Unobfuscated version:
function cfp_x($origin_x, $angle, $radius) {
return $origin_x + round(cos(deg2rad($angle)) * $radius);
}
function cfp_y($origin_y, $angle, $radius) {
return $origin_y + round(sin(deg2rad($angle)) * $radius);
}
$coords = array();
$num_pts = 0;
$img = imageCreateTrueColor(250, 250);
for ($i = -180; $i < 180; $i++) {
$coords[] = cfp_x(125, $i - 90, abs($i));
$coords[] = cfp_y(50, $i - 90, abs($i));
$num_pts++;
}
imageFilledPolygon($img, $coords, $num_pts, 0xFF0000);
header('Content-type: image/png');
imagePNG($img);
imageDestroy($img);