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;
circPt.setCoords(0, 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());
}
运行环境: Qt
main.cpp
#define NDEBUG
#ifndef GLUT_DISABLE_ATEXIT_HACK
#define GLUT_DISABLE_ATEXIT_HACK
#endif
#include
#include
#include
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;
circPt.setCoords(0, 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;
}
运行截图: