进来讲讲画线的Bresenham算法

shangpin 2003-08-30 03:07:51
请讲的详细些,谢谢
...全文
106 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
meteor135 2003-08-30
  • 打赏
  • 举报
回复
////////////////////////////////////////////
//头文件 test.priv.h
//THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#if HAVE_CONFIG_H
#include <ncurses_cfg.h>
#else
#define HAVE_CURSES_VERSION 0
#define HAVE_RESIZETERM 0
#define HAVE_USE_DEFAULT_COLORS 0
#define HAVE_WRESEZE 0
#endif

#ifndef HAVE_NC_ALLOC_H
#define HAVE_NC_ALLOC_H 0
#endif

#ifndef HAVE_LOCALE_H
#define HAVE_LOCALE_H 0
#endif

#ifndef NCURSES_NOMACROS
#define NCURSES_NOMACROS 0
#endif

#ifndef NEED_PTEM_H
#define NEED_PTEM_H 0
#endif

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#if HAVE_UNISTD_H
#include <UNISTD.H>
#endif


#if NCURSES_NOMACROS
#include <nomacros.h>
#endif

#if HAVE_GETOPT_H
#include <getopt.h>
#else

extern char *optarg;
extern int optind;
#endif

#ifndef GCC_NORETURN
#define GCC_NORETURN
#endif
#ifndef GCC_UNUSED
#define GCC_UNUSED
#endif

#define SIZEOF(table) (sizeof(table)/sizeof(table[0]))

#if defined(NCURSES_VERSION) && HAVE_NC_ALLOC_H
#include <nc_alloc.h>
#else
#define typeMalloc(type,n) (type *) malloc(n*sizeof(type))

#define typeRealloc(type,n,p) (type *) realloc(p, n*sizeof(type))
#endif

#ifndef ExitProgram
#define ExitProgram(code) return code
#endif

#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif

#ifndef NCURSES_CONST
#define NCURSES_CONST
#endif

//////////////////////////////////////////
//////////////////////////////////////////
//SCO OpenServer 5 下的字符时钟的C程序
//File name:tclock.c
#include "test.priv.h"
#include <math.h>
#include <time.h>
#include <stdio.h>
#ifndef PI
#define PI 3.141592654
#endif

#define sign(_x) (_x<0?-1:1)

#define ASPECT 2.2
#define ROUND(value) ((int)((value)+0.5))

#define A2X(angle,radius) ROUND(ASPECT*radius*sin(angle))
#define A2Y(angle,radius) ROUND(radius*cos(angle))

//Plot a point
static void plot(int x, int y, char col)
{
mvaddch(y,x,/*(chtype)*/col);
}
////////////////////////////////////////////////////////////////////////
////////////////注意看这个函数的实现://////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//Draw a diagonal(arbitrary) line using Bresenham's alogrithm.
static void dline(int pair, int from_x, int from_y, int x2, int y2, char ch)
{
int dx, dy;
int ax, ay;
int sx, sy;
int x, y;
int d;

if(has_colors())
attrset(COLOR_PAIR(pair));

dx = x2 - from_x;
dy = y2 - from_y;

ax = abs(dx * 2);
ay = abs(dy * 2);

sx = sign(dx);
sy = sign(dy);
x = from_x;
y = from_y;

if(ax > ay)
{
d = ay - (ax / 2);
while(1)
{
plot(x,y,ch);
if(x == x2)
return;
if(d>=0)
{
y+=sy;
d-=ax;
}
x+=sx;
d+=ay;
}
}
else
{
d = ax - (ay/2);
while(1)
{
plot(x,y,ch);
if(y==y2)
return;
if(d>=0)
{
x+=sx;
d-=ay;
}
y+=sy;
d+=ax;
}
}
}

