Location: PHPKode > scripts > Space maps > space-maps/map.php
<?
//BSD LICENSE
//Copyright (c) 2008, Danilo Moncastro <hide@address.com>
//All rights reserved.

//	
//	STAR MAP GENERATOR
//


// check README and index.php for more info
//


class starmap{
	public $sectors;
	public $font;
	public $havegates;
	public $gatesdistance;
	public $height;
	public $width;
	public $axis;
	private $image;
	private $preto;
	private $cinza;
	private $branco;
	private $systems = array();

	function createMap(){
		$this->image = imagecreatetruecolor($this->height, $this->width);
		$this->preto = imagecolorallocate($this->image, 0, 0, 0);
		$this->branco = imagecolorallocate($this->image, 255, 255, 255);
		$this->cinza = imagecolorallocate($this->image, 105, 105, 105);
		$this->createSectors();
		if($this->havegates === true)
		$this->createGates();
		imagettftext($this->image, 15, 0, $this->width-150, 20, $this->branco, $this->font, "Setores: $this->sectors");
	}

	function createSectors(){
		$i = 1;
		while($i <= $this->sectors){
			while($this->checkSystems($i) === false){
			}
			$i++;
		}
		foreach ($this->systems as $n=>$s){
			imageellipse($this->image, $s['nx'], $s['ny'], 5, 5, $this->branco);
			imageellipse($this->image, $s['nx'], $s['ny'], 8, 8, $this->branco);
			imagettftext($this->image, 10, 0, $s['nx']+6, $s['ny']+4, $this->branco, $this->font,$n);
			$i = 1;
			while($i < $this->sectors){
				$this->systems[$n]['distance'][$i] = $this->getDistance($n, $i);
				$i++;
			}
		}
	}
	function createGates(){
		foreach($this->systems as $n=>$s){
			foreach($s['distance'] as $num => $dis ){
				if($dis < $this->gatesdistance){
					$sx = $this->getNCords($n);
					$sy = $this->getNCords($num);
					if($n < $num){
						imagedashedline($this->image, $sx['nx'], $sx['ny'], $sy['nx'], $sy['ny'], $this->branco);
					}
				}
			}
		}
	}
	function showMap(){
		imagejpeg($this->image);
	}
	function checkSystems($i){
		$x = rand(0,$this->width);
		$y = rand(0,$this->height);
		$z = rand(0,$this->axis);
		if($z > $this->axis/2){
			$nx = $x-ceil($z/4);
			$ny = $y-ceil($z/4);
		}elseif($z <= $this->axis/2){
			$nx = $x+floor($z/2);
			$ny = $y+floor($z/2);
		}
		if(($nx > $this->width-25) || $nx < 25)
		return false;
		if(($ny > $this->height-25) || $ny < 50)
		return false;
		foreach($this->systems as $one){
			if(($one['nx'] <= $nx+8 && $one['nx'] >= $nx) || ($one['nx'] >= $nx-8 && $one['nx'] <= $nx) || ($one['ny'] <= $ny+8 && $one['ny'] >= $ny) || ($one['ny'] >= $ny-8 && $one['ny'] <= $ny))
			return false;

		}
		$this->systems[$i] = array("x"=>$x, "y"=>$y, "z"=>$z, "ny"=>$ny, "nx"=>$nx);
		return true;
	}
	function getDistance($x, $y){
		$x = $this->systems[$x];
		$y = $this->systems[$y];
		$xt = $x['x']-$y['x'];
		$yt = $x['y']-$y['y'];
		$zt = $x['z']-$y['z'];
		if($xt < 0)$xt = -1*$xt;
		if($yt < 0)$yt = -1*$yt;
		if($zt < 0)$zt = -1*$zt;
		$total = $xt+$yt+$zt;
		return $total;
	}
	function getNCords($x){
		return array("nx"=>$this->systems[$x]['nx'],"ny"=>$this->systems[$x]['ny']);
	}
}
Return current item: Space maps