The code below creates a simple picture showing 50 telephone poles lining a country road. The purpose of the graph is to show that both the width and height of the items in a perspective picture change at the rate of 1/x. Since both the width and height change at the same rate, it appears that the lines in the picture generate from a vanishing point on the horizon. The picture is used in the article The Rediscovery of Perspective.
Notice how the height of the second pole is 1/2 that of the first. The third pole is 1/3 the first and so on.
<?php
/** This PHP code makes a PNG image that symbolizes telephone poles lining a country road.
*
* Author: Kevin Delaney
* project http://y-intercept.com/rich/rediscovery.html
* date: 6/11/2008
*
* In computer graphics the upper left corner is (0,0). I wish to do the calculations from
* a point near the center of the graphics. So, I will define the width, height and calculate
* the center point (I want to the picture to a bit below the center of the image.
*/
define('IMAGE_WIDTH',440);
define('IMAGE_HEIGHT',300);
define('POLE_WIDTH',6);
define('SKY_PCT', .6); // 70% of the picture will be sky.
define('IMAGE_PADDING',15); // number of pixels for the image margin.
$centerX = floor(IMAGE_WIDTH/2);
$centerY = floor(IMAGE_HEIGHT * SKY_PCT);
// lenght of the upper and lower portion of the telephone pole.
$upperPole = floor(IMAGE_HEIGHT * SKY_PCT) - IMAGE_PADDING;
$lowerPole = floor(IMAGE_HEIGHT * (1-SKY_PCT)) - IMAGE_PADDING;
// the variable roadwidth is used for the x position of the poles.
// It is one half of the display area.
$roadWidth = floor(IMAGE_WIDTH/2) - IMAGE_PADDING;
// This array defines a triangle which will be used for the
// the contry road.
header ("Content-type: image/png");
$im = @imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT)
or die('Cannot Initialize new GD image stream');
// define the colors for the pallette.
$sky_color = imagecolorallocate($im, 200, 200, 255);
$grass_color = imagecolorallocate($im, 120, 233, 120);
$pole_color = imagecolorallocate($im, 200, 28, 90);
//$text_color = imagecolorallocate($im, 14, 80, 50);
$line_color = imagecolorallocate($im, 14, 14, 233);
$road_color = imagecolorallocate($im, 120, 100, 180);
// Set background colors.
imagefilledrectangle($im, 0, $centerY, IMAGE_WIDTH-1, IMAGE_HEIGHT-1, $grass_color);
$roadArray = array($centerX, $centerY,
IMAGE_PADDING * 2, IMAGE_HEIGHT-1,
(IMAGE_WIDTH - (IMAGE_PADDING * 2) -1), IMAGE_HEIGHT);
imagefilledpolygon($im, $roadArray, 3, $road_color);
imagefilledrectangle($im, 0, 0, IMAGE_WIDTH, $centerY, $sky_color);
// This code will calculate the position of 20 telephone poles
// draw the telephone poles.
// To avoid a div zero error, I start at 1.
for ($i=1; $i < 50; $i++) {
$pTop = ceil($upperPole/$i);
$pBottom = ceil($lowerPole/$i);
$px = ceil($roadWidth/$i);
$pWidth = ceil(POLE_WIDTH/$i);
// the pole on the left side of the image
imagefilledrectangle ($im , $centerX - $px , $centerY - $pTop , $centerX - $px + $pWidth , $centerY + $pBottom , $pole_color );
imagefilledrectangle ($im , $centerX + $px , $centerY - $pTop , $centerX + $px + $pWidth , $centerY + $pBottom , $pole_color );
//imageline($im, 20, 70, $polePos, 10, $line_color);
//imageline($im, 20, 70, $polePos, 100, $line_color);
}
imagepng($im);
imagedestroy($im);
?>index - y-intercept - Salt Lake - sponsors