Mostrando entradas con la etiqueta MiniWin. Mostrar todas las entradas
Mostrando entradas con la etiqueta MiniWin. Mostrar todas las entradas

domingo, 11 de octubre de 2020

Tangram con MiniWin

En el siguiente ejemplo se van a recrear las piezas de un tangram empleando triángulos rectángulos para formarlas.

Esto implica crear una o varias funciones para dibujar los triángulos rectángulos. En este caso se optó por usar cuatro funciones llamadas:

  • tipoA
  • tipoB
  • tipoC
  • tipoD

Estas podrían ser "inline", pero se optó por funciones convencionales, las funciones serían:

/*Trabajando con 4 tipos de triángulos rectángulos*/
void tipoA(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1+ta-i,y1+i,x1+ta,y1+i);	
	}
}
void tipoB(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1,y1+i,x1+i,y1+i);	
	}
}
void tipoC(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1+i,y1+i,x1+ta,y1+i);	
	}
}
void tipoD(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1,y1+i,x1+ta-i,y1+i);	
	}
}

Donde cada función corresponde a:

Tipo A Tipo B
    *
   **
  ***
 ****
*****
*
**
***
****
*****
Tipo C Tipo D
*****
 ****
  ***
   **
    *
*****
****
***
**
*

El código completo sería:

#include "miniwin.h"
using namespace miniwin;

const int DIM = 200; // Valor medio del ancho

/*Funciones */
void tipoA(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1+ta-i,y1+i,x1+ta,y1+i);	
	}
}
void tipoB(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1,y1+i,x1+i,y1+i);	
	}
}
void tipoC(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1+i,y1+i,x1+ta,y1+i);	
	}
}
void tipoD(int x1, int y1, int ta){
	for(int i=0; i<=ta; i++){		
		linea(x1,y1+i,x1+ta-i,y1+i);	
	}
}

int main(){
	vredimensiona(2*DIM, 2*DIM);	
	color_rgb(0, 0, 0);
	rectangulo_lleno(0, 0, 2*DIM, 2*DIM);
	
	/*Pieza 1*/
	color(AZUL);
	tipoB(0,0,DIM);
	tipoD(0,DIM,DIM);
	
	/*Pieza 2*/
	color_rgb(255, 150, 0);	
	tipoC(0,0,DIM);
	tipoD(DIM,0,DIM);	
	
	/*Pieza 3*/
	color(VERDE);	
	tipoA(DIM*1.5,0,DIM/2);
	rectangulo_lleno(DIM*1.5,DIM/2,2*DIM,DIM);
	tipoD(DIM*1.5,DIM,DIM/2);
	
	/*Pieza 4*/
	color(ROJO);	
	tipoA(DIM,DIM,DIM);
	
	/*Pieza 5*/
	color(MAGENTA);	
	tipoA(0,DIM*1.5,DIM/2);
	tipoB(DIM/2,DIM*1.5,DIM/2);
	
	/*Pieza 6*/
	color(AMARILLO);	
	tipoA(DIM/2,DIM,DIM/2);
	tipoB(DIM,DIM,DIM/2);
	tipoC(DIM/2,DIM*1.5,DIM/2);
	tipoD(DIM,DIM*1.5,DIM/2);
	
	/*Pieza 7*/
	color_rgb(100,0,155);	
	tipoA(DIM,DIM/2,DIM/2);
	tipoC(DIM,DIM,DIM/2);
	
	refresca();
	return 0;

}

El resultado visual sería: