-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbezcurve2.c
145 lines (128 loc) · 3.71 KB
/
bezcurve2.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* bezcurve2.c
*
* Modified from bezcurve.c by Sumanta.
* Illustrates that two Bezier curves meet smoothly when they share
* an endpoint and the control points adjacent to the shared endpoint,
* on either curve, lie on a straight segment with the shared endpoint
* in between them on the segment.
*
* Interaction:
* Press space to select a control point of second Bezier curve.
* Press the arrow keys to move the selected control point.
*/
#include <GL/glut.h>
#include <stdlib.h>
static int count = 0;
// Control points for the first curve.
GLfloat ctrlpoints1[4][3] = {
{ -4.0, -4.0, 0.0}, { -3.0, 2.0, 0.0},
{-1.0, 0.0, 0.0}, {0.0, 2.0, 0.0}};
// Control points for the second curve.
GLfloat ctrlpoints2[4][3] = {
{ 0.0, 1.0, 0.0}, { 2.0, 4.0, 0.0},
{2.0, 1.0, 0.0}, {4.0, -1.0, 0.0}};
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
// Draw the first curve in red.
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints1[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
// Draw the second curve in blue.
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints2[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* The following code displays the control points as dots. */
// Control points of the first curve are red, of the second
// curve are blue, and the shared point is yellow.
glPointSize(5.0);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints1[i][0]);
glColor3f(0.0, 0.0, 1.0);
for (i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints2[i][0]);
glColor3f(1.0, 0.0, 0.0);
glVertex3fv(&ctrlpoints2[count][0]);
glEnd();
// Draw a white dashed line for the common tangent.
glEnable(GL_LINE_STIPPLE);
glColor3f(0.25, 0.25, 0.25);
glLineStipple(1, 0x00FF);
glBegin(GL_LINES);
glVertex3f(-3.5, -5.0, 0.0);
glVertex3f(1.5, 5.0, 0.0);
glEnd();
glDisable(GL_LINE_STIPPLE);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case ' ':
if (count < 3) count++;
else count = 0;
glutPostRedisplay();
default:
break;
}
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
ctrlpoints2[count][1] += 0.1f;
if(key == GLUT_KEY_DOWN)
ctrlpoints2[count][1] -= 0.1f;
if(key == GLUT_KEY_LEFT)
ctrlpoints2[count][0] -= 0.1f;
if(key == GLUT_KEY_RIGHT)
ctrlpoints2[count][0] += 0.1f;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
return 0;
}