-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (166 loc) · 4.18 KB
/
main.cpp
File metadata and controls
180 lines (166 loc) · 4.18 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include "bezier1.h"
#include "primitive.h"
#include <iostream>
#include <algorithm>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "graphics.h"
using namespace std;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
vector<point> curve;
vector<point> control;
int wind_w=1200,wind_h=780;
bool flag_shift;
int i;
void key(unsigned char key,int x,int y){
if(key==32){
cout<<"Pressed"<<endl;
double a[(int)curve.size()+1];
double b[(int)curve.size()+1];
int count=0;
for(vector<point>::iterator it=curve.begin();it!=curve.end();it++)
{
a[count] = it->x;
b[count] = it->y;
printf("%lf %lf\n",a[count],b[count]);
count+=1;
}
func(a,b,count);
}
}
void init() {
//intializing the display window
glutInitWindowPosition(-1,-1);
glutInitWindowSize(wind_w ,wind_h); //The resolution of the output Window
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("Bezier Curve");
glClearColor(1.0,1.0,1.0,1.0f); //the screen is cleared and the screen is made opaque
glColor3f(1.0,1.0,1.0);
glViewport(0, 0, wind_w, wind_h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, wind_w,wind_h,0, 0, 1);
}
void disp()
{
glPointSize(4);
glClearColor(1.0f,1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); //choose red color
//glFrontFace(GL_CW);
if(control.size()!=0)
{
curve.clear();
for(double t=0;t<=1;t+=0.1)
{
point ans=bezier(control, t);
curve.push_back((ans));
}
}
glBegin(GL_LINE_STRIP);
for(vector<point>::iterator it=curve.begin();it!=curve.end();it++)
{
glVertex2f((GLfloat)it->x,(GLfloat) it->y);
//cout<<it->x<<" "<<it->y<<endl;
}
glEnd();
glBegin(GL_POINTS);
for(vector<point>::iterator it=control.begin();it!=control.end();it++)
{
glVertex2f((GLfloat)it->x, (GLfloat)it->y);
}
glEnd();
// glVertex2f(0.2f, -0.5f);
glutSwapBuffers();
//glFlush();
}
bool found(point x)
{
for(vector<point>::iterator it=control.begin();it!=control.end();it++)
if(*it==x)
return true;
return false;
}
void mouse(int button,int state,int x,int y)
{
int mod=glutGetModifiers();
//cout<<mod<<endl;
if(mod==GLUT_ACTIVE_SHIFT)
{
if(flag_shift)
{
i=-1;
flag_shift=false;
}
cout<<"Returning\n";
return;
}
if(button==GLUT_LEFT_BUTTON)
{
if(found(point((double)x, (double)y)))
{
return;
}
control.push_back(point((double)x, (double)y));
cout<<control.size()<<"<---Size after adding one pt\n";
}
else if(button==GLUT_RIGHT_BUTTON)
{
cout<<"Deleted\n";
for(vector<point>::iterator it=control.begin();it!=control.end();it++)
{
if((*it)==point(double(x),double(y)))
{
control.erase(it);
break;
}
}
//cout<<control.size()<<"<---Size after deleting one pt\n";
}
glutPostRedisplay();
}
void drag(int x,int y)
{
int n=control.size();
if(!flag_shift)
{
flag_shift=true;
for(i=0;i<n;i++)
{
if((control[i])==point(double(x),double(y)))
{
break;
}
}
}
if(i!=(n+1) && flag_shift)
{
//cout<<"Changing point\n";
control[i]=point(double(x),double(y));
cout<<control.size()<<"<---Size\n";
glutPostRedisplay();
}
else{
cout<<"No Point Selected\n";
}
}
int main(int argc,char **argv) {
flag_shift=false;
i=-1;
glutInit(&argc, argv);
init();
glutDisplayFunc(disp);
glutMouseFunc(mouse);
glutMotionFunc(drag);
glutKeyboardFunc(key);
glutMainLoop();
//cout<<ans.x<<" "<<ans.y<<endl;
}
#pragma GCC diagnostic pop