Computer graphics code triangle drawing

Computer graphics code triangle drawing (Experiment 3)

The use of software vc ++
code is C language.
This code tutorial is used in the following book (version upgrade does not matter). book
This code is not the idea in the textbook. It is a bit tricky.
Thinking way:
Since 3 points are clicked on the plane and 3 points are connected into 3 lines, the equation of 3 straight lines is known.
Because you know the equation, you can start from a straight line (set to t1), start from t1 and follow t1, and connect to the other line at the same time. Use the existing code to write straight lines.
Explanation of ideas

#include "Line.h"
# include <math.h>
# include <stdio.h>
# include <stdlib.h>
void drawLineBresenham(int x1 ,int y1,int x2,int y2)//画直线

{
    glColor3f(0.0f,1.0f,0.0f);
    glBegin(GL_LINES);	
 
    glVertex2i(x1,y1);
	glVertex2i(x2,y2);

	glEnd();
	glFlush();
	
}

void fillTriangle(int x1,int y1,int x2,int y2,int x3,int y3)
{

   glColor3f(0.0f,0.0f,1.0f);
    glBegin(GL_LINES);	
    int i=0;
    double dk1,dk2,dk3; 
	int tx,ty;
    
	
		if(y1>=y2)//比较三个点的顺序,从下向上
		{
			tx=x2;
			x2=x1;
			x1=tx;
			
            ty=y2;
			y2=y1;
			y1=ty;
		}
		if(y1>=y3)
		{
			tx=x3;
			x3=x1;
			x1=tx;

			ty=y3;
			y3=y1;
			y1=ty;
		}
		if(y2>=y3) 
		{
            tx=x3;
			x3=x2;
			x2=tx;

			ty=y3;
			y3=y2;
			y2=ty;
		}

	dk1=(x2-x1)*1.0/(y2-y1);
	dk2=(x3-x2)*1.0/(y3-y2);
	dk3=(x3-x1)*1.0/(y3-y1);
    printf("x1:%d, y1:%d , dk1:%lf\n",x1,y1,dk1);
    printf("x2:%d, y2:%d , dk2:%lf\n",x2,y2,dk2);
	printf("x3:%d, y3:%d , dk3:%lf\n",x3,y3,dk3);
	double dx1,dx2,dx3,dy1,dy2,dy3;
	double dx4,dy4;
	  
		  int k;
             dx1=x1;dy1=y1;
			 dx2=x1;dy2=y1;
			 dx3=x2;dy3=y2;
			 dx4=x3;dy4=y3;
			for(;dy1<y2;dy1++,dy2++)
			{
                 drawLineBresenham(dx1, dy1, dx2, dy2);
				 dx2+=dk1;
				 dx1+=dk3;
			}
		  
			for(;dy3<=y3;dy1++,dy3++)
			{
                 drawLineBresenham(dx1, dy1, dx3, dy3);
				 dx3+=dk2;
				 dx1+=dk3;
			}
	glEnd();
	glFlush();
}

Resultsachieve

Published 4 original articles · Likes0 · Visits 510

Guess you like

Origin blog.csdn.net/weixin_43902812/article/details/99405461