2017年8月8日火曜日

凸集合の描画

いくつか点があるときに凸集合を作成して描画をしました。

1.ライブラリの用意
今回は下記のライブラリを使いました。
https://github.com/benpoulson/PHP-ConvexHull/blob/master/convexHull.php
関数になっていたのでAppController.phpに追加しておきました。

2.コントローラでの呼び出し
コントローラから下記のように呼び出します。
public function convex() {
$this->Tsp->recursive = 0;
$this->paginate = array(
'limit'=>10000,
'maxLimit' => 30000,
);
$this->set('tsps', $this->Paginator->paginate());
$tsps = $this->Paginator->paginate();
$input = array(array());
for($i = 0; $i < count($tsps); $i++){
$input[$i][0] = $tsps[$i]['Tsp']['latitude'];
$input[$i][1] = $tsps[$i]['Tsp']['longitude'];
}
$output = $this->convexHull($input);
$this->set('output', $output);
}
view raw Convex hosted with ❤ by GitHub


3.ビューでの描画
ビューで描画します。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=MyKey&sensor=false"></script>
<div class="tsps index">
<h2><?php echo __('Tsps'); ?></h2>
<?php
$map_options = array(
'id' => 'map1',
'width' => '1200px',
'height' => '800px',
'zoom' => 13,
'type' => 'ROAD',
//'custom' => null,
'localize' => false,
'latitude' => $tsps[0]['Tsp']['latitude'],
'longitude' => $tsps[0]['Tsp']['longitude'],
'marker' => true,
'markerIcon' => 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
'markerShadow' => 'http://google-maps-icons.googlecode.com/files/shadow.png',
'infoWindow' => true,
'windowText' => "自分で指定"
);
?>
<div id="map">
<?php echo $this->GoogleMap->map($map_options); ?>
</div>
<?php $marker_index =1; ?>
<?php foreach ($tsps as $tsp): ?>
<?php
$marker_options = array(
'showWindow' => true,
'windowText' =>$tsp['Tsp']['id'].":".$tsp['Tsp']['name'],
'markerTitle' => 'Title',
'markerIcon' => 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='.$tsp['Tsp']['id'].'|ff7e73|000000',
'markerShadow' => 'http://labs.google.com/ridefinder/images/mm_20_purpleshadow.png'
);
echo $this->GoogleMap->addMarker('map1', $marker_index, array('latitude' => $tsp['Tsp']['latitude'], 'longitude' =>$tsp['Tsp']['longitude'] ),$marker_options);
$marker_index ++;
?>
<?php endforeach; ?>
<?php for($i = 0; $i < count($output); $i++){ ?>
<?php
if($i == count($output)-1){
echo $this->GoogleMap->addPolyline("map1", "polyline", array(
"start" => array("latitude" => $output[$i][0] ,"longitude"=> $output[$i][1]),
"end" => array("latitude" => $output[0][0] ,"longitude"=> $output[0][1])
));
}
else{
echo $this->GoogleMap->addPolyline("map1", "polyline", array(
"start" => array("latitude" => $output[$i][0] ,"longitude"=> $output[$i][1]),
"end" => array("latitude" => $output[$i+1][0] ,"longitude"=> $output[$i+1][1])
));
}
?>
<?php } ?>
view raw Convex View hosted with ❤ by GitHub




0 件のコメント:

コメントを投稿