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
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
|
Derniers Blogs
L'INTERFACE NATURELLE DE WINDOWS PHONE 7 SERIESL'INTERFACE NATURELLE DE WINDOWS PHONE 7 SERIES par odewit
La tendance est aux interfaces naturelles (NUI), et le keynote de Bill Buxton au MIX l'a bien souligné.
La charte graphique et ergonomique de Windows Phone 7 a donc été entièrement repensée en vue d'obtenir un maximum d'efficacité sur ce point. En re...
Cliquez pour lire la suite de l'article par odewit COMMENT MAPPER UNE VUE SQL SUR UNE COLLECTION DE COMPLEX TYPE?COMMENT MAPPER UNE VUE SQL SUR UNE COLLECTION DE COMPLEX TYPE? par Matthieu MEZIL
Avec EF, les vues doivent être mappées sur des entity types. Le problème c'est que les entity types doivent avoir une clé. Avec EF, nous avons les complex type qui n'ont pas de clé mais les vues ne peuvent pas être mappées dessus. Avec EF4, il est possibl...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [WF4] UN BINDING ACTIVITY/ACTIVITYDESIGNER QUI PASSE MAL?[WF4] UN BINDING ACTIVITY/ACTIVITYDESIGNER QUI PASSE MAL? par JeremyJeanson
Certain d'entre vous on peut être vécu cette situation embarrassante après quelques temps passer avec WF4 : Au début avec mon " ActivityDesigner" , tout allait bien. Et puis un jour j'ai au des problèmes de " Binding" . Alors nous sommes allé sur le site ...
Cliquez pour lire la suite de l'article par JeremyJeanson
Forum
MYSQL PROBLEMEMYSQL PROBLEME par remitete
Cliquez pour lire la suite par remitete
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
|