标题:用数值微分法生成直线
取消只看楼主
wei0814
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-20
 问题点数:0 回复次数:0 
用数值微分法生成直线
计算机图形学这门课程上讲到生成直线有三种算法:数值微分法、中点Bresenham算法、改进的Bresenham算法。
考试的时候老师让我们用数值微分法生成一条直线。

下面的程序一直没有运行出来,大家帮忙看看。小弟感激不尽!

#include "Conio.h"
#include "graphics.h"
#include "math.h"
#define closegr closegraph

void initgr(void)
{
int gd = DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
}

void my_dline(int x0,int y0,int x1,int y1,int color)
{
    float k1,k2,dx,dy,x,y,i;
    dx=x1-x0;
    dy=y1-y0;
    if(dx==0)
    {
        if(dy>0)
        {
            y=y0;
            i=y1;

        }
        else
        {
            y=y1;
            i=y0;
        }
        for(;y<=i;y++)
        {
            putpixel(x1,y,color);
        }
        return;
    }
    if(dy==0)
    {
        if(dx>0)
        {
            x=x0;
            i=x1;
        }
        else
        {
            x=x1;
            i=x0;
        }
        for(;x<i;x++)
        {
            putpixel(x,y1,color);
        }
        return;
    }
    k1=dy/dx;
    k2=dx/dy;
    if(k1>=-1&&k1<=1)
    {
        if(x0<x1)
        {
            y=y0;
            x=x0;
            i=x1;
        }
        else
        {
            y=y1;
            x=x1;
            i=x0;
        }
        for(;x<=i;x++)
        {
            putpixel(x,(int)(y+0.5),color);
            y+=k1;

        }
        return;
    }
    if(k2>-1&&k2<1)
    {
        if(y0<y1)
        {
            x=x0;
            y=y0;
            i=y1;
        }
        else
        {
            x=x1;
            y=y1;
            i=y0;
        }
        for(;y<=i;y++)
        {
            putpixel((int)(x+0.5),y,color);
            x+=k2;
        }
        return;
    }
}

int main(void)
{
int x,y;
double i;
x=100;
y=100-(int)(50*sqrt(2));
i=0;

initgr();

while(y<=(100+(int)(50*sqrt(2))))
{
    i=y*(200-y);
    x=100+(int)sqrt(i);
    my_dline(100,100,x,y,2);
    delay(10000);
    y++;
}

x=100+(int)(50*sqrt(2));
while(x>=(100-(int)(50*sqrt(2))))
{
    i=x*(200-x);
    y=100+(int)sqrt(i);
    my_dline(100,100,x,y,2);

    delay(10000);
    x--;
}

y=100+(int)(50*sqrt(2));
while(y>=(100-(int)(50*sqrt(2))))
{
    i=y*(200-y);
    x=100-(int)sqrt(i);
    my_dline(100,100,x,y,2);
    delay(10000);
    y--;
}

x=100-(int)(50*sqrt(2));
while(x<=(100+(int)(50*sqrt(2))))
{
    i=x*(200-x);
    y=100-(int)sqrt(i);
    my_dline(100,100,x,y,2);
    delay(10000);
    x++;
}

getch();
closegr();
return 0;
}
搜索更多相关主题的帖子: include 计算机图形学 int 直线 数值微分法 
2008-06-20 12:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-220180-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.233008 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved