是什么原因导致我没有圈内触摸三角形的每一边。是否有任何问题的线条图代码?有时候,如果三角形以外的部分完全在里面,我会得到最多的圆圈。
#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;
}
答案 0 :(得分:0)
我会发布生成内切圆的方法,而不是攻击你的变量方阵。
铭刻圈子:
找到圈子的激怒。这是将三角形角度平分的三条光线相交的点。我发布了关于香火的公式,但它的格式很好here on Wikipedia,很容易理解。自行解决,检查您的配方是否正常工作。
三角形的区域为sr
,其中s=(a+b+c)/2
和r
是圆圈的半径。一个小代数将帮助您找到半径。
另外,需要注意的事项:
S
行标有S= (float)(A+B+c)/2;
,有些内容告诉我您可能希望将c
大写。