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
| #include <stdlib.h> #include <GL/glut.h>
#define EKRAN 720
GLfloat p[4][2] = { {-0.9f,0}, {0.1f,0.9f}, {0.5,0.9f}, {0.9f,0} };
int hassasiyet = 5;
void draw (void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
bezierEgrisi(hassasiyet); bezierNoktalari();
glFlush();
}
GLfloat c ( GLfloat t, int y) {
return p[0][y]*(1.0f-t)*(1.0f-t)*(1.0f-t) + 3*p[1][y]*t*(1.0f-t)*(1.0f-t) + 3*p[2][y]*t*t*(1.0f-t) + p[3][y]*t*t*t; }
void bezierEgrisi ( int hassasiyet ) { int i;
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
for(i = 0 ; i<=hassasiyet ; i++) glVertex3f(c(1.00f*i/hassasiyet,0),c(1.00f*i/hassasiyet,1),0.0f);
glEnd(); }
void bezierNoktalari ( void) { int i;
glColor3f(1.0, 0.0, 0.0); glPointSize(5.0f); glBegin(GL_POINTS); for (i = 0; i < 4; i++) glVertex3f(p[i][0],p[i][1],0.0f); glEnd();
}
float farkkare(float x,float y,float m,float n){ return (m-x)*(m-x) + (n-y)*(n-y); }
float ekran2koorx(float x){ return (((float)x/(float)EKRAN))*2 -1; } float ekran2koory(float x){ return (((float)x/(float)EKRAN))*-2 + 1; }
int secili_nokta=0;
void bezir_noktasi_sec( int x, int y){ float min=2.0f,d; int i; for (i = 0; i < 4; i++){ d = farkkare(ekran2koorx(x),ekran2koory(y),p[i][0],p[i][y]); printf("fark nokta %f\n",d );
if( d < min){ min = d; secili_nokta = i; } } printf("secili nokta %i\n",secili_nokta ); }
void bezir_noktasi_degistir(int x, int y){ p[secili_nokta][0] = ekran2koorx(x); p[secili_nokta][1] = ekran2koory(y); }
void keyboard(unsigned char key, int x, int y){ printf("%c %i | %i %i \n",key,key,x,y ); if(key==43) hassasiyet+=1; if(key==45) hassasiyet-=1; if(key==104) printf("hassasiyet : %i\n",hassasiyet ); } void skeyboard(unsigned char key, int x, int y){ printf("skey %i | %i %i \n",key,key,x,y ); }
void mouse(int buton,int durum,int x,int y){ printf("mouse %i %i %i %i\n",buton,durum,x,y ); if(buton==0 && durum==0) bezir_noktasi_sec(x,y); else if((buton==0 && durum==1) ) bezir_noktasi_degistir(x,y); }
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_ALPHA | GLUT_RGBA);
glutInitWindowSize (EKRAN, EKRAN); glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutIdleFunc(draw); glutKeyboardFunc(keyboard); glutSpecialFunc(skeyboard); glutMouseFunc(mouse);
glutMainLoop();
return 0; }
|