int main(int argc, char *argv[])
{
int i, cx, cy;
double mradius, hradius, mangle, hangle;
double sangle, sradius, hours;
int hdx, hdy;
int mdx, mdy;
int sdx, sdy;
int ch;
int lastbeep = -1;
time_t tim;
struct tm *t;
char szChar[10];
int my_bg = COLOR_BLACK;

initscr();
noecho();
cbreak();
nodelay(/*stdscr*/stdprn,TRUE);
curs_set(0);

if(has_colors())
{
start_color();
#if HAVE_USE_DEFAULT_COLORS
if(use_default_colors() == OK)
{
my_bg = -1;
}
#endif
init_pair(1, COLOR_RED,my_bg);
init_pair(2, COLOR_MAGENTA,my_bg);
init_pair(3, COLOR_GREEN,my_bg);
}
#ifdef KEY_RESIZE
keypad(stdscr, TRUE);
restart:
#endif
cx = (COLS - 1) / 2; //39
cy = LINES / 2;//12
ch = (cx>cy) ? cy : cx;//usually cy
mradius = (3 * cy) / 4;//9
hradius = cy / 2;//6
sradius = (2 * cy) / 3; //8

for(i = 0; i < 12; i ++)
{
sangle = (i + 1) * (2.0 * PI) / 12.0;
sradius = (5 * cy) / 6;//10
sdx = A2X(sangle,sradius);
sdy = A2Y(sangle,sradius);
sprintf(szChar,"%d", i + 1);
mvaddstr(cy-sdy, cx + sdx, szChar);
}
mvaddstr(0, 25, "SCO OpenServer 5 下的字符时钟");

sradius = 8;
for(;;)
{
napms(1000);
tim = time(0);
t = localtime(&tim);

hours = (t->tm_hour+(t->tm_min/60.0));
if(hours > 12.0)
hours -= 12.0;

mangle = ((t->tm_min) * (2 * PI) / 60.0);
mdx = A2X(mangle,mradius);
mdy = A2Y(mangle,mradius);

hangle = ((hours) * (2.0*PI) / 12.0);
hdx = A2X(hangle,hradius);
hdy = A2Y(hangle,hradius);

sangle = ((t->tm_sec) * (2 * PI) / 60.0);
sdx = A2X(sangle,sradius);
sdy = A2Y(sangle,sradius);

dline(3, cx, cy, cx + mdx, cy - mdy, '#');
attrset(A_REVERSE);
dline(2, cx, cy, cx + hdx, cy - hdy, '.');
attroff(A_REVERSE);

if(has_colors())
attrset(COLOR_PAIR(1));
plot(cx + sdx, cy - sdy, 'O');

if(has_colors())
attrset(COLOR_PAIR(0));

mvaddstr(LINES-2, 0, ctime(&tim));
refresh();

if((t->tm_sec % 5) == 0 && t->tm_sec != lastbeep)
{
lastbeep = t->tm_sec;
beep();
}

if((ch = getch()) != ERR)
{
#ifdef KEY_RESIZE
if(ch == KEY_RESIZE)
{
erase();
goto restart;
}
#endif
break;
}

plot(cx + sdx, cy- sdy,' ');
dline(0,cx,cy,cx+hdx,cy-hdy,' ');
dline(0,cx,cy,cx+mdx,cy-mdy,' ');
}
curs_set(1);
endwin();
return 0;
}
shangpin 2003-08-30
  • 打赏
  • 举报
回复
我看不明白算法过程
Wincent 2003-08-30
  • 打赏
  • 举报
回复
的确,关键在四舍五入。
kashima 2003-08-30
  • 打赏
  • 举报
回复
ft!你过来问的时候回去翻翻计算机图形学的书籍就行了,^_^
无非就是用整数运算画那些比较简单的几何图形,算法实现的一个关键就是
四舍五入的问题,自己看看吧,书上将的很清楚。
因为这是图形学最基本的算法,所以每本图形学书都有的。Good luck!
课程解决的问题: 作为游戏行业或者图形学从业者,你是否面临以下问题: 到底openGL底层如何实现的? 到底矩阵操作变换是怎么做到的? 到底光栅化的算法以及原理是什么? 到底如何才能从3D世界投射到2D屏幕呢? 图形学有这么多的矩阵操作,到底如何推导如何应用呢? 学完这门课程,你应该就可以从底层了解一个初级的openGL图形接口如何实现,图形学最底层的封装到底面临哪些挑战;跟随我们一行一行写完代码,你就会得到一个迷你版本的openGL图形库,你可以深度体会图形从模型变换,观察矩阵变换,投影矩阵变换一直到光栅化纹理操作的全套模拟流程。 课程介绍: 本课程将带领学员不使用任何图形库,实现从0到1的图形学接口封装以及算法讲解,并且带领大家手敲代码,一行一行进行实现。 涵盖了(环境搭建,绘制点,Bresenham算法绘制完美直线,三角形拆分绘制算法,颜色插值算法,图片操作,图片二次插值放缩算法,纹理系统接口搭建及封装,矩阵操作理论以及实践,openGL类似接口封装,3D世界的图形学理论及接口封装等) 最终将带领大家通过C++实现一个3D世界的图形接口,方便所有人入门图形学,进行接下来的openGL接口以及GPU编程的学习   本课程为系列课程的第一步入门,且带领所有人进行实现,更加实用,可以让大家打牢图形学的基础知识及编程技能

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