IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C#] Problème génération heightmap avec l'algo DiamondSquare [Débutant(e)]


Sujet :

Windows Forms

  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2011
    Messages
    204
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2011
    Messages : 204
    Points : 511
    Points
    511
    Par défaut [C#] Problème génération heightmap avec l'algo DiamondSquare
    Bonjour à tous,

    Je fais un projet de générateur de Heightmaps en C#, mais il y a un problème avec l'image générée. En effet, il y a des "glitchs" qui apparaissent, comme si on voyait la structure des différents "carrés" et "diamants" de l'algorithme. Faute de mieux, j'ai adapté le code source donné dans cet article directement en C#.

    Une image valant mieux que mille mots, voici une des heightmaps incriminées :



    Le code écrit est le suivant (c'est un projet WinForm) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
     
    namespace Diamond_Square
    {
        class DiamondSquare
        {
            private float[,] vectPoints;
            private int size;
            private float variability;
            private float spacing;
            private float min;
            private float max;
            private bool filled;
            private Random rnd;
     
            private const int PIXEL_SIZE = 3;
     
            public DiamondSquare(int steps, float variability, float spacing)
            {
                size = (1 << steps) + 1;
                this.variability = variability;
                this.spacing = spacing;
                rnd = new Random();
                filled = true;
                vectPoints = new float[size, size];
            }
     
            private float Randomize()
            {
                const int limit = 1000000;
                return (float)(rnd.Next() % limit) / limit;
            }
     
            private float DiamondStep(int x, int y, int halfSpacing)
            {
                float sum = 0f;
                int n = 0;
     
                if ((x >= halfSpacing) && (y >= halfSpacing))
                {
                    sum += vectPoints[x - halfSpacing, y - halfSpacing];
                    n++;
                }
     
                if ((x >= halfSpacing) && (y + halfSpacing < size))
                {
                    sum += vectPoints[x - halfSpacing, y + halfSpacing];
                    n++;
                }
     
                if ((x + halfSpacing < size) && (y >= halfSpacing))
                {
                    sum += vectPoints[x + halfSpacing, y - halfSpacing];
                    n++;
                }
     
                if ((x + halfSpacing < size) && (y + halfSpacing < size))
                {
                    sum += vectPoints[x + halfSpacing, y + halfSpacing];
                    n++;
                }
     
                return sum / n;
            }
     
            private float SquareStep(int x, int y, int halfSpacing)
            {
                float sum = 0f;
                int n = 0;
     
                if (x >= halfSpacing)
                {
                    sum += vectPoints[x - halfSpacing, y];
                    n++;
                }
     
                if (x + halfSpacing < size)
                {
                    sum += vectPoints[x + halfSpacing, y];
                    n++;
                }
     
                if (y >= halfSpacing)
                {
                    sum += vectPoints[x, y - halfSpacing];
                    n++;
                }
     
                if (y + halfSpacing < size)
                {
                    sum += vectPoints[x, y + halfSpacing];
                    n++;
                }
     
                return sum / n; 
            }
     
            public void Generate(float leftBottom = 0f, float leftTop = 0f, float rightTop = 0f, float rightBottom = 0f)
            {
                vectPoints[0, 0] = leftBottom;
                vectPoints[0, size - 1] = rightBottom;
                vectPoints[size - 1, 0] = leftTop;
                vectPoints[size - 1, size - 1] = rightTop;
     
                int spacing = size - 1;
     
                while (spacing > 1)
                {
                    int halfSpacing = spacing / 2;
     
                    for (int x = halfSpacing; x < size; x += spacing)
                    {
                        for (int y = halfSpacing; y < size; y += spacing)
                        {
                            vectPoints[x, y] = DiamondStep(x, y, halfSpacing) + Randomize() * spacing * variability;
                        }
                    }
     
                    for (int x = 0; x < size; x += halfSpacing)
                    {
                        int yStart = ((x / halfSpacing) % 2 == 0) ? halfSpacing : 0;
     
                        for (int y = yStart; y < size; y += spacing)
                        {
                            vectPoints[x, y] = SquareStep(x, y, halfSpacing) + Randomize() * spacing * variability;
                        }
                    }
     
                    spacing = halfSpacing;
                }
     
                max = vectPoints[0, 0];
                min = vectPoints[0, 0];
     
                for (int x = 0; x < size - 1; x++)
                {
                    for (int y = 0; y < size - 1; y++)
                    {
                        if (vectPoints[x, y] > max)
                        {
                            max = vectPoints[x, y];
                        }
     
                        if (vectPoints[x, y] < min)
                        {
                            min = vectPoints[x, y];
                        }
                    }
                }
            }
     
            public void Draw(Graphics graphics)
            {
                for (int x = 0; x < size - 1; x++)
                {
                    for (int y = 0; y < size - 1; y++)
                    {
                        if (filled)
                        {
                            graphics.FillRectangle(new SolidBrush(Color.FromArgb((int)(vectPoints[x, y]), (int)(vectPoints[x, y]), (int)(vectPoints[x, y]))), x * PIXEL_SIZE, y * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE);
                        }
                    }
                }
            }
        }
    }
    La classe est instanciée et appelée depuis le formulaire principal et l'image est dessinée dans une PictureBox. Pour information, les valeurs transmises au constructeur de l'objet de type DiamondSquare sont respectivement 6, 0.5 et 2.0.

    J'ai beau chercher, je ne vois pas d'où vient le problème

  2. #2
    Expert confirmé Avatar de DonQuiche
    Inscrit en
    Septembre 2010
    Messages
    2 741
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 2 741
    Points : 5 485
    Points
    5 485
    Par défaut
    Je n'ai jamais expérimenté cet algo mais, à vue de nez, ce ne serait pas la variabilité qui serait trop faible ?

Discussions similaires

  1. problème génération PDF avec HTML2PDF
    Par int59 dans le forum Bibliothèques et frameworks
    Réponses: 0
    Dernier message: 12/06/2015, 11h38
  2. Problème génération .class avec Tomcat 5.5 sous Windows 7!
    Par mumu27 dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 18/02/2010, 14h00
  3. Problème génération javadoc avec Eclipse
    Par pontus21 dans le forum Eclipse Java
    Réponses: 5
    Dernier message: 05/01/2007, 00h24
  4. Problème sur un réseau routier avec l'algo de Ford-Fulkerson
    Par Yakurena dans le forum Algorithmes et structures de données
    Réponses: 1
    Dernier message: 20/02/2006, 09h35
  5. Problême avec les algos, itérateurs ...
    Par R'SKaP dans le forum C++
    Réponses: 14
    Dernier message: 18/12/2005, 23h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo