Kamis, 06 Desember 2012

Algoritma Elipse dan Midpoint Elipse


// AHMAD FADLY DZIL JALAL [2010 470 121]
// @ ALL RIGHTS RESERVED

#include "stdafx.h"
#include <glut.h>

GLuint global_w = 1200;
GLuint global_h = 600;
float putaran_A = 0;
float putaran_B = 0;
int sisi = 50;

void init();
void idle();
void display();
void reshape(int w, int h);
void ElipseSederhana(float xc, float yc, float x, float y);
void elipseMidpoint(float xc, float yc, float rx, float ry);

int _tmain(int argc, char* argv[])
{
    glutInit(&argc,argv );
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(50,50);
glutInitWindowSize(global_w,global_h);
glutCreateWindow ("Tugas");
glutSetWindowTitle( "Elipse" );
    init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
    //glutIdleFunc(idle);
glutMainLoop();
return 0;
}

void init()
{
// clear background
glClearColor(0.0,0.0,0.0,1.0);

glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();


glOrtho(-global_w/2,global_w/2,-global_h/2,global_h/2,-1.0,1.0);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
};



void display (){
    glClear(GL_COLOR_BUFFER_BIT);
   

    glPushMatrix();
    glTranslatef(sisi,sisi,0);  
glRotatef(putaran_B,0.0,0.0,0.0); //SUDUT, X, Y, Z
glTranslatef(-sisi,-sisi,0);
    glBegin (GL_POINTS);  
    glColor3f (1,1,0);
    elipseMidpoint(600, 300, 100 , 40);
    glEnd();
    glPopMatrix();
glutSwapBuffers();
   
    glPushMatrix();
    glTranslatef(sisi,sisi,0);  
glRotatef(putaran_A,0.0,0.0,0.0); //SUDUT, X, Y, Z
glTranslatef(-sisi,-sisi,0);
    glBegin (GL_LINES);//PUTIH
    glColor3f (1,1,1);
    elipseMidpoint(600, 300, 100 , 20);
    glEnd();

    glBegin (GL_LINES);//HIJAU
    glColor3f (0,1,0);
    elipseMidpoint(600, 400, 200 , 40);
    glEnd();

    glBegin (GL_POINTS);//HIJAU
    glColor3f (0,1,0);
    elipseMidpoint(400, 400, 20 , 40);
    glEnd();

    glBegin (GL_POINTS);//HIJAU
    glColor3f (0,1,0);
    elipseMidpoint(800, 400, 20 , 40);
    glEnd();

    glBegin (GL_POINTS);//BIRU
    glColor3f (0,0,1);
    elipseMidpoint(600, 200, 200 , 40);
    glEnd();

    glBegin (GL_POINTS);//BIRU
    glColor3f (0,0,1);
    elipseMidpoint(400, 200, 20 , 40);
    glEnd();

    glBegin (GL_POINTS);//BIRU
    glColor3f (0,0,1);
    elipseMidpoint(800, 200, 20 , 40);
    glEnd();
   
    glColor3f (1,1,1);
    glBegin (GL_LINE_LOOP);
    ElipseSederhana(200, 200, 100, 40);
    glEnd();
   
    glPopMatrix();
glutSwapBuffers();


    glFlush();
}

void reshape (int w, int h){
    global_w = w;
    global_h = h;
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(1,1,1);
glutPostRedisplay();
}

void ElipseSederhana(float xc, float yc, float x, float y)
{
    int p1[] = {(int)xc+x, (int)yc+y}; glVertex2iv(p1);
    int p2[] = {(int)xc-x, (int)yc+y}; glVertex2iv(p2);
    int p3[] = {(int)xc+x, (int)yc-y}; glVertex2iv(p3);
    int p4[] = {(int)xc-x, (int)yc-y}; glVertex2iv(p4);  
   
}

void elipseMidpoint(float xc, float yc, float rx, float ry)
{
    float rxSq = rx * rx;
    float rySq = ry * ry;
    float x = 0, y = ry, p;
    float px = 0, py = 2 * rxSq * y;
    ElipseSederhana(xc, yc, x, y);

    p = rySq - (rxSq * ry) + (0.25 * rxSq);
    while (px < py)
    {
        x++;
        px = px + 2 * rySq;
        if (p < 0)
            p = p + rySq + px;
        else
        {
            y--;
            py = py - 2 * rxSq;
            p = p + rySq + px - py;
        }
        ElipseSederhana(xc, yc, x, y);
    }

    p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
    while (y > 0)
    {
        y--;
        py = py - 2 * rxSq;
        if (p > 0)
            p = p + rxSq - py;
        else
        {
            x++;
            px = px + 2 * rySq;
            p = p + rxSq - py + px;
        }
        ElipseSederhana(xc, yc, x, y);
    }
}