begin process at 2010 03 21 06:03:16
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > IMAGES GENETIQUES

IMAGES GENETIQUES




 Description

Cliquez pour voir la capture en taille normale
salut

je m'ennuyais cette nuit et j'avais envie de faire une pause sur mes autres projets alors j'ai fait ca :

un algorithme genetique de generation d'images aleatoires

Source

  • <?php
  • session_start();
  • // unset($_SESSION['db']);
  • class Point{
  • public function random(){
  • $this->x = ( self::$ox + mt_rand(- self::x_max ,self::x_max) / 5 ) % self::x_max;
  • $this->y = ( self::$oy + mt_rand(- self::y_max,self::y_max) / 5 ) % self::y_max;
  • self::$ox = $this->x;
  • self::$oy = $this->x;
  • }
  • public function mute(){
  • $this->x += mt_rand(-5, 5);
  • $this->y += mt_rand(-5, 5);
  • }
  • const x_max = 200;
  • const y_max = 200;
  • public $x, $y;
  • private static $ox = 0, $oy = 0;
  • }
  • class polygone{
  • public function __construct(){
  • }
  • public function getImg($img, $black){
  • $c = count($this->array) - 1;
  • $fp = null;
  • $op = null;
  • foreach ($this->array as $p){
  • if ($fp === null) {
  • $fp = $p;
  • }else{
  • imageLine($img, $op->x, $op->y, $p->x, $p->y, $black);
  • }
  • $op = $p;
  • }
  • imageLine($img, $fp->x, $fp->y, $op->x, $op->y, $black);
  • }
  • public function random(){
  • $this->array = array();
  • $number = mt_rand(3,15);
  • for ($i = 0; $i<$number ; $i++){
  • $this->array[$i] = new Point();
  • $this->array[$i]->random();
  • }
  • }
  • public function mute(){
  • if (count($this->array) > 3 && mt_rand(0, 10) < 4 ){
  • unset($this->array[mt_rand(0, count($this->array) - 1)]);
  • }else{
  • $p = $array[] = new Point();
  • $p->random();
  • }
  • foreach ($this->array as $p){
  • $p->mute();
  • }
  • }
  • private $array;
  • }
  • function cmpimgs(Img $a, Img $b){
  • return $a->getNote() - $b->getNote();
  • }
  • class Img{
  • public function __construct(){
  • $this->note = 0;
  • }
  • public function getHtml(){
  • return '<a href="imgs.php?checked='.$this->id.'"><img src="imgs.php?id='.$this->id.'" /> ('.$this->note.') </a>';
  • }
  • public function getImg(){
  • $img = imageCreate(200, 200);
  • $white = imageColorAllocate($img, 255, 255, 255);
  • $black = imageColorAllocate($img, 0, 0, 0);
  • foreach ($this->array as $p){
  • $p->getImg($img, $black);
  • }
  • return $img;
  • }
  • public function random(){
  • $this->array = array();
  • $number = mt_rand(2,16);
  • for ($i = 0; $i<$number ; $i++){
  • $this->array[$i] = new polygone();
  • $this->array[$i]->random();
  • }
  • }
  • public function setId($id){
  • $this->id = $id;
  • }
  • public function clicked(ImgDatabase $db, $nbr){
  • $imgs = $db->getimgs($nbr);
  • usort($imgs, 'cmpimgs');
  • $this->note += $nbr;
  • foreach ($imgs as $img){
  • $img->note --;
  • }
  • if (mt_rand(0, 10) < 4){
  • $db->rem($nbr - 1);
  • }else {
  • $imgs[$nbr - 1]->accoupler($this);
  • if (mt_rand(0, 10) < 4){
  • $me = clone $this;
  • $me->mute();
  • $db->store($me);
  • }
  • if (mt_rand(0, 10) < 4){
  • $m = new Img();
  • $m->random();
  • $db->store($m);
  • }
  • }
  • }
  • private function accoupler(Img $i){
  • $this->array = array_merge($this->getRPart(), $i->getRPart());
  • $this->note = 0;
  • }
  • private function mute(){
  • unset($this->array[mt_rand(0, count($this->array) - 1)]);
  • foreach ($this->array as $p){
  • if (mt_rand(0, 10) < 4){
  • $p->mute();
  • }
  • }
  • }
  • private function getRPart(){
  • $c = count($this->array);
  • $l = mt_rand(5, $c);
  • $f = mt_rand(0, $c - $l);
  • return array_slice($this->array, $f, $l);
  • }
  • public function getNote(){
  • return $this->note;
  • }
  • private $array, $id, $note;
  • }
  • class ImgDatabase{
  • public function __construct(){
  • if (!isset($_SESSION['db'])){
  • $_SESSION['db'] = array();
  • }
  • $this->db = $_SESSION['db'];
  • }
  • public function __destruct(){
  • $_SESSION['db'] = $this->db;
  • }
  • public function getimgs($n){
  • return array_slice($this->db, 0, $n);
  • }
  • public function store($img){
  • $this->db[] = $img;
  • }
  • public function rem($n){
  • unset($this->db[$n]);
  • $this->db = array_values($this->db);
  • }
  • public function get($n){
  • $out = array();
  • $l = count($this->db);
  • for ($i=0;$i<$n && $i < $l;$i++){
  • $j = mt_rand($i, $l-1);
  • $out[$i] = $this->db[$j];
  • $this->db[$j] = $this->db[$i];
  • $this->db[$i] = $out[$i];
  • $this->db[$i]->setId($i);
  • $this->db[$j]->setId($j);
  • }
  • for ($i=$l;$i<$n;$i++){
  • $out[$i] = $this->db[$i] = new Img();
  • $out[$i]->random();
  • $out[$i]->setId($i);
  • }
  • return $out;
  • }
  • public function getById($id){
  • return $this->db[$id];
  • }
  • public function getNbrPic(){
  • return count($this->db);
  • }
  • private $db;
  • }
  • $db = new ImgDatabase();
  • $nbr = 8;
  • function echopage($db, $nbr){
  • $imgs = $db->get($nbr);
  • ?>
  • <table>
  • <tr>
  • <td><?php echo $imgs[0]->getHTML(); ?></td>
  • <td><?php echo $imgs[1]->getHTML(); ?></td>
  • <td><?php echo $imgs[2]->getHTML(); ?></td>
  • </tr>
  • <tr>
  • <td><?php echo $imgs[3]->getHTML(); ?></td>
  • <td><?php echo $imgs[4]->getHTML(); ?></td>
  • <td><?php echo $imgs[5]->getHTML(); ?></td>
  • </tr>
  • <tr>
  • <td><?php echo $imgs[6]->getHTML(); ?></td>
  • <td><?php echo $imgs[7]->getHTML(); ?></td>
  • <td><p><?php echo $db->getNbrPic(); ?></p></td>
  • </tr>
  • </table>
  • <?php
  • }
  • if (isset($_GET['checked'])){
  • $img = $db->getById((int) $_GET['checked']);
  • $img->clicked($db, $nbr);
  • echopage($db, $nbr);
  • }else if (!isset($_GET['id'])){
  • echopage($db, $nbr);
  • }else{
  • $img = $db->getById((int) $_GET['id']);
  • $a = $img->getImg();
  • imagejpeg($a);
  • }
  • ?>
<?php
session_start();

// unset($_SESSION['db']);

class Point{
	public function random(){
		$this->x = ( self::$ox + mt_rand(- self::x_max ,self::x_max) / 5 ) % self::x_max;
		$this->y = ( self::$oy + mt_rand(- self::y_max,self::y_max) / 5 ) % self::y_max;

		self::$ox = $this->x;
		self::$oy = $this->x;
	}
	public function mute(){
		$this->x += mt_rand(-5, 5);
		$this->y += mt_rand(-5, 5);
	}
	const x_max = 200;
	const y_max = 200;
	public $x, $y;
	private static $ox = 0, $oy = 0;
}

class polygone{
	public function __construct(){

	}
	public function getImg($img, $black){
		$c = count($this->array) - 1;
		$fp = null;
		$op = null;
		foreach ($this->array as $p){
			if ($fp === null) {
				$fp = $p;
			}else{
			      imageLine($img, $op->x, $op->y, $p->x, $p->y, $black);
			}
			$op = $p;
		}
		imageLine($img, $fp->x, $fp->y, $op->x, $op->y, $black);
	}
	public function random(){
		$this->array = array();
		$number = mt_rand(3,15);
		for ($i = 0; $i<$number ; $i++){
			$this->array[$i] = new Point();
			$this->array[$i]->random();
		}
	}
	public function mute(){
		if (count($this->array) > 3 && mt_rand(0, 10) < 4 ){
			unset($this->array[mt_rand(0, count($this->array) - 1)]);
		}else{
			$p = $array[] = new Point();
			$p->random();
		}
		foreach ($this->array as $p){
			$p->mute();
		}
	}
	private $array;
}

function cmpimgs(Img $a, Img $b){
	return $a->getNote() - $b->getNote();
}

