You are on page 1of 7

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package geometrias.poligonos;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.*;
/**
*
* @author Eder
*/
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.12f, 0.50f, 0.26f);

// Dibujar polilinea cerrada


List<Punto> vertices = new ArrayList<Punto>();
//si obedece el orden
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, 300, 0));
vertices.add(new Punto(150, 250, 0));
vertices.add(new Punto(0, 300, 0));

dibujarPolilineasCerrada(vertices);
int NroVertices = vertices.size();
System.out.println(NroVertices);

ConvertirConcavo(vertices);
dividirTriangulos(vertices);
// Identificar poligono concavo
if (Concavo_Convexo(vertices).size()>0)
System.out.println("El poligono es concavo");
else
System.out.println("El poligono es convexo");
}

////////////////////////////////////////////////////////
/*Convierte una lista de puntos en una lista de vectores
(uniendo los puntos)*/
private static List<Vector> ListaVectores(List<Punto>
vertices){
List<Vector> Vectores= new ArrayList<Vector>();
for (int i = 0; i < vertices.size()-1; i++)
{
//determnar los vectores
Vectores.add(new Vector(vertices.get(i),
vertices.get(i+1)));
}
Vectores.add(new Vector(vertices.get(vertices.size()-1),
vertices.get(0)));
return Vectores;
}

/*Divide en triangulos un poligono convexo*/


private void dividirTriangulos(List<Punto> vertices){
dibujarPolilineasCerrada(vertices);
int numeroVertices = vertices.size();int i =0;
while(numeroVertices>3){
int x0 = vertices.get(0).getX();
int y0 = vertices.get(0).getY();
int xn = vertices.get(i + 2).getX();
int yn = vertices.get(i + 2).getY();
lineaBresenham(x0, y0, xn, yn);
numeroVertices--;
i++;
}
}

/*Devuelve una lista con los vectores que deben de ser


prolongados*/
public static List<Vector> Concavo_Convexo(List<Punto>
vertices)
{
//ingresar los puntos en orden
List<Vector> Rectas_Prolongar= new ArrayList<Vector>();
List<Vector> Vectores= new ArrayList<Vector>();
Vectores=ListaVectores(vertices);
for (int j = 0; j < Vectores.size() -1; j++)
{
//obtener el producto cartesiano
int z= Vectores.get(j).getValor().getX() *
Vectores.get(j+1).getValor().getY() -
(Vectores.get(j+1).getValor().getX() *
Vectores.get(j).getValor().getY());
if (z<0) { //concavo
Rectas_Prolongar.add(Vectores.get(j));
}
}
return Rectas_Prolongar;
}

/*Devuelve el punto de inteseccion de un vector con otro en


caso exista*/
private Punto Intersectar(Vector a, List<Vector> lista){
double m1 = a.Pendiente;
double b1 = a.Inicial.getY()-(m1*a.Inicial.getX());
double x=0,y=0;
//buscar un punto de interseccion del vector a con algun
otro vectores
//Verificar si se trata del mismo vector
for (int i = 0;i<lista.size();i++){
if (!a.equals(lista.get(i))){
//determinar si existe punto de interseccion
if(!RectasConsecutivas(a, lista.get(i))){
Vector vec = lista.get(i);
double m2=vec.Pendiente;
double b2 = vec.Inicial.getY()-
(m2*vec.Inicial.getX());
x = (b2-b1)/(m1-m2);
y = m1*x+b1;
break;
}
}
}
return new Punto((int)Math.floor(x),(int)Math.floor(y),0);
}

/*Determina si una recta es consecutiva con otra o no*/


private boolean RectasConsecutivas(Vector a,Vector b){
if (a.Inicial.equals(b.Inicial)||
(a.Final.equals(b.Inicial))||a.Inicial.equals(b.Final)||
a.Final.equals(b.Final)){return true;}
else{return false;}
}

/*Dibuja la recta prolongada*/


private void dibujarInterseccion(List<Vector>
lista,List<Vector> vectores){
for (int i=0;i<lista.size();i++){
Punto punto = Intersectar(lista.get(i), vectores);
if (punto !=null){
int xn = punto.getX();
int yn = punto.getY();
int x0 = lista.get(i).Inicial.getX();
int y0 = lista.get(i).Inicial.getY();
lineaBresenham(x0, y0, xn, yn);
}
}

}
/*Convierte un poligono concavo en uno convexo*/
private void ConvertirConcavo(List<Punto> vertices){
List<Vector> vectores= new ArrayList<Vector>();
vectores=ListaVectores(vertices);
if (Concavo_Convexo(vertices).size()>0){
dibujarInterseccion(Concavo_Convexo(vertices),
vectores);
}
else dibujarPolilineasCerrada(vertices);
}
/////////////////////////////////////////////////

/*
*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();
}
}

You might also like