DANIEL EDUARDO PEDROZA PEDRAZA
[email protected]
ALGORITMO ANTIALIASING
#include "glut.h"
#include <math.h>
float ancho = 300, alto = 300;
int CX1, CX2, CY1, CY2;
void pintaPixel(int x, int y, int r, int v, int a){
glPointSize(1);
glBegin(GL_POINTS);
glColor3f(r, v, a);
glVertex2f(x, y);
glEnd();
}
void pintalinea(int CX1, int CY1, int CX2, int CY2) {
int DX, DY, x, y;
float m, b;
DX = CX2 - CX1;
DY = CY2 - CY1;
if (DX == 0)
{
if (CY1 <= CY2)
{
for (y = CY1; y <= CY2; y++)
{
pintaPixel(CX1, y, 1, 1, 1);
}
}
else
{
for (y = CY2; y <= CY1; y++)
{
pintaPixel(CX1, y, 1, 1, 1);
}
}
}
if (DY == 0)
{
if (CX1 <= CX2)
{
for (x = CX1; x <= CX2; x++)
{
pintaPixel(x, CY1, 1, 1, 1);
pintaPixel(x + 1, CY1, 1, 1, 1);
}
}
else
{
for (x = CX2; x <= CX1; x++)
{
pintaPixel(x, CY1, 1, 1, 1);
pintaPixel(x + 1, CY1, 1, 1, 1);
}
}
}
else
{
m = float(DY) / float(DX);
DANIEL EDUARDO PEDROZA PEDRAZA
[email protected] b = CY1 - (m*CX1);
if (abs(DX) >= abs(DY))
{
if (CX1 <= CX2)
{
for (x = CX1; x <= CX2; x++)
{
y = (m*x) + b;
pintaPixel(x, y, 1, 1, 1);
pintaPixel(x, y + 1, 0.5, 0.5, 0.5);
pintaPixel(x, y - 1, 0.5, 0.5, 0.5);
}
}
else
{
for (x = CX2; x <= CX1; x++)
{
y = (m*x) + b;
pintaPixel(x, y, 1, 1, 1);
pintaPixel(x, y + 1, 0.5, 0.5, 0.5);
pintaPixel(x, y - 1, 0.5, 0.5, 0.5);
}
}
}
else
{
if (CY1 <= CY2)
{
for (y = CY1; y <= CY2; y++)
{
x = (y - b) / m;
pintaPixel(x, y, 1, 1, 1);
pintaPixel(x+1, y, 0.5, 0.5, 0.5);
pintaPixel(x-1, y, 0.5, 0.5, 0.5);
}
}
else
{
for (y = CY2; y <= CY1; y++)
{
x = (y - b) / m;
pintaPixel(x, y, 1, 1, 1);
pintaPixel(x + 1, y, 0.5, 0.5, 0.5);
pintaPixel(x - 1, y, 0.5, 0.5, 0.5);
}
}
}
}
}
void raton(int btm, int state, int x, int y) {
if (state == GLUT_DOWN && btm == GLUT_RIGHT_BUTTON) {
CX1 = x - (ancho / 2);
CY1 = (alto / 2) - y;
pintaPixel(CX1, CY1, 1, 1, 1);
}
if (state == GLUT_DOWN && btm == GLUT_LEFT_BUTTON) {
CX2 = x - (ancho / 2);
CY2 = (alto / 2) - y;
pintaPixel(CX2, CY2, 1, 1, 1);
DANIEL EDUARDO PEDROZA PEDRAZA
[email protected] pintalinea(CX1, CY1, CX2, CY2);
}
glFlush();
}
void plano() {
for (float i = -(alto / 2); i < alto; i++) {
pintaPixel(0, i, 1, 1, 1);
}
for (float i = -(ancho / 2); i < ancho; i++) {
pintaPixel(i, 0, 1, 1, 1);
}
glFlush();
}
int main(int argc, char**argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(ancho, alto);
glutCreateWindow("ANTIALIASING");
gluOrtho2D(-(ancho / 2), (ancho / 2), -(alto / 2), (alto / 2));
glClearColor(0, 0, 0, 0);
glutDisplayFunc(plano);
glutMouseFunc(raton);
glutMainLoop();
return 0;
}