是什么原因我没有在三角形的每一边都被圈起来。画线代码有什么问题吗?有时,如果部分在三角形之外,有时完全在里面,我会得到最多的圆圈。
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
int w,h,a,b,c,d,e,f,g,h,i,j,k,l;
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
}
int round1(double number)
{
return (number>=0) ? (int)(number+0.5):(int)(number-0.5);
}
void setPixel(int x,int y)
{
glColor3f(0,1,1);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void LineWithDDA(int x0,int y0,int x1,int y1)
{
int dy=y1-y0;
int dx=x1-x0;
int steps,i;
float xinc,yinc,x=x0,y=y0;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=(float)dx/(float)steps;
yinc=(float)dy/(float)steps;
setPixel(round1(x),round1(y));
for(i=0;i<steps;i++)
{
x+=xinc;
y+=yinc;
setPixel(round1(x),round1(y));
}
glutSwapBuffers();
}
circledim(int x1,int y1,int x2,int y2,int x3,int y3)
{
float A,B,C,S,P,incX,incY,At;
int R;
A= (float) sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
B= (float) sqrt((x3-x2)*(x3-x2)+(y2-y1)*(y2-y1));
C= (float) sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
S= (float)(A+B+c)/2;
At= (float) sqrt(S*(S-A)*(S-B)*(S-C));
R= (int) At/ (int)S;
P= A+B+C;
incX=(int) (B*x1+C*x2+A*x3)/(int)P;
incY=(int) (B*y1+C*y2+A*y3)/(int)P;
int xCenter=incX,yCenter=incY,r=R;
int x=0,y=r;
int d=3/2-r;
glColor3f(0,1,1);
while(x<=y)
{
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-x,yCenter-y);
setPixel(xCenter-y,yCenter-x);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-y,yCenter+x);
if(d<0)
d+=(2*x)+3;
else
{
d+=(2*(x-y))+5;
y-=1;
}
x++;
}
//glFlush();
glutSwapBuffers();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
LineWithDDA(a,b,c,d);
LineWithDDA(c,d,e,f);
LineWithDDA(e,f,a,b);
circledim(a,b,c,d,e,f);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(0,0);
printf("Enter the details:\n");
printf("Windows width w : ");
scanf("%d",&w);
printf("Windows heigth h : ");
scanf("%d",&h);
glutInitWindowSize(w,h);
printf("\nInitial Vertex : x1: ");
scanf("%d",&a);
printf(" y1: ");
scanf("%d",&b);
printf("\nsecond Vertex : x2: ");
scanf("%d",&c);
printf(" y2: ");
scanf("%d",&d);
printf("\nthird Vertex : x3: ");
scanf("%d",&e);
printf(" y3: ");
scanf("%d",&f);
glutDisplayFunc(Display);
glutCreateWindow("dda Line");
glutDisplayFunc(Display);
Init();
glutMainLoop();
return 0;
}