API?
如果调用Windows
API,要包含windows.h
另誉禅外Windows
API
SetPixel有四个参数,第一个参数是DC的
。后面三个如你这样子。
而且控制台程序也没有DC句柄慧兆,要选
Win32
工程,不过Win32工前虚租程的入口不是main而是WinMain。
#include <graphics.h>锋空#include <math.h>
#define ROUND(a) ((int)(a+0.5))
#define OX 320
#define OY 240
void lineDDA (int xa, int ya, int xb, int yb, int color)
void setpixel (int x, int y, int color)
main(){
int gdrive=DETECT, gmode=0
initgraph(&gdrive, &gmode, "银档瞎d:\\tc")
setbkcolor(BLACK)
line (0, OY, 2*OX, OY)
line (OX, 0, OX, 2*OY)
lineDDA (100, 10, 0, 0, RED)
getch ()
closegraph()
return 0
}
/*
* DDA, digital differential analyzer, algoritm to calculating pixel position.
* Donald Hearn &M. Pauline Baker, Computer Graphics: C Version,
* Second Edition, 清华大学出版社, 2004, p88
*/
void lineDDA (int xa, int ya, int xb, int yb, int color) {
int dx = xb - xa
int dy = yb - ya
int steps, i
float xIncrement, yIncrement
float x=xa
float y=ya
if(abs(dx) >蠢顷 abs(dy))
steps = abs(dx)
else
steps = abs(dy)
/*
* y=kx+b, if k>0, x+1 and y+kif k<0, y+1 and x+1/k.
*/
xIncrement = dx/(float)steps
yIncrement = dy/(float)steps
putpixel (ROUND(x), ROUND(y), color)
for (i=0i<stepsi++) {
x += xIncrement
y += yIncrement
setpixel (ROUND(x), ROUND(y), color)
}
return
}
void setpixel (int x, int y, int color) {
/*printf ("(%d,%d)", x, y)
*/putpixel (OX+x, OY-y, color)
return
}
function DDA(x1,y1,x2,y2,color)length =abs(x2-x1)
if abs(y2-y1)>length
length=abs(y2-y1)
end
dx=(x2-x1)/length
dy=(y2-y1)/length
x=x1+0.5*sign(dx)
y=y1+0.5*sign(dy)
hold on
for i=1:length
plot(round(x),round(y),'Color',color)
x=x+dx
y=y+dy
end
hold off
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)