Java - relleno de poligonos

 
Vista:

relleno de poligonos

Publicado por Diego (1 intervención) el 16/05/2002 05:36:06
Hola
Sería posible que alguien me diera o me dijera dónde puedo encontrar un algoritmo de relleno de polígonos en Java? He tratado de implementar uno, pero no me ha resultado.
Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

relleno de poligonos

Publicado por reptar (1 intervención) el 20/03/2013 01:13:47
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// aunque tarde pero apenas pude te trate de responder espero sirva a las otras 3300 personas que lo buscaban
// aquí coloco la web donde lo saque:
// http://hanconina.nubeuniversitaria.com/unsaac/computacion-grafica-i/laboratorio-de-computacion-grafica-i/laboratorio4rellenopoligonal
 
/*
 * Creado el 23 de mayo, 2012 por Hernán Nina Hanco
 *
 * Este trabajo es parte del proyecto CG1, que corresponde a la 
 * implementación de algoritmos de Dibujo de graficas.
 * 
 * Universidad Nacional de San Antonio Abad del Cusco
 * Carrera Profesional de Ingeniería Informática y de Sistemas
 * Asignatura: Computación Gráfica I
 */
package geometrias.rellenoareas;
/*
 * Dibujo de líneas con pendiente -1<m<1
 * @author Hernan Nina Hanco 
 */
 
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.*;
 
public class Renderer_RellenoPoligonal implements GLEventListener {
 
    static final Logger logger = Logger.getLogger("BasicLoggingExample");
    protected GL2 gl;
    /*
     * Inicializar graficador OpenGL
     */
 
    @Override
    public void init(GLAutoDrawable gLDrawable) {
        logger.log(Level.INFO, "método - init");
        // Provee un objeto que enlaza las APIs del OpenGL
        // Que se encargara de realizar las instrucciones de dibujos 
        gl = gLDrawable.getGL().getGL2();
 
        // Color de fondo del GLCanvas
        gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
 
        // definir el color del pincel
        gl.glColor3f(1.0f, 0.0f, 0.0f);
 
    }
    /*
     * Método para actualizar el dibujo cuando,
     * se modifica el tamaño de la ventana.
     */
 
    @Override
    public void reshape(GLAutoDrawable glad, int x, int y, int width,
            int height) {
        logger.log(Level.INFO, "Reshape");
        // 7. Especificar el área de dibujo (frame) utilizamos coordenadas
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(-width, width, -height, height, -1.0, 1.0);
    }
 
    @Override
    public void dispose(GLAutoDrawable glad) {
        // no implementado
    }
 
    /*
     * Inicializar y presentar el dibujo de OpenGL
     */
    @Override
    public void display(GLAutoDrawable drawable) {
        // Establecer el tamaño y color del punto     
        gl.glPointSize(1);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
 
        // Dibujar polilinea cerrada
        List<Punto> vertices = new ArrayList<Punto>();
        vertices.add(new Punto(0, 0, 0));
        vertices.add(new Punto(100, 0, 0));
        vertices.add(new Punto(200, 100, 0));
        vertices.add(new Punto(300, 0, 0));
        vertices.add(new Punto(300, 300, 0));
        vertices.add(new Punto(0, 300, 0));
        dibujarPolilineasCerrada(vertices);
        // Identificar poligono concavo
        // identificarPoligonoConcavo();
    }
    /*
     * Dibujar un poligono utilizando polilinea cerrada
     */
    private void dibujarPolilineasCerrada(List<Punto> vertices) {
        int numeroVertices = vertices.size();
        for (int i = 0; i < numeroVertices - 1; i++) {
            int x0 = vertices.get(i).getX();
            int y0 = vertices.get(i).getY();
            int xn = vertices.get(i + 1).getX();
            int yn = vertices.get(i + 1).getY();
            lineaBresenham(x0, y0, xn, yn);
        }
        // cerrar la figura
        int x0 = vertices.get(numeroVertices - 1).getX();
        int y0 = vertices.get(numeroVertices - 1).getY();
        int xn = vertices.get(0).getX();
        int yn = vertices.get(0).getY();
        lineaBresenham(x0, y0, xn, yn);
    }
    /*
     * Dibujar línea arbitraria con metodo bresenham 
     */
    public void lineaBresenham(int x0, int y0, int xn, int yn) {
        int dx, dy, incrE, incrNE, d, x, y, flag = 0;
 
        if (xn < x0) {
            //intercambiar(x0,xn);
            int temp = x0;
            x0 = xn;
            xn = temp;
 
            //intercambiar(y0,yn);
            temp = y0;
            y0 = yn;
            yn = temp;
        }
        if (yn < y0) {
            y0 = -y0;
            yn = -yn;
            flag = 10;
        }
 
        dy = yn - y0;
        dx = xn - x0;
 
        if (dx < dy) {
            //intercambiar(x0,y0);
            int temp = x0;
            x0 = y0;
            y0 = temp;
 
            //intercambiar(xn,yn);
            temp = xn;
            xn = yn;
            yn = temp;
 
            //intercambiar(dy,dx);
            temp = dy;
            dy = dx;
            dx = temp;
            flag++;
        }
 
        x = x0;
        y = y0;
        d = 2 * dy - dx;
        incrE = 2 * dy;
        incrNE = 2 * (dy - dx);
 
        while (x < xn + 1) {
            escribirPixel(x, y, flag); /* Dibujar punto */
            x++; /* siguiente pixel */
            if (d <= 0) {
                d += incrE;
            } else {
                y++;
                d += incrNE;
            }
        }
    }
 
    void escribirPixel(int x, int y, int flag) {
        int xf = x, yf = y;
        if (flag == 1) {
            xf = y;
            yf = x;
        } else if (flag == 10) {
            xf = x;
            yf = -y;
        } else if (flag == 11) {
            xf = y;
            yf = -x;
        }
        dibujarPunto(xf, yf);
    }
 
    /*
     * Dibujar un punto 
     */
    protected void dibujarPunto(int x, int y) {
        gl.glPointSize(2);
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2i(x, y);
        gl.glEnd();
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar