计算机图形学画图代码总结

来源:互联网 发布:手机查看淘宝买家等级 编辑:程序博客网 时间:2024/05/16 19:34
///DDA画直线#include <cstdio>using namespace std;void Line(int x1, int y1, int x2, int y2, int color){    CDC *pDC = GetDC();///获取图形设备环境    float t, x, y, step;    step = 1.0/(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));    for(t=0.0; t<=1.0; t=t+step)    {        x = x1 + t*(x2-x1);        y = y1 + t*(y2-y1);        pDC->SetPixel(x,y, color);    }}void DDA(int x1,int y1,int x2,int y2,int color){    CDC *pDC=GetDC();    float t,x,y,step;    step=1.0/((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));    for(t=0.0; t<=1.0; t+=step)    {        x=x1+t*(x2-x1);        y=y1+t*(y2-y1);        PDC->SetPiexl(x,y,color);    }}void Bresenham(int x1, int y1, int x2, int y2){    CDC *pDC = GetDC();///获取图形设备环境    float dy = y2-y1;    float dx = x2-x1;    float m = dy/dx;    int x = x1;    int y = y1;    float e = m-0.5;    for(x=x1; x<=x2; x++)    {        pDC->SetPixel(x, y, RGB(0, 0, 255));        if(e>=0)        {            y=y+1;            e=e-1;        }        e=e+m;    }}///正负法void MidpointLine(int x0, int y0, int x1, int y1){    CDC *pDC = GetDC();    int a, b, delta1, delta2, d,x, y;    a=y0-y1;    b=x1-x0;    d=2*a+b;    delta1=2*(a+b);    delta2=2*a;    x=x0;    y=y0;    SetPixel(x, y, RGB(,,));    while(x<x1)    {        if(d<0)        {            x++;            y++;            d += delta1;        }        else        {            x++;            d += delta2;        }        SetPixel(x, y, RGB(,,));    }}///bresenham生成圆弧void bresenham_arc(int R, int color){    CDC *pDC = GetDC();//获取设备环境    int x,y,d;    x=0;    y=R;    d=3-2*R;    while(x<=y)    {        pDC->SetPixel(x, y, color);        if (d<0) d=d+4*x+6;        else        {            d=d+4*(x-y)+10;            y=y-1;        }        x=x+1;    }}///中点画圆void MidPoint_arc(int R, int color){    CDC *pDC = GetDC();//获取设备环境    int x, y;    float d;    x=0;    y=R;    d=1.25-R;    while(x<=y)    {        pDC->SetPixel(x, y, color);        if (d<0) d=d+2*x+3;        else        {            d=d+2*(x-y)+5;            y=y-1;        }        x=x+1;    }}///椭圆生成法void Ellipse(int a, int b, int color){    CDC *pDC = GetDC();//获取设备环境    int x, y;    float d;    x = 0;    y = b;    d = b*b +a*a*(-b+0.25);    pDC->SetPixel(x, y, color);    while( b*b*(x+1) < a*a*(y-0.5) )    {        if (d<0)        {            d = d+b*b*(2*x+3);            x=x+1;        }        else        {            d=d+(b*b*(2*x+3)+a*a*(-2*y+2));            x=x+1;            y=y-1;        }        pDC->SetPixel(x, y, color); //(x, -y); (-x, y); (-x, -y);    }//第一象限上部分    d = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;    while( y > 0 )    {        if (d<0)        {            d=d+b*b*(2*x+2)+a*a*(-2*y+3);            x=x+1;            y=y-1;        }        else        {            d=d+a*a*(-2*y+3);            y=y-1;        }        pDC->SetPixel(x, y, color);    }//第一象限下部分}
0 0