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

Turbo Pascal Discussion :

Cherche l'algorithme de la fonction FloodFill


Sujet :

Turbo Pascal

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 15
    Points : 8
    Points
    8
    Par défaut Cherche l'algorithme de la fonction FloodFill
    Bonjour je souhaiterais faire une fonction FloodFill pour ceux qui connaissent pas c'est un peu comme Paint dans le basic.

    Mais je ne veux pas utiliser celle de l'unité graph.tpu et je cherche un algorithme, en Turbo Pascal ou même en basm integré, en gardant le principe du FloodFill de l'unité Graph.

    Je voudrais créer une procédure de ce genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Const
      Fill ($FF,$00,$FF,$00,$FF,$00,$FF,$00): Byte
     
    FloodFill(X, Y, CouleurFond, Couleur Pixel, Fill);
    Est-ce que c'est compliqué de fabriquer l'algorithme de cette procédure ?

    Si vous avez des exemples merci ça pourrait m'aider.

  2. #2
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 062
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 062
    Points : 15 353
    Points
    15 353
    Billets dans le blog
    9
    Par défaut
    Bonjour !

    Je me souviens d'une discussion sur ce sujet dans le forum Free Basic, avec plusieurs exemples :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (* http://www.freebasic.net/forum/viewtopic.php?f=2&t=19943 *)
    Autrement vous pourriez regardez le code source de l'unité Graph de Free Pascal, ou encore celui de l'unité WinGraph.

    Bonne continuation !
    Mon site personnel consacré à MSEide+MSEgui : msegui.net

  3. #3
    Membre averti

    Homme Profil pro
    Diverses
    Inscrit en
    Février 2014
    Messages
    122
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Diverses

    Informations forums :
    Inscription : Février 2014
    Messages : 122
    Points : 428
    Points
    428
    Par défaut
    L'algorithme est très simple en version récursive mais très lent. Il faut donc se tourner vers les versions non récursives rendues un peu plus complexe par la gestion d'une pile.

    http://fr.wikipedia.org/wiki/Algorit..._par_diffusion

    Voici une version que j'ai écrite pour une bibliothèque graphique que je compte diffuser dès qu'elle sera présentable :

    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
     
    {--- Fill ---}
    procedure Fill(Img: PImage; X, Y: SmallInt; Color: TColor);
    type
      TSegment = record
        X1, X2, Y : SmallInt;
      end;
     
    var
      Stack : array of TSegment;
      SP : Integer;
      P : TSegment;
      Background : TColor;
      Left, Top, Right, Bottom : SmallInt;
     
      {-- PushSegment --}
      procedure PushSegment(const S : TSegment);
      begin
        if SP + 1 = Length(Stack) then
          SetLength(Stack, Length(Stack) + 64);
     
        Inc(SP);
        Stack[SP] := S;
      end;
     
      {-- PopSegment --}
      procedure PopSegment(out S : TSegment);
      begin
        S := Stack[SP];
        Dec(SP);
      end;
     
      {-- ExpandLeft --}
      procedure ExpandLeft(var S: TSegment);
      begin
        while S.X1 > Img^.fClipLeft do
          if Point(Img, S.X1 - 1, S.Y) = Background then
            Dec(S.X1)
          else
            Break;
      end;
     
      {-- ExpandRight --}
      procedure ExpandRight(var S: TSegment);
      begin
        while S.X2 < Img^.fClipRight do
          if Point(Img, S.X2 + 1, S.Y) = Background then
            Inc(S.X2)
          else
            Break;
      end;
     
      {-- AddBgColorSegments --}
      procedure AddBgColorSegments(X1, Y, X2: SmallInt);
      var
        I : SmallInt;
        Seg : TSegment;
      begin
        I := X1;
        while I <= X2 do
        begin
          if _Point(Img, I, Y) = Background then
          begin
            Seg.X1 := I;
            Seg.X2 := I;
            Seg.Y := Y;
            ExpandRight(Seg);
            PushSegment(Seg);
            I := Seg.X2 + 1;
          end
          else
            Inc(I);
        end;
      end;
     
    begin
      if not Img^.fDraw then
        Exit;
     
      if (X < Img^.fClipLeft) or (Y > Img^.fClipRight) then
        Exit;
     
      if (Y < Img^.fClipTop) or (Y > Img^.fClipBottom) then
        Exit;
     
      Background := _Point(Img, X, Y);
     
      if Background = Color then
        Exit;
     
      if Img = _Screen then
        LockScreen;
     
      SP := -1;
     
      Left := Img^.fWidth;
      Top := Img^.fHeight;
      Right := -1;
      Bottom := -1;
     
      P.X1 := X;
      P.X2 := X;
      P.Y := Y;
      PushSegment(P);
      while SP > -1 do
      begin
        PopSegment(P);
     
        if (P.Y >= Img^.fClipTop) and (P.Y <= Img^.fClipBottom) then
        begin
          ExpandLeft(P);
          ExpandRight(P);
     
          _HLine(Img, P.X1, P.Y, P.X2, Color);
     
          if P.X1 < Left then
            Left := P.X1;
     
          if P.X2 > Right then
            Right := P.X2;
     
          if P.Y < Top then
            Top := P.Y;
     
          if P.Y > Bottom then
            Bottom := P.Y;
     
          if P.Y - 1 > Img^.fClipTop then
            AddBgColorSegments(P.X1, P.Y - 1, P.X2);
     
          if P.Y + 1 < Img^.fClipBottom then
            AddBgColorSegments(P.X1, P.Y + 1, P.X2);
        end;
      end;
     
      if Img = _Screen then
      begin
        InvalidateRect(Left, Top, Right, Bottom);
        UnlockScreen;
      end;
    end;
    J'ai fait quelques tests et ça semble fonctionner mais c'est sans garantie.

Discussions similaires

  1. algorithme comportant une fonction récursive
    Par TraxX dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 21/02/2008, 17h09
  2. Algorithme de la fonction rand()
    Par Tellmarch dans le forum Algorithmes et structures de données
    Réponses: 12
    Dernier message: 02/03/2007, 21h07
  3. je cherche un algorithme
    Par mehdi_m1986 dans le forum Algorithmes et structures de données
    Réponses: 9
    Dernier message: 03/01/2007, 21h42
  4. cherche l'algorithme d'un programme en language java
    Par rafik larbi dans le forum Langage
    Réponses: 19
    Dernier message: 06/06/2006, 22h05
  5. Cherche le minimum d'une fonction
    Par yakamone3 dans le forum Algorithmes et structures de données
    Réponses: 12
    Dernier message: 06/06/2006, 11h01

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