计算机图形学直线和园

#include <windows.h>
#include<stdlib.h>
#include <GL/glut.h>

/* initialization: */
void myinit(void)
{

    /* attributes */

    glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
    glColor3f(1.0, 0.0, 0.0); /* draw in red */

    /* set up viewing: */
    /* 500 x 500 window with origin lower left */

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
}

void dda_line(int xa,int ya,int xb,int yb)
{
    GLfloat delta_x,delta_y,x,y;
    int dx,dy,steps;
    dx=xb-xa;
    dy=yb-ya;
    if(abs(dx)>abs(dy))
        steps=abs(dx);
    else
        steps=abs(dy);
    delta_x=(GLfloat)dx/(GLfloat)steps;
    delta_y=(GLfloat)dy/(GLfloat)steps;
    x=xa;
    y=ya;
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    glVertex3f(x,y,0);
    int k;
    for(k=1; k<=steps; k++)
    {
        x+=delta_x;
        y+=delta_y;
        glBegin(GL_POINTS);
        glVertex3f(x,y,0);
        glEnd();
    }

}
void C(int x,int y,int center)
{

    glBegin(GL_POINTS);
    glVertex3f(x+center,y+center,0);
    glVertex3f(y+center,x+center,0);
    glVertex3f(-y+center,x+center,0);
    glVertex3f(-x+center,y+center,0);
    glVertex3f(-x+center,-y+center,0);
    glVertex3f(-y+center,-x+center,0);
    glVertex3f(y+center,-x+center,0);
    glVertex3f(x+center,-y+center,0);

    glEnd();


}
void MidC(int r,int center)
{
    GLfloat x,y,d;
    x=0;
    y=r;
    d=1-r;
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    glVertex3f(x,y,0);
    while(x<=y)
    {
        C(x,y,center);
        if(d<0)d+=2*x+3;
        else
        {
            d+=2*(x-y)+5;
            y--;
        }
        x++;
    }



}

/* the display callback: */
void display( void )
{
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */

    /*----------------------------------------*/
    /*  viewport stuff                        */
    /*----------------------------------------*/
    /* set up a viewport in the screen window */
    /* args to glViewport are left, bottom, width, height */
    glViewport(0, 0, 500, 500);
    /* NB: default viewport has same coords as in myinit, */
    /* so this could be omitted: */


     //dda_line(0,0,500,500);
    MidC(100,250);//r x/y

    /* and flush that buffer to the screen */
    glFlush();
}

int main(int argc, char** argv)
{

    /* Standard GLUT initialization */

    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0); /* place window top left on display */
    glutCreateWindow("Digital Differential  Analyser  Line"); /* window title */
    glutDisplayFunc(display); /* display callback invoked when window opened */
    myinit(); /* set attributes */
    glutMainLoop(); /* enter event loop */
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值