Login   Register  
PHP Classes
elePHPant
Icontem

File: example2.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Yasir Siddiqui  >  PHP Closest Pair Problem  >  example2.php  >  Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Closest Pair Problem
Determine the closest pair in a set of points
Author: By
Last change:
Date: 10 months ago
Size: 1,146 bytes
 

Contents

Class file image Download
<?php
require_once 'ClosestPair.php';

// Create points array and fill x and y axis randomly
$points = array();
for(
$i=0;$i<=25;$i++) {
    
// Make sure range of rand function is less then image width otherwise points will not be drawn
    
$xaxis rand(5490);
    
// Make sure range of rand function is less then image height otherwise points will not be drawn
    
$yaxis rand(5490);
    
$points[] = array('x' => $xaxis'y' => $yaxis);
}
// Create class object
$obj = new ClosestPair();

// create a blank image
$image imagecreatetruecolor(500500);

// choose a color for the ellipse
$col_ellipse imagecolorallocate($image255255255);

// draw the white ellipse
foreach ($points as $p) {
    
// Draw point on image
    
imagefilledellipse($image$p['x'], $p['y'], 22$col_ellipse);
}

// Calculate closest pair using brute force approcah 
$results =  $obj->findClosestPairUsingBruteForce($points);

$col_line imagecolorallocate($image25500);
// Draw line between two closest points
imageline($image$results[1]['x'], $results[1]['y'], $results[2]['x'], $results[2]['y'], $col_line);

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