# 各种画直线:

## DDA:

void lineDDA (int x0, int y0, int xEnd, int yEnd){
int dx=xEnd-x0,dy=yEnd-y0,steps,k;
float xIncrement,yIncrement,x=x0,y=y0;
if(fabs(dx)> fabs(dy)){
steps = fabs(dx);
}else {
steps = fabs(dy);
}
xIncrement = float(dx)/float(steps);
yIncrement = float(dy)/ float(steps);
setPixel(round(x),round(y));
for(k = 0;k< steps;k++){
x += xIncrement;
y += yIncrement;
setPixel(round(x),round(y));
}
return ;
}


## 中点画线法:

void lineMiddle(int x0, int y0, int x1, int y1){
int a,b,d1,d2,d,x,y;
a=y0-y1,b=x1-x0,d=2*a+b;
d1=2*a,d2=2*(a+b);
x=x0,y=y0;
setPixel (x,y);
while (x<x1){
if (d<0){
x++;
y++;
d+=d2;
}
else{
x++;
d+=d1;
}
setPixel(x,y);
}
return ;
}


## Bresenham画线法:

void lineBres(int x0,int y0,int xEnd,int yEnd){
int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
int p=2*dy-dx;
int twoDy=2*dy,twoDyMinusDx=2*(dy-dx);
int x,y;

if(x0>xEnd){
x=xEnd;
y=yEnd;
xEnd=x0;
}
else{
x=x0;
y=y0;
}
setPixel(x,y);
while(x<xEnd){
x++;
if(p<0)
p+=twoDy;
else{
y++;
p+=twoDyMinusDx;
}
setPixel(x,y);
}
}



## Bresenham 画圆:

class screenPt {
private:
GLint x, y;

public:
screenPt() { x = y = 0; }
void setCoords(GLint xCoordValue, GLint yCoordValue) {
x = xCoordValue;
y = yCoordValue;
}
GLint getx() const { return x; }
GLint gety() const { return y; }
void incremetx() { x++; }
void decrementy() { y--; }
};

void circleMidp(GLint xc, GLint yc, GLint radius) {
screenPt circPt;
GLint p = 1 - radius;
void circlePlotPoints(GLint, GLint, screenPt);
circlePlotPoints(xc, yc, circPt);
while (circPt.getx() < circPt.gety()) {
circPt.incremetx();
if (p < 0)
p += 2 * circPt.getx() + 1;
else {
circPt.decrementy();
p += 2 * (circPt.getx() - circPt.gety()) + 1;
}
circlePlotPoints(xc, yc, circPt);
}
}
void circlePlotPoints(GLint xc, GLint yc, screenPt circPt) {
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
}



## 完整程序:

main.cpp

#define NDEBUG
#ifndef GLUT_DISABLE_ATEXIT_HACK
#define GLUT_DISABLE_ATEXIT_HACK
#endif
#include <windows.h>
#include <gl/glut.h>
#include <cmath>

inline int round (const float a){
return int(a+0.5) ;
}

void setPixel(int xCoord, int yCoord){
glBegin (GL_POINTS);
glVertex2i (xCoord, yCoord);
glEnd();
}

void lineDDA (int x0, int y0, int xEnd, int yEnd){
int dx=xEnd-x0,dy=yEnd-y0,steps,k;
float xIncrement,yIncrement,x=x0,y=y0;
if(fabs(dx)> fabs(dy)){
steps = fabs(dx);
}else {
steps = fabs(dy);
}
xIncrement = float(dx)/float(steps);
yIncrement = float(dy)/ float(steps);
setPixel(round(x),round(y));
for(k = 0;k< steps;k++){
x += xIncrement;
y += yIncrement;
setPixel(round(x),round(y));
}
return ;
}

void lineMiddle(int x0, int y0, int x1, int y1){
int a,b,d1,d2,d,x,y;
a=y0-y1,b=x1-x0,d=2*a+b;
d1=2*a,d2=2*(a+b);
x=x0,y=y0;
setPixel (x,y);
while (x<x1){
if (d<0){
x++;
y++;
d+=d2;
}
else{
x++;
d+=d1;
}
setPixel(x,y);
}
return ;
}

void lineBres(int x0,int y0,int xEnd,int yEnd){
int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
int p=2*dy-dx;
int twoDy=2*dy,twoDyMinusDx=2*(dy-dx);
int x,y;

if(x0>xEnd){
x=xEnd;
y=yEnd;
xEnd=x0;
}
else{
x=x0;
y=y0;
}
setPixel(x,y);
while(x<xEnd){
x++;
if(p<0)
p+=twoDy;
else{
y++;
p+=twoDyMinusDx;
}
setPixel(x,y);
}
}

class screenPt {
private:
GLint x, y;

public:
screenPt() { x = y = 0; }
void setCoords(GLint xCoordValue, GLint yCoordValue) {
x = xCoordValue;
y = yCoordValue;
}
GLint getx() const { return x; }
GLint gety() const { return y; }
void incremetx() { x++; }
void decrementy() { y--; }
};

void circleMidp(GLint xc, GLint yc, GLint radius) {
screenPt circPt;
GLint p = 1 - radius;
void circlePlotPoints(GLint, GLint, screenPt);
circlePlotPoints(xc, yc, circPt);
while (circPt.getx() < circPt.gety()) {
circPt.incremetx();
if (p < 0)
p += 2 * circPt.getx() + 1;
else {
circPt.decrementy();
p += 2 * (circPt.getx() - circPt.gety()) + 1;
}
circlePlotPoints(xc, yc, circPt);
}
}
void circlePlotPoints(GLint xc, GLint yc, screenPt circPt) {
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
}

//绘制程序
void display(){
glClear(GL_COLOR_BUFFER_BIT);//将屏幕设置为黑色
glColor3f(0.0,0.4,0.2);//设置当前颜色状态为白色
lineDDA (0,400,200,300);
lineMiddle (0,300,350, 400);
lineBres (0,0,150,400);
circleMidp(200,200,100);
glFlush();//发送缓冲区
return ;
}

//初始化绘制
void init(){
glClearColor(1.0,1.0,1.0,0.0);//清除颜色设置
glMatrixMode(GL_PROJECTION);//设置投影方式
gluOrtho2D (0.0,400.0,0.0,400.0);
return ;
}

int main(int argc, char** argv){
glutInit(&argc, argv);//初始化glut
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置显示模式为单缓冲，RGB模式
glutInitWindowPosition(0,0);//设置窗口位置
glutInitWindowSize(400,400);//设置窗口大小
glutCreateWindow("GLUT_Project_1");//设置窗口标题
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}



• 点赞 1
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Janus_V

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
09-11 1248
10-25 8318

10-02 840
09-17 972
05-06
03-28
10-18
11-20