<?php
function drawPoint($image,$d,$w,$c)
{
	for($i=0;$i<count($d);$i+=2)
	{
		$ii=$i+1;
		if(isset($d[$i]) && isset($d[$ii]))
		{	
			imagefilledrectangle($image, $d[$i]-$w/2, $d[$ii]-$w/2, $d[$i]+$w/2, $d[$ii]+$w/2, $c);
		}
	}
	
}
function labelPoint($image,$d,$l,$w,$s,$c)
{
	$font = 'arial.ttf';
	for($i=0;$i<count($d);$i+=2)
	{
		$ii=$i+1;
		$i3=$i/2;
		if(isset($d[$i]) && isset($d[$ii]) && isset($l[$i3]))
		{	
		  // Add the text
		  imagettftext($image, $s, 0, $d[$i]+$w, $d[$ii]+$w, $c, $font, $l[$i3]);
		}
	}
	
}
function normalize($ang)
{
	if($ang < 0 && $ang >=-90)
	{
		$ang= 360 + $ang;	
	} else 	if($ang < 0 && $ang <=-90)
	{
		$ang= (360 +  $ang);	
	}  else 	if($ang > 0 && $ang > 90)
	{
		$ang= (180 -  $ang);	
	}
	
	return $ang;
}
function normalize2($ang)
{
	if($ang == 0)
	{
		$ang= 270+ $ang;	
	} else if($ang > 0 && $ang <=90)
	{
		$ang= 90+ $ang;	
	} else 	if($ang > 90 && $ang <180)
	{
		$ang= (270 -  $ang);	
	}  else 	if($ang > 0 && $ang >=180)
	{
		$ang= ($ang -90);	
	} else 	if($ang < 0 && $ang >=-90)
	{
		$ang= 270 - $ang;	
	} else 	if($ang <= 0 && $ang <=-90)
	{
		$ang=  -$ang - 90;	
	}
	return $ang;
}
function labelAngle($image,$d,$l,$w,$s,$c)
{
	$font = 'arial.ttf';
	$last=count($d) - 1;
	$scale=$d[$last];
	for($i=0;$i<count($d);$i+=2)
	{
		$ii=$i+1;
		$iii=$i+2;
		$iv=$i+3;
		if(isset($d[$i]) && isset($d[$ii]))
		{
		  if(isset($d[$iii]) && isset($d[$iv]))
		  {
			  $dx=$d[$i] -$d[$iii];
			  $dy=$d[$ii] -$d[$iv];
			  
			  $mx=($d[$i] +$d[$iii])/2;
			  $my=($d[$ii] +$d[$iv])/2;
		  }
		  else
		  {	
			  $dx=$d[$i] -$d[0];
			  $dy=$d[$ii] -$d[1];
  
			  $mx=($d[$i] +$d[0])/2;
			  $my=($d[$ii] +$d[1])/2;
		  }
			$dh= sqrt(pow($dy,2) + pow($dx,2));
			$ang= rad2deg(atan2(-$dy,$dx)) ;
			
			$ofx=$w * $dy/$dh;
			$ofy=$w * -$dx/$dh;
			
			$ang2=normalize($ang);
			$ang=normalize2($ang);
			$deg= ($ang - floor($ang))* 60;
			$ang=floor($ang)." *  ".sprintf("%01.0f '",$deg);
			$len= sprintf("%01.2f m",$dh/$scale);
			imagettftext($image, $s, $ang2, $mx+  $ofx, $my+$ofy, $c, $font, $len);
			imagettftext($image, $s, $ang2, $mx-  $ofx, $my-$ofy, $c, $font, $ang);
		}
	}
	
}

function LabelDrawing ($image,$margin,$c, $title, $line1,$line2,$line3,$line4)
{
		  $font = 'arial.ttf';
		  imagettftext($image, 13, 0, $margin, $margin, $c, $font, $title);
		  imagettftext($image, 12, 0, $margin, $margin+20, $c, $font, $line1);
		  imagettftext($image, 12, 0, $margin, $margin+40, $c, $font, $line2);
		  imagettftext($image, 10, 0, $margin, $margin+60, $c, $font, $line3);
		  imagettftext($image, 10, 0, $margin, $margin+75, $c, $font, $line4);
}

