OpenGL实现中点划线法

时间:2022-03-20 08:19:48

本文实例为大家分享了OpenGL实现中点划线法的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "GL/glut.h"
#include "stdio.h"
#include "math.h"
 
int xs, ys, xe, ye;
 
void MidpointLine(int x0, int y0, int x1, int y1)
{
 if((x0 != x1) && (y0 != y1))
 {
 int a, b, deltal, delta2, d, x, y;
 a = y0 - y1;
 b = x1 - x0;
 d = 2 * a + b;
 deltal = 2 * a;
 delta2 = 2 * (a + b);
 x = x0;
 y = y0;
 glVertex2i(x, y);
 while(x < x1)
 {
 if(d < 0)
 {
 x++;
 y++;
 d += delta2;
 }
 else
 {
 x++;
 d += deltal;
 }
 glVertex2i(x, y);
 }
 }
 else
 {
 int min, d;
 if(x0 == x1)
 {
 int x = x0, y;
 y = (y0<=y1) ? y0 : y1;
 d = fabs((double)(y0 - y1));
 while (d <= 0)
 {
 glVertex2i(x, y);
 y++;
 d--;
 }
 }
 }
}
 
void lineSegment()
{
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(0.0, 0.0, 0.0);
 glBegin(GL_POINTS);
 MidpointLine(xs, ys, xe, ye);
 glEnd();
 glFlush();
}
 
void main(int argc, char * argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 printf("输入线段起始和终止点坐标(范围为0-500,0-500)");
 scanf("%d, %d, %d, %d", &xs, &ys, &xe, &ye);
 glutInitWindowPosition(50, 100);
 glutInitWindowSize(500, 500);
 glutCreateWindow("中点划线法");
 glClearColor(1.0, 1.0, 1.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 gluOrtho2D(0.0, 500.0, 0.0, 500.0);
 glutDisplayFunc(lineSegment);
 glutMainLoop();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/redeagle_gbf/article/details/14041481