marek_mar
01-23-2006, 01:34 AM
I was playing around and made this... but it can be usefull... if you'll ever need to do such things. ;)
Ah yes descritption.
It'll draw pretty much anything as long as you give coordinates.
It'll give you the length of the line and return the point at it's middle.
For polygons it'll return the area (with the exception that the polygion intersects with itself) and perimeter.
An example is given below.
<?php
class point
{
public $x = 0,
$y = 0;
function __construct($x = 0, $y = 0)
{
$this->set($x, $y);
}
function set($x, $y)
{
if(!is_nan($x) && !is_nan($y))
{
$this->x = $x;
$this->y = $y;
return true;
}
return false;
}
function pos()
{
return array($this->x, $this->y);
}
function move_by($vector)
{
if(!$vector instanceof vector)
{
return false;
}
return $this->set($this->x + $vector->x, $this->y + $vector->y);
}
}
class line
{
public $points = array();
public $length = 0;
function __construct($a = false, $b = false)
{
return $this->set($a, $b);
}
function set($a, $b)
{
if($a instanceof point && $b instanceof point)
{
$this->points[] = $a;
$this->points[] = $b;
$this->length();
return true;
}
return false;
}
private function length()
{
$this->length = sqrt((pow($this->points[1]->x - $this->points[0]->x, 2)) + (pow($this->points[1]->y - $this->points[0]->y, 2)));
}
function middle()
{
return new point(($this->points[0]->x - $this->points[1]->x) / 2, ($this->points[0]->y - $this->points[1]->y) / 2);
}
}
class vector
{
public $x,
$y;
function __construct($x, $y)
{
$this->set($x, $y);
}
function set($x, $y)
{
$this->x = (int) $x;
$this->y = (int) $y;
}
}
class polygon
{
public $points = array();
public $area = 0;
public $perimeter = 0;
function __construct()
{
$n = func_num_args();
if($n < 3)
{
return false;
}
for($i = 0; $i < $n; $i++)
{
if(func_get_arg($i) instanceof point)
{
$this->points[] = func_get_arg($i);
}
}
$this->area();
$this->perimeter();
}
private function area()
{
$area = 0;
for($i = 0, $n = count($this->points) - 1; $i < $n; $i++)
{
$area += $this->points[$i]->x * $this->points[$i + 1]->y - $this->points[$i + 1]->x * $this->points[$i]->y;
}
$area += $this->points[$n]->x * $this->points[0]->y - $this->points[0]->x * $this->points[$n]->y;
$area /= 2;
$area = abs($area);
$this->area = $area;
}
private function perimeter()
{
$perimeter = 0;
for($i = 0, $n = count($this->points) - 1; $i < $n; $i++)
{
$side = new line($this->points[$i], $this->points[$i + 1]);
$perimeter += $side->length;
// var_dump($perimeter);
}
$side = new line($this->points[$n], $this->points[0]);
$perimeter += $side->length;
$this->perimeter = $perimeter;
}
function move_by($vector)
{
if(!$vector instanceof vector)
{
return false;
}
for($i = 0, $n = count($this->points); $i < $n; $i++)
{
$this->points[$i]->set($this->points[$i]->x + $vector->x, $this->points[$i]->y + $vector->y);
}
return true;
}
}
class grid
{
private $size = array(),
$data = array(),
$im = null;
function __construct($min_x, $max_x, $min_y, $max_y, $scale_x, $scale_y)
{
$this->size['min'] = array($min_x, $max_y);
$this->size['max'] = array($max_x, $min_y);
$this->size['scale'] = array($scale_x, $scale_y);
$this->size[0] = array($scale_x * $min_x, $scale_y * $max_y);
return $this->create_image();
}
private function create_image()
{
$this->im = imagecreate(1 + ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], 1 + ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1]);
$back = imagecolorallocatealpha($this->im, 255, 255, 255, 127); // transparent.
return true;
}
function plot($obj, $color = false)
{
if(!$color)
{
$color = imagecolorallocate($this->im, 0, 0, 0);
}
switch(get_class($obj))
{
case 'point':
imagefilledellipse($this->im, $this->size[0][0] + ($obj->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->y * $this->size['scale'][1]), 3, 3, $color);
break;
case 'line':
imageline($this->im, $this->size[0][0] + ($obj->points[0]->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->points[0]->y * $this->size['scale'][1]), $this->size[0][0] + ($obj->points[1]->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->points[1]->y * $this->size['scale'][1]), $color);
break;
case 'polygon':
$data = array();
for($i = 0, $n = count($obj->points); $i < $n; $i++)
{
$data[] = $this->size[0][0] + ($obj->points[$i]->x * $this->size['scale'][0]);
$data[] = $this->size[0][1] - ($obj->points[$i]->y * $this->size['scale'][1]);
}
imagefilledpolygon($this->im, $data, $n, $color);
break;
}
}
function plot_axis()
{
$black = imagecolorallocate($this->im, 0, 0, 0);
imageline($this->im, $this->size['min'][0] * $this->size['scale'][0], 0, $this->size['min'][0] * $this->size['scale'][0], ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1], $black);
imageline($this->im, 0, $this->size['min'][1] * $this->size['scale'][1], ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], $this->size['min'][1] * $this->size['scale'][1], $black);
}
function func($eval = '$x', $percision = 10, $color = false)
{
if(!$color)
{
$color = imagecolorallocate($this->im, 0, 0, 0);
}
if(substr($eval, -1) != ';')
{
$eval .= ';';
}
for($x = -1 * ($this->size['min'][0] + $this->size['max'][0]), $n = ($this->size['min'][0] + $this->size['max'][0]) * $percision; $x < $n; $x += 1 / $percision)
{
$points[] = array($this->size[0][0] + ($x * $this->size['scale'][0]), $this->size[0][1] - (eval('return ' . $eval) * $this->size['scale'][1]));
}
for($i = 0, $n = count($points) - 1; $i < $n; $i++)
{
imageline($this->im, $points[$i][0], $points[$i][1], $points[$i + 1][0], $points[$i + 1][1], $color);
}
}
function make_image($dest = false)
{
if(!$dest)
{
header('Content-Type: image/png');
return imagepng($this->im);
}
return imagepng($this->im, $dest);
}
function color($r, $g, $b, $alpha)
{
return imagecolorallocatealpha($this->im, $r, $g, $b, $alpha);
}
}
?>
Example:
<?php
$poly = new polygon(
new point(0, 0),
new point(0, 1),
new point(1, 2),
new point(2, 2),
new point(2, 3),
new point(3, 4),
new point(4, 4),
new point(5, 3),
new point(5, 0),
new point(4, 0),
new point(4, 3),
new point(3, 3),
new point(3, 2),
new point(2, 1),
new point(1, 1)
);
$point = new point(4, 4);
$line = new line(new point(-2, 2), new point(4, 5));
$poly2 = new polygon(
new point(0, 0),
new point(0, 4),
$point,
new point(4, 0),
new point(3, 0),
new point(3, 3),
new point(1, 3),
new point(1, 0)
);
$vector = new vector(-5, 3);
$grid = new grid(10, 10, 10, 10, 20, 20);
$grid->plot_axis();
$grid->plot($point);
$grid->plot($poly, $grid->color(100, 200, 100, 0));
$poly->move_by($vector);
$grid->func('pow($x, 2) + $x', 10);
$grid->make_image(/*'C:/test.png'*/);
It can produce an image like the one attached (PNG not GIF but png isn't allowed here for some reason).
Ah yes descritption.
It'll draw pretty much anything as long as you give coordinates.
It'll give you the length of the line and return the point at it's middle.
For polygons it'll return the area (with the exception that the polygion intersects with itself) and perimeter.
An example is given below.
<?php
class point
{
public $x = 0,
$y = 0;
function __construct($x = 0, $y = 0)
{
$this->set($x, $y);
}
function set($x, $y)
{
if(!is_nan($x) && !is_nan($y))
{
$this->x = $x;
$this->y = $y;
return true;
}
return false;
}
function pos()
{
return array($this->x, $this->y);
}
function move_by($vector)
{
if(!$vector instanceof vector)
{
return false;
}
return $this->set($this->x + $vector->x, $this->y + $vector->y);
}
}
class line
{
public $points = array();
public $length = 0;
function __construct($a = false, $b = false)
{
return $this->set($a, $b);
}
function set($a, $b)
{
if($a instanceof point && $b instanceof point)
{
$this->points[] = $a;
$this->points[] = $b;
$this->length();
return true;
}
return false;
}
private function length()
{
$this->length = sqrt((pow($this->points[1]->x - $this->points[0]->x, 2)) + (pow($this->points[1]->y - $this->points[0]->y, 2)));
}
function middle()
{
return new point(($this->points[0]->x - $this->points[1]->x) / 2, ($this->points[0]->y - $this->points[1]->y) / 2);
}
}
class vector
{
public $x,
$y;
function __construct($x, $y)
{
$this->set($x, $y);
}
function set($x, $y)
{
$this->x = (int) $x;
$this->y = (int) $y;
}
}
class polygon
{
public $points = array();
public $area = 0;
public $perimeter = 0;
function __construct()
{
$n = func_num_args();
if($n < 3)
{
return false;
}
for($i = 0; $i < $n; $i++)
{
if(func_get_arg($i) instanceof point)
{
$this->points[] = func_get_arg($i);
}
}
$this->area();
$this->perimeter();
}
private function area()
{
$area = 0;
for($i = 0, $n = count($this->points) - 1; $i < $n; $i++)
{
$area += $this->points[$i]->x * $this->points[$i + 1]->y - $this->points[$i + 1]->x * $this->points[$i]->y;
}
$area += $this->points[$n]->x * $this->points[0]->y - $this->points[0]->x * $this->points[$n]->y;
$area /= 2;
$area = abs($area);
$this->area = $area;
}
private function perimeter()
{
$perimeter = 0;
for($i = 0, $n = count($this->points) - 1; $i < $n; $i++)
{
$side = new line($this->points[$i], $this->points[$i + 1]);
$perimeter += $side->length;
// var_dump($perimeter);
}
$side = new line($this->points[$n], $this->points[0]);
$perimeter += $side->length;
$this->perimeter = $perimeter;
}
function move_by($vector)
{
if(!$vector instanceof vector)
{
return false;
}
for($i = 0, $n = count($this->points); $i < $n; $i++)
{
$this->points[$i]->set($this->points[$i]->x + $vector->x, $this->points[$i]->y + $vector->y);
}
return true;
}
}
class grid
{
private $size = array(),
$data = array(),
$im = null;
function __construct($min_x, $max_x, $min_y, $max_y, $scale_x, $scale_y)
{
$this->size['min'] = array($min_x, $max_y);
$this->size['max'] = array($max_x, $min_y);
$this->size['scale'] = array($scale_x, $scale_y);
$this->size[0] = array($scale_x * $min_x, $scale_y * $max_y);
return $this->create_image();
}
private function create_image()
{
$this->im = imagecreate(1 + ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], 1 + ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1]);
$back = imagecolorallocatealpha($this->im, 255, 255, 255, 127); // transparent.
return true;
}
function plot($obj, $color = false)
{
if(!$color)
{
$color = imagecolorallocate($this->im, 0, 0, 0);
}
switch(get_class($obj))
{
case 'point':
imagefilledellipse($this->im, $this->size[0][0] + ($obj->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->y * $this->size['scale'][1]), 3, 3, $color);
break;
case 'line':
imageline($this->im, $this->size[0][0] + ($obj->points[0]->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->points[0]->y * $this->size['scale'][1]), $this->size[0][0] + ($obj->points[1]->x * $this->size['scale'][0]), $this->size[0][1] - ($obj->points[1]->y * $this->size['scale'][1]), $color);
break;
case 'polygon':
$data = array();
for($i = 0, $n = count($obj->points); $i < $n; $i++)
{
$data[] = $this->size[0][0] + ($obj->points[$i]->x * $this->size['scale'][0]);
$data[] = $this->size[0][1] - ($obj->points[$i]->y * $this->size['scale'][1]);
}
imagefilledpolygon($this->im, $data, $n, $color);
break;
}
}
function plot_axis()
{
$black = imagecolorallocate($this->im, 0, 0, 0);
imageline($this->im, $this->size['min'][0] * $this->size['scale'][0], 0, $this->size['min'][0] * $this->size['scale'][0], ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1], $black);
imageline($this->im, 0, $this->size['min'][1] * $this->size['scale'][1], ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], $this->size['min'][1] * $this->size['scale'][1], $black);
}
function func($eval = '$x', $percision = 10, $color = false)
{
if(!$color)
{
$color = imagecolorallocate($this->im, 0, 0, 0);
}
if(substr($eval, -1) != ';')
{
$eval .= ';';
}
for($x = -1 * ($this->size['min'][0] + $this->size['max'][0]), $n = ($this->size['min'][0] + $this->size['max'][0]) * $percision; $x < $n; $x += 1 / $percision)
{
$points[] = array($this->size[0][0] + ($x * $this->size['scale'][0]), $this->size[0][1] - (eval('return ' . $eval) * $this->size['scale'][1]));
}
for($i = 0, $n = count($points) - 1; $i < $n; $i++)
{
imageline($this->im, $points[$i][0], $points[$i][1], $points[$i + 1][0], $points[$i + 1][1], $color);
}
}
function make_image($dest = false)
{
if(!$dest)
{
header('Content-Type: image/png');
return imagepng($this->im);
}
return imagepng($this->im, $dest);
}
function color($r, $g, $b, $alpha)
{
return imagecolorallocatealpha($this->im, $r, $g, $b, $alpha);
}
}
?>
Example:
<?php
$poly = new polygon(
new point(0, 0),
new point(0, 1),
new point(1, 2),
new point(2, 2),
new point(2, 3),
new point(3, 4),
new point(4, 4),
new point(5, 3),
new point(5, 0),
new point(4, 0),
new point(4, 3),
new point(3, 3),
new point(3, 2),
new point(2, 1),
new point(1, 1)
);
$point = new point(4, 4);
$line = new line(new point(-2, 2), new point(4, 5));
$poly2 = new polygon(
new point(0, 0),
new point(0, 4),
$point,
new point(4, 0),
new point(3, 0),
new point(3, 3),
new point(1, 3),
new point(1, 0)
);
$vector = new vector(-5, 3);
$grid = new grid(10, 10, 10, 10, 20, 20);
$grid->plot_axis();
$grid->plot($point);
$grid->plot($poly, $grid->color(100, 200, 100, 0));
$poly->move_by($vector);
$grid->func('pow($x, 2) + $x', 10);
$grid->make_image(/*'C:/test.png'*/);
It can produce an image like the one attached (PNG not GIF but png isn't allowed here for some reason).