function drawOrigin($image,$c,$mid,$mg,$tnlen,$tnwd,$pd,$pdwd,$armg,$arwd)
{
	$font = 'arial.ttf';
	imageline($image,$mid,$mg,$mid,$mg+$tnlen,$c);
	imageline($image,$mid-$tnwd/2,$mg+$tnlen/2,$mid+$tnwd/2,$mg+$tnlen/2,$c);
	
	imageline($image,$mid-$pd,$mg-$pd +$tnlen/2,$mid-$pd,$mg-$pd-$pdwd+$tnlen/2,$c);
	imageline($image,$mid-$pd,$mg-$pd +$tnlen/2,$mid-$pd-$pdwd,$mg-$pd +$tnlen/2,$c);
	
	imageline($image,$mid+$pd,$mg-$pd +$tnlen/2,$mid+$pd,$mg-$pd-$pdwd+$tnlen/2,$c);
	imageline($image,$mid+$pd,$mg-$pd +$tnlen/2,$mid+$pd+$pdwd,$mg-$pd +$tnlen/2,$c);
	
	imageline($image,$mid+$pd,$mg+$pd +$tnlen/2,$mid+$pd,$mg+$pd+$pdwd+$tnlen/2,$c);
	imageline($image,$mid+$pd,$mg+$pd +$tnlen/2,$mid+$pd+$pdwd,$mg+$pd +$tnlen/2,$c);
	
	imageline($image,$mid-$pd,$mg+$pd +$tnlen/2,$mid-$pd,$mg+$pd+$pdwd+$tnlen/2,$c);
	imageline($image,$mid-$pd,$mg+$pd +$tnlen/2,$mid-$pd-$pdwd,$mg+$pd +$tnlen/2,$c);
	
	imagefilledpolygon($image, array($mid,$mg + $armg,$mid-$arwd/2,$mg + $armg+ $arwd,$mid+$arwd/2,$mg + $armg+ $arwd),
		3,
		$c);
	imagettftext($image, 8, 0, $mid-10, $mg +$tnlen/2 -$armg, $c, $font, "T");
	imagettftext($image, 8, 0, $mid+10, $mg +$tnlen/2 -$armg, $c, $font, "N");
}
function drawMarker($image, $c, $midx, $midy,$mx,$my,$lx,$ly,$l,$w,$lbx,$lby)
{
	$font = 'arial.ttf';
	imageline($image,$midx,$my,$midx,$my+$ly,$c);
	imageline($image,$midx,$l,$midx,$l - $ly,$c);
	
	imageline($image,$mx,$midy,$mx+$lx,$midy,$c);
	imageline($image,$w,$midy,$w -$lx,$midy,$c);
	imagettftext($image, 8, 0, $w -$lx-10, $midy+12, $c, $font, sprintf("%01.2f m. N",$lby));
	imagettftext($image, 8, 90, $midx-5, $l, $c, $font, sprintf("%01.2f m. E",$lbx));
}
function modifyPoints ($d,$l,$w,$ml,$mw)
{
	for($i=0;$i<count($d);$i+=2)
	{
		$ii=$i+1;
		$iii=$i/2;
		if(isset($d[$i]) && isset($d[$ii]))
		{
			$dx[$iii]=$d[$i];
			$dy[$iii]=$d[$ii];
		}
	}
	$xmax=max($dx);
	$xmin=min($dx);
	
	$ymax=max($dy);
	$ymin=min($dx);
	
	$xw=$xmax - $xmin;
	$yw=$ymax - $ymin;
	
	$yscale=($l-$ml)/ $yw;
	$xscale=($w-$mw)/ $xw;
	
	$scale= min($xscale,$yscale);

	for($i=0;$i<count($d);$i+=2)
	{
		$ii=$i+1;
		$iii=$i/2;
		if(isset($dx[$iii]) && isset($dy[$iii]))
		{
			$d[$i]=$mw + (($dx[$iii]-$xmin) * $scale);
			$d[$ii]= $l - (($dy[$iii]-$ymin) * $scale);
		}
	}
	$d[]=$scale;
	return $d;	
}
?>