⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lbcgwork.c

📁 直直线三种算法直线三种算法直线三种算法线三种算法
💻 C
字号:
/* ------------------------------------------------------------ */
/*     Name        : Lbcgwork 1.0");                            */
/*     Information : Computer Graphics Work - lines & circle    */
/*     CopyRight   : (C) Lewsn 2006                             */
/*     Made by     : LiuBing  OUC  2006/12                      */
/*                                                              */
/* ------------------------------------------------------------ */


#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <bios.h>
#include <time.h>
#include <conio.h>

void DrawZb();
void BLine(int, int, int, int);
void DDALine(int, int, int, int);
void MidLine(int, int, int, int);
void Bcircle(int, int);

void main()
{
    int i;
    char str[20];
    long oldtime, newtime,time;
    int gd = VGA,      gm = VGAHI;
    initgraph(&gd, &gm, "D:\\TC\\TCPP\\BGI");
    DrawZb();
    outtextxy(10,10,"Welcome to this program!");
    
    settextstyle(2,0,0);
    outtextxy(10,420,"Name : Lbcgwork 1.0");
    outtextxy(10,430,"Information : Computer Graphics Work - lines & circle");
    outtextxy(10,440,"CopyRight : (C) Lewsn 2006");
    outtextxy(10,450,"Made by : LiuBing  OUC  2006/12 ");
    settextstyle(0,0,0);
    
    setcolor(11);
    outtextxy(10,30,"Press to Bline! ");
    getch();
    oldtime = biostime(0,0L);
    for(i = 0; i < 200; i++){
	BLine(0, 0, 200, 50);
	BLine(0, 0, 50, 200);
	BLine(0, 0, -50, 200);
	BLine(0, 0, -200, 50);
	BLine(0, 0, -200, -50);
	BLine(0, 0, -50, -200);
	BLine(0, 0, 50, -200);
	BLine(0, 0, 200, -50);
    }
    newtime = biostime(0,0L);
    time = (newtime - oldtime) * 1000 /CLK_TCK;
    sprintf(str, "Time: %d ms",time);
    outtextxy(170, 30, str);

    setcolor(10);
    outtextxy(10, 50, "Press to DDAline! ");
    getch();
    oldtime = biostime(0,0L);
    for(i = 0; i < 200; i++){
	DDALine(0, 0, 190, 80);
	DDALine(0, 0, 80, 190);
	DDALine(0, 0, -80, 190);
	DDALine(0, 0, -190, 80);
	DDALine(0, 0, -190, -80);
	DDALine(0, 0, -80, -190);
	DDALine(0, 0, 80, -190);
	DDALine(0, 0, 190, -80);
    }
    newtime = biostime(0,0L);
    time = (newtime - oldtime) * 1000 /CLK_TCK;
    sprintf(str, "Time: %d ms",time);
    outtextxy(170, 50, str);

    setcolor(12);
    outtextxy(10, 70, "Press to Midline! ");
    getch();
    oldtime = biostime(0,0L);
    for(i = 0; i < 200; i++){
	MidLine(0, 0, 180, 100);
	MidLine(0, 0, 100, 180);
	MidLine(0, 0, -100, 180);
	MidLine(0, 0, -180, 100);
	MidLine(0, 0, -180, -100);
	MidLine(0, 0, -100, -180);
	MidLine(0, 0, 100, -180);
	MidLine(0, 0, 180, -100);
    }
    newtime = biostime(0,0L);
    time = (newtime - oldtime) * 1000 /CLK_TCK ;
    sprintf(str, "Time: %d ms",time);
    outtextxy(170, 70, str);

    setcolor(14);
    outtextxy(10, 90, "Press to circle! ");
    getch();
    Bcircle(100, 14);

    setcolor(15);
    outtextxy(10,110,"Press to Exit! ");

    getch();
    closegraph();
}

void DrawZb()
{
    setcolor(15);
    line(320,10,320,470);
    line(20,240,620,240);
    line(320,10,315,15);
    line(320,10,325,15);
    line(615,235,620,240);
    line(615,245,620,240);
    return;
}

void BLine(int x1,  int y1,  int x2, int y2)
{
    int  i, e, exchange, xsign, ysign;
    int  x, y, dx, dy;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    x=x1;  y=y1;
    exchange=0;
    if(dy>dx)  { i=dx;  dx=dy;  dy=i;  exchange=1; }
    e=2*dy-dx;
    xsign=(x2>x1)?  1:-1;
    ysign=(y2>y1)?  1:-1;
    for(i=0; i<=dx; i++){
	putpixel(x+320, 240-y, 11);

	if(e>=0){
	    e=e-2*dx;
	    if(exchange) x+=xsign;
	    else y+=ysign;
	}

	e=e+2*dy;

	if(exchange) y+=ysign;
        else x+=xsign;
    }
}

void DDALine(int x1,int y1,int x2,int y2)
{
    float x,y,dx,dy;
    int i,step;
    x = x1 + 0.5;         y = y1 + 0.5;
    step = abs(x2 - x1) > abs(y2 - y1) ? abs(x2 - x1) : abs(y2 - y1);
    dx = ((float)(x2 - x1)) / step;
    dy = ((float)(y2 - y1)) / step;
    for(i = 0; i<=step; i++){
        putpixel( (int)x+320, 240-(int)y, 10);
        x += dx;    y += dy;
    }
}

void MidLine(int x1, int y1, int x2, int y2)
{
    if(abs(x1 - x2) <= 1 && abs(y1 - y2) <= 1){
	   putpixel ( (int)(320 + (x1 + x2)/2 +0.5), (int)(240 - (y1 + y2)/2 +0.5), 12 );
    }
    else{
        MidLine(x1, y1, (int) ((x1 + x2)/2), (int) ((y1 + y2)/2));
        MidLine((int) ((x1 + x2)/2), (int) ((y1 + y2)/2), x2, y2);
    }
}

void Bcircle(int r, int color)
{
    int x, y, d, d1, d2, direction;
    x=0; y=r;
    d=2*(1-r);
    while(y>=0){
	putpixel(x+320, 240-y, color);
	putpixel(-x+320, 240+y, color);
	putpixel(x+320, 240+y, color);
	putpixel(-x+320, 240-y, color);
	if(d<0){
	    d1=2*(d+y)-1;
	    if (d1<=0)  direction=1;
	    else  direction=2;
	}
	else  if(d>0){
		  d2=2*(d-x)-1;
		  if (d2<=0)  direction=2;
		  else  direction=3;
	      }
	      else  direction=2;

	switch(direction)
	  {
	    case 1 : x++;
		     d+=2*x+1;
		     break;
	    case 2 : x++;
		     y--;
		     d+=2*(x-y+1);
		     break;
	    case 3 : y--;
		     d+=(-2*y+1);
		     break;
	  }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -