Accueil > > > IMAGES GENETIQUES
IMAGES GENETIQUES
Information sur la source
Description
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
Sources de la même categorie
Commentaires et avis
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é a la vollé des images, donc j'aimerais savoir quelle fonction de la lib gd utiilisé !!
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 à tous,Je cherche à faire une page pour afficher la carte d'identité 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
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
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
Probleme de création d'images [ par _stic_ ]
Bonjour à tous,J'ai créé un fichier php dans lequel je crée des images. Ca fonctionne très bien en local sous easyphp sous windows mais lorsque je le
afficher des images [ par sgheir ]
je veux afficher des image stocker dans la base de donnee sans l'affichage de le nom de l image
Detecter GD [ par Defaite ]
Bonjour à tous !Juste un question toute bête, mais à laquelle je ne trouve pas de réponse. (et pourtant, Google est mon pote...)Voila, comme savoir si
|
Derniers Blogs
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|