Gambas: V. 2.23
Escritorio: Gnome 3
Componente gráfico del proyecto: gb.gui
Buenos días a todos,
Escribo porque he buscado por Internet y no he encontrado una respuesta que me ayude.
Estoy realizando un trabajo para la universidad y necesito hacer a mano las funciones primitivas en 2d.
Todo iba bien hasta que llegue a la parte de relleno. Ya que para realizar este algoritmo necesito leer un pixel del DrawingArea que estoy usando y comprobar si es igual al ForeColor.
Pero no he podido encontrar alguna función o alguna forma de sacar el color de un pixel cualquiera.
Espero me puedan ayudar, Gracias de ante mano.
Saludos
He probado DrawingArea1.grab().image[x,y] pero gambas me tira un error.
(graficos:24221): Gdk-CRITICAL **: IA__gdk_draw_drawable: assertion `GDK_IS_DRAWABLE (src)' failed[/color]
Código donde necesito usar la función
PUBLIC SUB relleno(xe AS Integer, ye AS Integer, c1 AS Integer)
DIM colorq AS Integer
colorq = dw.Grab().Image[xe, ye] 'color = LeerPixel(x, y) 'donde dw es mi DrawingArea. esta es una funcion de una clase
IF colorq <> c1 THEN 'Si(color! = col1 & & color! = col2)entonces
Draw.Point(xe, ye) 'PintaPixel(x, y, col1)
relleno(xe + 1, ye, c1) 'Inundaci ón (x+1, y, col1, col2);
relleno(xe - 1, ye, c1) 'Inundaci ón (x-1, y, col1, col2);
relleno(xe, ye + 1, c1) 'Inundaci ón (x, y+1, col1, col2);
relleno(xe, ye - 1, c1) 'Inundaci ón (x, y-1, col1, col2);
ENDIF
DIM colorq AS Integer
colorq = dw.Grab().Image[xe, ye] 'color = LeerPixel(x, y) 'donde dw es mi DrawingArea. esta es una funcion de una clase
IF colorq <> c1 THEN 'Si(color! = col1 & & color! = col2)entonces
Draw.Point(xe, ye) 'PintaPixel(x, y, col1)
relleno(xe + 1, ye, c1) 'Inundaci ón (x+1, y, col1, col2);
relleno(xe - 1, ye, c1) 'Inundaci ón (x-1, y, col1, col2);
relleno(xe, ye + 1, c1) 'Inundaci ón (x, y+1, col1, col2);
relleno(xe, ye - 1, c1) 'Inundaci ón (x, y-1, col1, col2);
ENDIF
Clase por si la necesitan
' gambas class file
STATIC x0 AS Integer = CInt(552 / 2)
STATIC y0 AS Integer = CInt(432 / 2)
PRIVATE x1 AS Integer
PRIVATE x2 AS Integer
PRIVATE y1 AS Integer
PRIVATE y2 AS Integer
PRIVATE pic AS Picture
PRIVATE dw AS DrawingArea
PUBLIC SUB ini(dx AS DrawingArea)
dw = dx
Draw.Begin(dw)
dw.Visible = TRUE
dw.Clear
END SUB
PUBLIC SUB colores(fcolor AS Float, bcolor AS Float)
Draw.ForeColor = fcolor
Draw.Background = bcolor
END
PUBLIC SUB puntito(a AS Integer, b AS Integer)
Draw.Point(a - 1, b - 1)
Draw.Point(a, b - 1)
Draw.Point(a - 1, b)
Draw.Point(a, b)
Draw.Point(a + 1, b + 1)
Draw.Point(a + 1, b)
Draw.Point(a, b + 1)
Draw.Point(a - 1, b + 1)
Draw.Point(a + 1, b - 1)
END
PUBLIC SUB asignar(x11 AS Integer, x22 AS Integer, y11 AS Integer, y22 AS Integer)
x1 = x11
x2 = x22
y1 = y11
y2 = y22
END
PUBLIC SUB cambiarx(x11 AS Float, x22 AS Float)
DIM x3 AS Float
x3 = x1
x1 = x2
x2 = x3
END SUB
PUBLIC SUB cambiary(y11 AS Float, y22 AS Float)
DIM y3 AS Float
y3 = y1
y1 = y2
y2 = y3
END SUB
PUBLIC SUB dda()
DIM x AS Integer
DIM dx, dy, y, m AS Float
DIM nm AS Float
IF x1 > x2 THEN
cambiarx(x1, x2)
cambiary(y1, y2)
ENDIF
dy = y2 - y1
dx = x2 - x1
IF dx = 0 THEN
m = 1
ELSE
m = dy / dx
END IF
IF Abs(m) <= 1 THEN
y = y1
FOR x = x1 TO x2
'poner punto
puntito(x, CInt(y))
y = y + m
NEXT
ELSE
y = x1
FOR x = y1 TO y2
'poner punto
puntito(CInt(y), x)
y = y + (1 / m)
NEXT
END IF
END SUB
PUBLIC SUB circulo(x0 AS Integer, y0 AS Integer)
DIM radio, x, xc, yc, y AS Integer
DIM dx, dy AS Float
DIM p AS Float
IF x1 > x2 THEN
cambiarx(x1, x2)
cambiary(y1, y2)
ENDIF
dy = y2 - y1
dx = x2 - x1
radio = CInt(Round(Sqr((dy * dy) + (dx * dx)) / 2, 1))
yc = y0 + radio
xc = x0 + radio
y = radio
puntito(xc, y0) 'Pintar Pixel(0, R)
puntito(xc, yc + radio)
puntito(xc + radio, yc)
puntito(xc - radio, yc)
p = 1 - radio 'calcular p0 = 5 / 4 – R // si R es entero, p0 = 1-r
WHILE x <= y 'para cada xk
IF p < 0 THEN 'si pk < 0
'arco arriba
puntito(x + xc + 1, yc - y) 'Pintar Pixel(xk + 1, yk)
puntito(- x + xc + 1, yc - y)
'arco abajo
puntito(- x + xc + 1, yc + y)
puntito(x + xc + 1, yc + y)
'arco izq
puntito(- y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(- y + xc + 1, yc - x)
'arco derecho
puntito(y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(y + xc + 1, yc - x)
p = p + 2 * x + 1 'pk + 1 = pk + 2 xk + 1
ELSE IF p > 0 THEN 'si pk > 0
p = p + (2 * x) - (2 * y) + 1 'pk + 1 = pk + 2 xk – 2yk+ 1
y = y - 1
'arco arriba
puntito(x + xc + 1, yc - y) 'Pintar Pixel(xk + 1, yk)
puntito(- x + xc + 1, yc - y)
'arco abajo
puntito(- x + xc + 1, yc + y)
puntito(x + xc + 1, yc + y)
'arco izq
puntito(- y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(- y + xc + 1, yc - x)
'arco derecho
puntito(y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(y + xc + 1, yc - x)
ENDIF
x += 1
WEND
'puntito(x + xc, y - yc)
END SUB
PUBLIC SUB asignari()
'pic =
END SUB
PUBLIC SUB relleno(xe AS Integer, ye AS Integer, c1 AS Integer)
DIM colorq AS Integer
colorq = dw.Grab().Image[xe, ye] 'color = LeerPixel(x, y)
IF colorq <> c1 THEN 'Si(color! = col1 & & color! = col2)entonces
Draw.Point(xe, ye) 'PintaPixel(x, y, col1)
relleno(xe + 1, ye, c1) 'Inundaci ón (x+1, y, col1, col2);
relleno(xe - 1, ye, c1) 'Inundaci ón (x-1, y, col1, col2);
relleno(xe, ye + 1, c1) 'Inundaci ón (x, y+1, col1, col2);
relleno(xe, ye - 1, c1) 'Inundaci ón (x, y-1, col1, col2);
ENDIF
END SUB
STATIC x0 AS Integer = CInt(552 / 2)
STATIC y0 AS Integer = CInt(432 / 2)
PRIVATE x1 AS Integer
PRIVATE x2 AS Integer
PRIVATE y1 AS Integer
PRIVATE y2 AS Integer
PRIVATE pic AS Picture
PRIVATE dw AS DrawingArea
PUBLIC SUB ini(dx AS DrawingArea)
dw = dx
Draw.Begin(dw)
dw.Visible = TRUE
dw.Clear
END SUB
PUBLIC SUB colores(fcolor AS Float, bcolor AS Float)
Draw.ForeColor = fcolor
Draw.Background = bcolor
END
PUBLIC SUB puntito(a AS Integer, b AS Integer)
Draw.Point(a - 1, b - 1)
Draw.Point(a, b - 1)
Draw.Point(a - 1, b)
Draw.Point(a, b)
Draw.Point(a + 1, b + 1)
Draw.Point(a + 1, b)
Draw.Point(a, b + 1)
Draw.Point(a - 1, b + 1)
Draw.Point(a + 1, b - 1)
END
PUBLIC SUB asignar(x11 AS Integer, x22 AS Integer, y11 AS Integer, y22 AS Integer)
x1 = x11
x2 = x22
y1 = y11
y2 = y22
END
PUBLIC SUB cambiarx(x11 AS Float, x22 AS Float)
DIM x3 AS Float
x3 = x1
x1 = x2
x2 = x3
END SUB
PUBLIC SUB cambiary(y11 AS Float, y22 AS Float)
DIM y3 AS Float
y3 = y1
y1 = y2
y2 = y3
END SUB
PUBLIC SUB dda()
DIM x AS Integer
DIM dx, dy, y, m AS Float
DIM nm AS Float
IF x1 > x2 THEN
cambiarx(x1, x2)
cambiary(y1, y2)
ENDIF
dy = y2 - y1
dx = x2 - x1
IF dx = 0 THEN
m = 1
ELSE
m = dy / dx
END IF
IF Abs(m) <= 1 THEN
y = y1
FOR x = x1 TO x2
'poner punto
puntito(x, CInt(y))
y = y + m
NEXT
ELSE
y = x1
FOR x = y1 TO y2
'poner punto
puntito(CInt(y), x)
y = y + (1 / m)
NEXT
END IF
END SUB
PUBLIC SUB circulo(x0 AS Integer, y0 AS Integer)
DIM radio, x, xc, yc, y AS Integer
DIM dx, dy AS Float
DIM p AS Float
IF x1 > x2 THEN
cambiarx(x1, x2)
cambiary(y1, y2)
ENDIF
dy = y2 - y1
dx = x2 - x1
radio = CInt(Round(Sqr((dy * dy) + (dx * dx)) / 2, 1))
yc = y0 + radio
xc = x0 + radio
y = radio
puntito(xc, y0) 'Pintar Pixel(0, R)
puntito(xc, yc + radio)
puntito(xc + radio, yc)
puntito(xc - radio, yc)
p = 1 - radio 'calcular p0 = 5 / 4 – R // si R es entero, p0 = 1-r
WHILE x <= y 'para cada xk
IF p < 0 THEN 'si pk < 0
'arco arriba
puntito(x + xc + 1, yc - y) 'Pintar Pixel(xk + 1, yk)
puntito(- x + xc + 1, yc - y)
'arco abajo
puntito(- x + xc + 1, yc + y)
puntito(x + xc + 1, yc + y)
'arco izq
puntito(- y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(- y + xc + 1, yc - x)
'arco derecho
puntito(y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(y + xc + 1, yc - x)
p = p + 2 * x + 1 'pk + 1 = pk + 2 xk + 1
ELSE IF p > 0 THEN 'si pk > 0
p = p + (2 * x) - (2 * y) + 1 'pk + 1 = pk + 2 xk – 2yk+ 1
y = y - 1
'arco arriba
puntito(x + xc + 1, yc - y) 'Pintar Pixel(xk + 1, yk)
puntito(- x + xc + 1, yc - y)
'arco abajo
puntito(- x + xc + 1, yc + y)
puntito(x + xc + 1, yc + y)
'arco izq
puntito(- y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(- y + xc + 1, yc - x)
'arco derecho
puntito(y + xc + 1, yc + x) 'Pintar Pixel(xk + 1, yk)
puntito(y + xc + 1, yc - x)
ENDIF
x += 1
WEND
'puntito(x + xc, y - yc)
END SUB
PUBLIC SUB asignari()
'pic =
END SUB
PUBLIC SUB relleno(xe AS Integer, ye AS Integer, c1 AS Integer)
DIM colorq AS Integer
colorq = dw.Grab().Image[xe, ye] 'color = LeerPixel(x, y)
IF colorq <> c1 THEN 'Si(color! = col1 & & color! = col2)entonces
Draw.Point(xe, ye) 'PintaPixel(x, y, col1)
relleno(xe + 1, ye, c1) 'Inundaci ón (x+1, y, col1, col2);
relleno(xe - 1, ye, c1) 'Inundaci ón (x-1, y, col1, col2);
relleno(xe, ye + 1, c1) 'Inundaci ón (x, y+1, col1, col2);
relleno(xe, ye - 1, c1) 'Inundaci ón (x, y-1, col1, col2);
ENDIF
END SUB