OVH Cloud OVH Cloud

Tracé d'un polygone + fill ???

3 réponses
Avatar
St
Bonsoir et bonne année.

Je suis avec VB 6 pro et je cherche à tracer des polygones et à les remplir
d'une "certaine" couleur.
Pour un carré ou rectangle c'est line (x,y)-(x,y),couleur, BF
Pour un cercle idem facile.

Mais pour un losange ou autre parallélépipède comment fait-on, je sais le
tracer avec line, mais après pour le remplir d'une certaine couleur ?

Merci de votre aide.

3 réponses

Avatar
François Picalausa
Bonjour/soir,

Tu peux jeter un oeil à cet exemple sur mon site:
http://www.chez.com/fpicalausa/programmation/VB/Code/DrawArrow.htm

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"St" a écrit dans le message de
news:%
Bonsoir et bonne année.

Je suis avec VB 6 pro et je cherche à tracer des polygones et à les


remplir
d'une "certaine" couleur.
Pour un carré ou rectangle c'est line (x,y)-(x,y),couleur, BF
Pour un cercle idem facile.

Mais pour un losange ou autre parallélépipède comment fait-on, je sais le
tracer avec line, mais après pour le remplir d'une certaine couleur ?

Merci de votre aide.




Avatar
Jark
Le code qui suit est en Basic, mais sa logique est
indépendante du langage. Antoni Gual est un des maîtres de
Quick-Basic, son module est un des plus performants en
langage clair (sans ASM donc).

Pour en voir une application dans un programme complet,
vous pouvez charger TC-Draw, programme complet type MS-
Paint en SVGA sous QB :

http://www.zarbi27.com/apophis/

SUB Fill24 (x0%, y0%)
' This flood-fill algorithm was adapted from a program by
Antoni Gual
' who adapted it from Paul Heckbert's C code in Graphics
Gems

DrawRed% = Red%: DrawGreen% = Green%: DrawBlue% = Blue%
MaxX% = ScrWidth - 1
StackSize% = 2000

REDIM Stack%(StackSize%, 4)

IF CompCol%(x0%, y0%, DrawRed%, DrawGreen%, DrawBlue%) = 1
THEN EXIT SUB

Point24 x0%, y0%: BRed% = Red%: BGreen% = Green%: BBlue% =
Blue%


Stack%(0, 3) = y0% + 1
Stack%(0, 1) = x0%
Stack%(0, 2) = x0%
Stack%(0, 4) = -1
Sp% = 1

WHILE Sp2% <> Sp%

' Retrieve the data from a previous filled line and jump
to the bottom line
dy% = Stack%(Sp2%, 4)
y% = Stack%(Sp2%, 3) + dy%
x1% = Stack%(Sp2%, 1)
x2 = Stack%(Sp2%, 2)
Sp2% = Sp2% + 1
IF Sp2% > StackSize% THEN Sp2% = 0

' Any point in the bottom line in contact with at least a
point of it
' must be filled. Try points left of the left side of the
previous line:
' Get a left side
x% = x1%
WHILE x% > -1 AND CompCol%(x%, y%, BRed%, BGreen%, BBlue%)
= 1: x% = x% - 1: WEND
IF x% >= x1% THEN GOTO Skip
L% = x% + 1

' Store this part as filled
IF L% < x1% THEN
Stack%(Sp%, 3) = y%
Stack%(Sp%, 1) = L%
Stack%(Sp%, 2) = x1% - 1
Stack%(Sp%, 4) = -dy%
Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0
END IF

x% = x1% + 1
DO
WHILE x% <= MaxX% AND CompCol%(x%, y%, BRed%, BGreen%,
BBlue%) = 1: x% = x% + 1: WEND

RGB DrawRed%, DrawGreen%, DrawBlue%
FOR i% = L% TO x% - 1
Pset24 i%, y%
NEXT i%

Stack%(Sp%, 3) = y%: Stack%(Sp%, 1) = L%: Stack%(Sp%, 2) =
x% - 1: Stack%(Sp%, 4) = dy%: Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0

IF x% > x2 + 1 THEN
Stack%(Sp%, 3) = y%
Stack%(Sp%, 1) = x2 + 1
Stack%(Sp%, 2) = x% - 1
Stack%(Sp%, 4) = -dy%
Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0
END IF

Skip:
x% = x% + 1
WHILE (x% <= x2) AND CompCol%(x%, y%, BRed%, BGreen%,
BBlue%) = 0: x% = x% + 1: WEND
L% = x%

LOOP WHILE x% <= x2
WEND

ERASE Stack%

END SUB
Avatar
Christophe
Bonjour,

Pour ceux que ça interresse et qui voudraient des explications
complementaire, il existe un exellent livre de Rémy Malgouyres au éditions
DUNOD "Algorithmes pour la synthese d'images et l'animation 3D"
Les premiers chapitres traitant de la 2D, notamment le remplissage des
polygones.

Christophe Vergon

"Jark" a écrit dans le message de news:
03c701c3d83f$8802a5c0$
Le code qui suit est en Basic, mais sa logique est
indépendante du langage. Antoni Gual est un des maîtres de
Quick-Basic, son module est un des plus performants en
langage clair (sans ASM donc).

Pour en voir une application dans un programme complet,
vous pouvez charger TC-Draw, programme complet type MS-
Paint en SVGA sous QB :

http://www.zarbi27.com/apophis/

SUB Fill24 (x0%, y0%)
' This flood-fill algorithm was adapted from a program by
Antoni Gual
' who adapted it from Paul Heckbert's C code in Graphics
Gems

DrawRed% = Red%: DrawGreen% = Green%: DrawBlue% = Blue%
MaxX% = ScrWidth - 1
StackSize% = 2000

REDIM Stack%(StackSize%, 4)

IF CompCol%(x0%, y0%, DrawRed%, DrawGreen%, DrawBlue%) = 1
THEN EXIT SUB

Point24 x0%, y0%: BRed% = Red%: BGreen% = Green%: BBlue% Blue%


Stack%(0, 3) = y0% + 1
Stack%(0, 1) = x0%
Stack%(0, 2) = x0%
Stack%(0, 4) = -1
Sp% = 1

WHILE Sp2% <> Sp%

' Retrieve the data from a previous filled line and jump
to the bottom line
dy% = Stack%(Sp2%, 4)
y% = Stack%(Sp2%, 3) + dy%
x1% = Stack%(Sp2%, 1)
x2 = Stack%(Sp2%, 2)
Sp2% = Sp2% + 1
IF Sp2% > StackSize% THEN Sp2% = 0

' Any point in the bottom line in contact with at least a
point of it
' must be filled. Try points left of the left side of the
previous line:
' Get a left side
x% = x1%
WHILE x% > -1 AND CompCol%(x%, y%, BRed%, BGreen%, BBlue%)
= 1: x% = x% - 1: WEND
IF x% >= x1% THEN GOTO Skip
L% = x% + 1

' Store this part as filled
IF L% < x1% THEN
Stack%(Sp%, 3) = y%
Stack%(Sp%, 1) = L%
Stack%(Sp%, 2) = x1% - 1
Stack%(Sp%, 4) = -dy%
Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0
END IF

x% = x1% + 1
DO
WHILE x% <= MaxX% AND CompCol%(x%, y%, BRed%, BGreen%,
BBlue%) = 1: x% = x% + 1: WEND

RGB DrawRed%, DrawGreen%, DrawBlue%
FOR i% = L% TO x% - 1
Pset24 i%, y%
NEXT i%

Stack%(Sp%, 3) = y%: Stack%(Sp%, 1) = L%: Stack%(Sp%, 2) x% - 1: Stack%(Sp%, 4) = dy%: Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0

IF x% > x2 + 1 THEN
Stack%(Sp%, 3) = y%
Stack%(Sp%, 1) = x2 + 1
Stack%(Sp%, 2) = x% - 1
Stack%(Sp%, 4) = -dy%
Sp% = Sp% + 1
IF Sp% > StackSize% THEN Sp% = 0
END IF

Skip:
x% = x% + 1
WHILE (x% <= x2) AND CompCol%(x%, y%, BRed%, BGreen%,
BBlue%) = 0: x% = x% + 1: WEND
L% = x%

LOOP WHILE x% <= x2
WEND

ERASE Stack%

END SUB