class Img{
	public function __construct(){
		$this->note = 0;
	}
	public function getHtml(){
		return '<a href="imgs.php?checked='.$this->id.'"><img src="imgs.php?id='.$this->id.'" /> ('.$this->note.') </a>';
	}
	public function getImg(){
		$img = imageCreate(200, 200);
		$white = imageColorAllocate($img, 255, 255, 255);
		$black = imageColorAllocate($img, 0, 0, 0);
		foreach ($this->array as $p){
			$p->getImg($img, $black);
		}
		return $img;
	}
	public function random(){
		$this->array = array();
		$number = mt_rand(2,16);
		for ($i = 0; $i<$number ; $i++){
			$this->array[$i] = new polygone();
			$this->array[$i]->random();
		}
	}
	public function setId($id){
		$this->id = $id;
	}
	public function clicked(ImgDatabase $db, $nbr){
		$imgs = $db->getimgs($nbr);
		usort($imgs, 'cmpimgs');
		$this->note += $nbr;
		foreach ($imgs as $img){
			$img->note --;
		}
		if (mt_rand(0, 10) < 4){
			$db->rem($nbr - 1);
		}else {
			$imgs[$nbr - 1]->accoupler($this);
			if (mt_rand(0, 10) < 4){
				$me = clone $this;
				$me->mute();
				$db->store($me);
			}
			if (mt_rand(0, 10) < 4){
				$m = new Img();
				$m->random();
				$db->store($m);
			}
		}
	}
	private function accoupler(Img $i){
		$this->array = array_merge($this->getRPart(), $i->getRPart());
		$this->note = 0;
	}
	private function mute(){
		unset($this->array[mt_rand(0, count($this->array) - 1)]);
		foreach ($this->array as $p){
			if (mt_rand(0, 10) < 4){
				$p->mute();
			}
		}
	}
	private function getRPart(){
		$c = count($this->array);
		$l = mt_rand(5, $c);
		$f = mt_rand(0, $c - $l);
		return array_slice($this->array, $f, $l);
	}
	public function getNote(){
		return $this->note;
	}
	private $array, $id, $note;
}

class ImgDatabase{
	public function __construct(){
		if (!isset($_SESSION['db'])){
			$_SESSION['db'] = array();
		}
		$this->db = $_SESSION['db'];
	}
	public function __destruct(){
		$_SESSION['db'] = $this->db;
	}
	public function getimgs($n){
		return array_slice($this->db, 0, $n);
	}
	public function store($img){
		$this->db[] = $img;
	}
	public function rem($n){
		unset($this->db[$n]);
		$this->db = array_values($this->db);
	}
	public function get($n){
		$out = array();
		$l = count($this->db);
		for ($i=0;$i<$n && $i < $l;$i++){
			$j = mt_rand($i, $l-1);
			$out[$i] = $this->db[$j];
			$this->db[$j] = $this->db[$i];
			$this->db[$i] = $out[$i];
			$this->db[$i]->setId($i);
			$this->db[$j]->setId($j);
		}
		for ($i=$l;$i<$n;$i++){
			$out[$i] = $this->db[$i] = new Img();
			$out[$i]->random();
			$out[$i]->setId($i);
		}
		return $out;
	}
	public function getById($id){
		return $this->db[$id];
	}
	public function getNbrPic(){
		return count($this->db);
	}
	private $db;
}

$db = new ImgDatabase();
$nbr = 8;

function echopage($db, $nbr){
	$imgs = $db->get($nbr);
	?>
	<table>
	<tr>
	<td><?php echo $imgs[0]->getHTML(); ?></td>
	<td><?php echo $imgs[1]->getHTML(); ?></td>
	<td><?php echo $imgs[2]->getHTML(); ?></td>
	</tr>
	<tr>
	<td><?php echo $imgs[3]->getHTML(); ?></td>
	<td><?php echo $imgs[4]->getHTML(); ?></td>
	<td><?php echo $imgs[5]->getHTML(); ?></td>
	</tr>
	<tr>
	<td><?php echo $imgs[6]->getHTML(); ?></td>
	<td><?php echo $imgs[7]->getHTML(); ?></td>
	<td><p><?php echo $db->getNbrPic(); ?></p></td>
	</tr>
	</table>
	<?php
}

if (isset($_GET['checked'])){
	$img = $db->getById((int) $_GET['checked']);
	$img->clicked($db, $nbr);
	echopage($db, $nbr);
}else if (!isset($_GET['id'])){
	echopage($db, $nbr);
}else{
	$img = $db->getById((int) $_GET['id']);
	$a = $img->getImg();
	imagejpeg($a);
}
?>

 Conclusion

les reproductions ne sont pas hyper clean, on pourrait ajouter des options genre :

decaler un groupe de polygones

ne pas ajouter les points qu'on ajoute aux polygones seulement a la fin de ces polygones

ne pas faire bouger de +- 5 tout les points de chaque polygones mutant.

ne pas faire muter tout les polygones

on pourrait aussi ajouter a la selection naturelle : une selection du joyeux mutant (ca reviendrait a generer 5 mutants et enfants, et a demander a l'utilisateur lequel on garde)


 Sources du même auteur

Source avec Zip Source avec une capture RUBIX CUBE
Source avec Zip INTERPRETEUR BRAINFUCK
Source avec Zip PROXY HTTP : SAUVEZ VOS VIDEOS PREFEREES.
Source avec une capture ECRIRE UN TEXTE EN CERCLE SUR UNE IMAGE
Source avec Zip Source avec une capture FILTRER, METTRE EN CACHE OU MAPER, OU TRIER UN ITERATEUR

 Sources de la même categorie

Source avec une capture CALCUL DE TVA MARGE AVEC REMISE FOURNISSEUR SPÉCIALE POUR LE... par lcomb
Source avec Zip EVALUER UNE EXPRESSION À PARTIR D'UNE CHAINE DE CARACTÈRE par TheWeasel47
FONCTION EQUATION LÉGÈRE par ff5
Source avec Zip Source avec une capture TRACEUR DE COURBE EN COORDONNÉES CARTÉSIENNES (MAJ) par fredbonmatin
CONVERTIR LES RÉFÉRENCES DE COLONNE EXCEL DE CHIFFRE EN LETT... par computman007

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Y.A.P.C. 'YES ANOTHER PICTURES CLASS' par inwebo
Source avec Zip FUNCTION IMAGE SIMPLE ET ARRAY par astro53
Source avec Zip LABYRINTHES EN PHP (GD + HTML) GÉNÉRATION + RÉSOLUTION par nax333
Source avec Zip Source avec une capture [GD] PRÉSENTATION GRAPHIQUE DE DONNÉES : LES POLYGONES par Mr Aliasing
Source avec Zip GÉNÉRATION AUTOMATIQUE DE BOUTON par Franquito

Commentaires et avis

Commentaire de coucou747 le 06/03/2009 05:07:33

ameliorations possibles :
- ne prendre que des polynomes convexes OU "demeller" deux droites qui se croisent

Commentaire de malalam le 06/03/2009 19:26:30 administrateur CS

'tain, t'as de sacrées nuits, Max...;-)

Je ne commente ni ne note vu que je n'y pige que dalle. Moi et les sciences...:-) Mais bon, joli code, évidemment.

Commentaire de coucou747 le 06/03/2009 19:34:57

merci :)

c'est toujours pareil en periodes de greves ou de vacances, mes insomnies me reprennent et ca donne des codes aussi debiles que celui-ci :)

Commentaire de DiGhan le 07/03/2009 17:12:10

C'est bien de changer.
Continue à déposer ce genre de sources.

Commentaire de coucou747 le 07/03/2009 17:48:17

merci :)

je crois que je vais commencer par ameliorer celle-ci

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

comment redimensionner à la volée des images grâce à la lib GD ??? [TITRE MODERE CAR BOURRE DE FAUTES] [ par fredericmaill ] Bonjour, j'aimerais savoir comment redimention&#233; a la voll&#233; des images, donc j'aimerais savoir quelle fonction de la lib gd utiilis&#233; !! Php et les images [ par SeNeO64 ] Bonjour tout le monde.J'aurais voulu savoir s'il existait d'autre librairie que GD permettant de traiter des images.J'ai un script permettant de redim [BLOB->GD] Redimensionner un "flux image binaire"... [ par arnal69130 ] Bonjour &#224; tous,Je cherche &#224; faire une page pour afficher la carte d'identit&#233; d'un "agent", pour simplifier disons juste son nom et sa p gd et la superposition d'images transparentes [ par bizu29 ] Tout d'abord bonjour/soirAlors j'ai un petit soucis qui commence a me rendre dingue Mon but est de fusionner ces 2 images avec gd <img src="http://biz Pb avec la lib GD [ par quentin2b ] Bonjour a tous,Je cherchais un petit script pour protéger des images sur un de mes sites masi voila je rencontre un pb avec le code sources que j'ai t redimmensionner des images ... [ par loupile ] Bonjour, je vous explique sur mon site j'affiche des images hebergées sur des serveurs distants ... donc pas sur le mien :-) je voudrais pouvoir rédui Problème avec GD sur mon serveur [ par rastajeff ] Bonjour, je travaille actuellement sur un projet Flex/PHP et je rencontre une erreur bizarre. Je fais un upload et redimensionnement d'images, tout ce Upload de fichier php [ par paulito47 ] Je réalise un script php qui génère un formulaire qui permet la collecte des informations nécessaires pour l'ajout d'une photo (titre, description) et images defilantes en boucle [ par ckripat ] Bonjour Merci d'abord a toutes les personnes qui m'ont aidées grâce a code-sources à faire mon [url=http://patparlot.free.fr/]site[/url] en proposant Upload d'images : prévisualisation avant upload avec php+ajax+javascript [ par amewole ] Bonjour à vous tous, Je suis à la recherche d'un script php+ajax+javascript permettant de faire un preview des images avant le upload c'est à dire qu


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,780 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales