Created
April 15, 2018 10:22
-
-
Save SohanChy/b809f12f739b6ce6b3398e6e9e8390aa to your computer and use it in GitHub Desktop.
bohut koshter foshol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <GL/gl.h> | |
#include <GL/glut.h> | |
#include <math.h> | |
#include <iostream> | |
long long C(int n, int r) { | |
if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r) | |
long long ans = 1; | |
int i; | |
for(i = 1; i <= r; i++) { | |
ans *= n - r + i; | |
ans /= i; | |
} | |
return ans; | |
} | |
void drawBezier(int numsX[], int numsY[]){ | |
glColor3f (0.0, 1.0, 0.0); | |
float n=4-1; | |
float inc=0.001; | |
for(float t=0.0;t<=1;t+=inc) | |
{ | |
float x=0.0; | |
float y=0.0; | |
float p=0.0; | |
for(int i=0;i<=n;i++){ | |
p=C(n,i)*pow(1-t,n-i)*pow(t,i); | |
x+=p*numsX[i]; | |
y+=p*numsY[i]; | |
} | |
glBegin(GL_POINTS); | |
glVertex2f(x,y); | |
glEnd(); | |
} | |
glColor3f (1.0, 0.0, 0.0); | |
glBegin(GL_POINTS); | |
glVertex2i(numsX[0],numsY[0]); | |
glVertex2i(numsX[1],numsY[1]); | |
glVertex2i(numsX[2],numsY[2]); | |
glVertex2i(numsX[3],numsY[3]); | |
glEnd(); | |
} | |
void showdraw(void) | |
{ | |
glClear (GL_COLOR_BUFFER_BIT); | |
glPointSize(4.0); | |
//int n = 4; | |
int numsX[4] = {100,200,200,100}; | |
int numsY[4] = {100,250,350,400}; | |
drawBezier(numsX,numsY); | |
int nums1X[4] = {400,300,300,400}; | |
int nums1Y[4] = {100,250,350,400}; | |
drawBezier(nums1X,nums1Y); | |
int nums2X[4] = {100,50,50,100}; | |
int nums2Y[4] = {400,440,460,500}; | |
drawBezier(nums2X,nums2Y); | |
int nums3X[4] = {400,450,450,400}; | |
int nums3Y[4] = {400,440,460,500}; | |
drawBezier(nums3X,nums3Y); | |
int nums4X[4] = {400,350,350,200}; | |
int nums4Y[4] = {500,600,600,500}; | |
drawBezier(nums4X,nums4Y); | |
int nums5X[4] = {200,175,150,100}; | |
int nums5Y[4] = {500,600,600,500}; | |
drawBezier(nums5X,nums5Y); | |
glFlush (); | |
} | |
void myInit(void) | |
{ | |
glClearColor(1.0, 1.0, 1.0, 0.0); | |
glColor3f(0.0f, 0.0f, 0.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluOrtho2D(0, 800, 0, 800); | |
} | |
main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize (800, 800); | |
glutInitWindowPosition (0, 0); | |
glutCreateWindow ("show"); | |
glutDisplayFunc(showdraw); | |
myInit(); | |
glutMainLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment