[PHP]多角形の重心の座標や面積を計算する

polygon

PHP で使う機会は余りありませんが、たまたま多角形の重心点を計算する必要があったので、下記サイトを参考にして PHP で書き換えてみました。

参考
http://homepage2.nifty.com/gis_yasu21/sub1.htm(リンク切れ)

<?php
// 頂点を右回りに記述 (x, y)
$vertexes = array(
	array(0, 0),
	array(10, 80),
	array(100, 100),
	array(120, 30)
);

$n = count($vertexes);

$area = 0;
$gx   = 0;
$gy   = 0;

for($i=0;$i<$n;$i++){
	
	$pt1 = $vertexes[$i];
	$pt2 = ($i + 1 >= $n) ? $vertexes[0] : $vertexes[$i + 1];

	$area = $area + ($pt2[0] - $pt1[0]) * ($pt2[1] + $pt1[1]);
	
	$gx = $gx + ((pow($pt2[0], 3) - pow($pt1[0], 3)) * ($pt2[1] - $pt1[1]) / 3
		+ (pow($pt2[0], 2) - pow($pt1[0], 2)) * ($pt2[0] * $pt1[1] - $pt1[0] * $pt2[1]) / 2)
		/ ($pt2[0] - $pt1[0]);
	
	$gy = $gy + ((pow($pt2[1], 3) - pow($pt1[1], 3)) * ($pt2[0] - $pt1[0]) / 3
		+ (pow($pt2[1], 2) - pow($pt1[1], 2)) * ($pt1[0] * $pt2[1] - $pt2[0] * $pt1[1]) / 2)
		/ ($pt2[1] - $pt1[1]);
}

$area = abs($area * 0.5);    // 面積
$gx = $gx / $area;           // 重心x座標
$gy = -$gy / $area;          // 重心のy座標

[HTML5+JS+Canvas, PHP]アナログ時計の作り方

canvas_clock

Javascript と html5 の canvas を組み合わせてリアルタイムで動作するアナログ時計を作ってみました。
おまけですが PHP と GD を使ったバージョンも合わせて掲載しています。

動作サンプル

Html5 + Javascript + canvas

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Clock</title>

<script type="text/javascript">
<!--
window.onload = function()
{
	var width   = 100;
	var height  = 100;
	var centerX = Math.floor(width / 2);
	var centerY = Math.floor(height / 2);
	var canvas  = document.getElementById('clock');
	var context = canvas.getContext('2d');
	
	setInterval(drawClock, 1000);
	drawClock();
		
    function drawClock() {
		
		var date   = new Date();
		var hour   = date.getHours();
		var minute = date.getMinutes();
		var second = date.getSeconds();

		
		// 色の指定
		context.strokeStyle = '#666666';

		context.clearRect(0, 0, canvas.width, canvas.height);
		
		// 文字盤の描画
		context.beginPath();
		context.arc(centerX, centerY, centerX - 1, 0, Math.PI*2, false);
		context.stroke();
			
		context.save();
		context.translate(width/2, height/2);
		for(var i=0;i < 360; i+= 30){
			context.rotate( 30 * Math.PI / 180);
			context.beginPath();
			context.moveTo(0, centerY * 0.8);
			context.lineTo(0, centerY * 0.9);
			context.stroke();
		}
		context.translate(-width/2, -height/2);
		context.restore();
		
		context.strokeStyle = '#333333';
		
		drawHand(centerY * 0.5, hour * 30 + minute / 60 * 30);
		drawHand(centerY * 0.8, minute * 6 + second / 60 * 6);
		
		context.strokeStyle = '#EE0000';
		
		drawHand(centerY * 0.8, second * 6);
	}
	
	// 針の描画
	function drawHand(length, angle){
		context.save();
		context.translate(centerX, centerY);
		context.rotate( angle * Math.PI / 180);
		context.beginPath();
		context.moveTo(0, 0);
		context.lineTo(0, -length);
		context.stroke();
		context.restore();
	}
}
//-->
</script>

</head>
<body>
<canvas id="clock"></canvas>
</body>
</html>

rotate() だけでは回転軸を指定して回転させる事ができないので、translate() を使って画像全体をずらし、回転させた後で元の位置に戻すことで回転の中心を変えています。


PHP + GD

PHP の imagedrawellipse() と imagedrawline() の組み合わせでも一応作れます。
こちらはリアルタイム動作ではありません。また、サーバー側のリソースを消費するので使い道はあまり無いかもしれません。

clock

<?php
date_default_timezone_set('Asia/Tokyo');

$date   = new DateTime();
$hour   = (int)$date->format("h");
$minute = (int)$date->format("i");

$width  = 100;
$height = 100;

$centerX = floor($width / 2);
$centerY = floor($height / 2);

$img       = imagecreatetruecolor($width, $height);
$handColor = imagecolorallocate($img, 0x33, 0x33, 0x33);
$dialColor = imagecolorallocate($img, 0xAA, 0xAA, 0xAA);
$bg        = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);

imagefill($img, 0, 0, $bg);
imagecolortransparent($img, $bg);

// 文字盤の描画
imageellipse($img, $centerX, $centerY, $width - 1, $width -1, $dialColor);

for($i=0;$i < 360;$i+=30){
	list($x1, $y1) = point_rotate($centerX * 0.8, $i);
	list($x2, $y2) = point_rotate($centerX * 0.9, $i);
	imageline($img, $x1 + $centerX, $y1 + $centerY, $x2 + $centerX, $y2 + $centerY, $dialColor);
}

// 針の描画
$hour = $hour % 12;
list($hourX, $hourY) = point_rotate($centerX * 0.5, $hour * 30 + $minute / 2);
imageline($img, $centerX, $centerY, $hourX + $centerX, $hourY + $centerY, $handColor);
list($minuteX, $minuteY) = point_rotate($centerX * 0.8, $minute * 6);
imageline($img, $centerX, $centerY, $minuteX + $centerX, $minuteY + $centerY, $handColor);

header("Content-type:image/png");
imagepng($img);
imagedestroy($img);


function point_rotate($r, $degree){
  $radian = deg2rad($degree - 90);
  $x = round($r * cos($radian));
  $y = round($r * sin($radian));
  return array($x, $y);
}

[PHP]背景の明るさを基準に見やすい文字色を組み合わせる(YUV輝度)

背景色によって白か黒の文字色を選ぶ時、黒い背景の時は白い文字、白い背景の文字の時は黒い文字が見やすいのは確かですが、青い背景や赤い背景、黄色の背景ではどうでしょうか?

RGB値はそれぞれ 0 ~ 255 の数値で表され、0 に近いほど暗くなり、255 に近いほど白っぽくなるのはご存知かと思います。
そこで、RGB の平均が半分を超えていれば明るい背景で、半分以下なら暗い背景であると単純に考えてしまうのは誤りです。

青(0, 0, 255) はその基準では暗い背景であり、確かに白い文字は見やすいです。

SAMPLE

緑色(0, 255, 0) も同じ平均値なので白い文字を使ってみます。

SAMPLE

かなり読みづらいのがわかると思います。
人間の視覚では RGB の青は暗く、緑は明るく見えるからです。

そこで映像分野でよく使われている輝度成分を利用します。
輝度(Y)は次の変換公式がよく使われています。

Y = 0.299 × R + 0.587 × G + 0.114 × B

では、RGBの平均を基準にしたものと、輝度を基準にしたもので文字の見え方がどう変わるか見てみます。

yuv

RGBでは緑、ピンクが見づらいですが、輝度を基準にしたものは解消されています。算出に用いた PHP のプログラムは以下です。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sample</title>
</head>
<body>
<?php
$colors = array(
	array(0, 0, 0),
	array(255, 255, 255),
	array(255, 0, 0),
	array(0, 255, 0),
	array(0, 0, 255),
	array(255, 255, 0),
	array(0, 255, 255),
	array(255, 0, 255)
);

foreach($colors as $color){
	list($r, $g, $b) = $color;
	$y = 0.299 * $r + 0.587 * $g + 0.114 * $b;
	$html = <<<EOD
<span style="background-color: rgb(%d, %d, %d);color: %s;">
SAMPLE</span><br />
EOD;

	$textColor = ($y > 127) ? 'black' : 'white';
	printf($html, $r, $g, $b, $textColor);
}
?>
</body>
</html>

参考: Wikipedia「YUV」
http://ja.wikipedia.org/wiki/YUV