Задача №.2: Random (Случайный) график

Статус
Закрыто для дальнейших ответов.

Volter9

defined('SURRENDER') or die(); // StarWars
Регистрация
27.05.2012
Сообщения
1 145
Название: Случайный График.

Задача: написать скрипт, который будет отображать случайный график.

Техническое Описание: График создается на GD, и рисуем график с случайным y-координатой и (Необяз.) сторокой в виде дебага.

[hr]

Я закончил.

index.php
Код:
<?
require("config.php");

$image = new Graphics(1000,400);

$image->setBgColor(0,0,0);
$image->setFgColor(255,255,255);

$previus;
for ($i = 0; $i <= $image->width; $i+= $image->width/20) {
$point1 = Point::point($i,rand(0,$image->height));

$image->setFgColor(0,255,0);
$image->drawString(Point::point($point1->x,$point1->y),"(".$point1->x.",".$point1->y.")",2);
$image->setFgColor(0,0,255);
$image->drawLine($point1,$previus);


$previus = $point1;
}

$image->render();

$image->destroy();
?>
config.php
Код:
<?
require_once("graphics.class.php");
?>
graphics.class.php
Код:
<?
class Point {
public $x;
public $y;

static function point($x,$y) {
$point = new Point($x,$y);

return $point;
}

function __construct($x,$y) {
$this->x = $x;
$this->y = $y;
}
}
class Graphics {
protected $context;

protected $bgColor;
protected $fgColor;

public $width;
public $height;

protected $data;

function __construct($width, $height) {
$this->width = $width;
$this->height = $height;

$this->context = imagecreate($this->width,$this->height);
}

function setBgColor($r,$g,$b) {
$this->bgColor = imagecolorallocate($this->context,$r,$g,$b);
}

function setFgColor ($r,$g,$b) {
$this->fgColor = imagecolorallocate($this->context,$r,$g,$b);
}

function drawString($coord,$str,$size = 1) {
imagestring($this->context, $size,$coord->x, $coord->y,$str, $this->fgColor);
}

function drawEllipse($coord, $width, $height) {
$x = $coord->x; $y = $coord->y;

imageellipse($this->context, $x, $y, $width, $height, $this->fgColor);
}

function drawLine($point1,$point2) {
$x = $point1->x; $x1 = $point2->x;
$y = $point1->y; $y1 = $point2->y;

imageline($this->context, $x, $y, $x1, $y1, $this->fgColor);
}

function render() {
ob_start();
imagepng($this->context);
$this->data = ob_get_contents();
ob_clean();

echo "<img src=\"data:img/png;base64,".base64_encode($this->data)."\" />";
// echo $this->data;
}

function fill($point) {
imagefill($this->context,$point->x,$point->y,$this->bgColor);
}

function destroy() {
imagedestroy($this->context);
}
}
?>
 

Вложения

Статус
Закрыто для дальнейших ответов.
Верх Низ