Create a Leaf in Computer graphics using c++




/*Faisal Porag
*/

//Source code



#include <GL/glut.h>
#include <math.h>
#include <stdio.h>

#define CTRL_COUNT 100

int ctrlPointsCount;
int ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
int X1[3]={50,40,50}, Y1[3]={35,54,68};
int X2[3]={50,60,50}, Y2[3]={35,48,68};

int X3[3]={50,45,45}, Y3[3]={35,42,32};

void myInit()
{
    glClearColor(0.4,0.2,0.5,0.0);
    glColor3f(0.0,1.0,0.0);
    glPointSize(10.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,128.0,0.0,96.0);
}
//Main formula 
//p(t)=(1-t)^3*p0+3t(1-t)^2*p1+3t^2(1-t)p2+t^3p3

float getNextBezierPointX(float t)
{
float x=0.0;

for(int i=0; i<ctrlPointsCount; i++)
{
int c;
if(i==0 || i==ctrlPointsCount-1)
c = 1;
else
{
c = ctrlPointsCount-1;
}
x +=  c * pow(t, i) * pow(1-t, ctrlPointsCount-1-i) * ctrlPointsX[i];
}


return x;
}

float getNextBezierPointY(float t)
{
float y=0.0;

for(int i=0; i<ctrlPointsCount; i++)
{
int c;
if(i==0 || i==ctrlPointsCount-1)
c = 1;
else
{
c = ctrlPointsCount-1;
}
y +=  c * pow(t, i) * pow(1-t, ctrlPointsCount-1-i) * ctrlPointsY[i];
}



return y;
}
void drawline()
{
// draw control points using red color
/*for(int i=0; i < 3; i++)
{
glBegin(GL_POINTS);
glVertex2i(ctrlPointsX[i], ctrlPointsY[i]);
glEnd();
glFlush();
}*/
// draw bezier curve using control poitns by calculating next points using cubic bezier curve formula
float oldX=ctrlPointsX[0], oldY=ctrlPointsY[0];
for(double t = 0.0;t <= 1.0; t += 0.01) {

float x = getNextBezierPointX(t);
float y = getNextBezierPointY(t);
glColor3f(1.0,t,1.0);
glBegin(GL_LINES);
glVertex2f(oldX, oldY);
glVertex2f(x, y);
glEnd();
glFlush();

oldX = x;
oldY = y;
    }
}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.58,1.0,1.0);
ctrlPointsCount=3;

///left leaf part
for(int i=0;i<3;i++)
{
ctrlPointsX[i] = X1[i];
ctrlPointsY[i] = Y1[i];
}
drawline();

    ///right leaf part
for(int i=0;i<3;i++)
{
ctrlPointsX[i] = X2[i];
ctrlPointsY[i] = Y2[i];
}
drawline();




///the middle line
        glBegin(GL_LINES);
        glColor3f(0.89,1.0,0.5);
glVertex2f(50, 35.5);
glVertex2f(50, 65);
glEnd();
//


        glBegin(GL_LINES);
glVertex2f(50, 39);
glVertex2f(53, 45);
glEnd();
//

   ///
        glBegin(GL_LINES);
glVertex2f(50, 44);
glVertex2f(46.5, 51);
glEnd();


    ///
  glBegin(GL_LINES);
glVertex2f(50, 51);
glVertex2f(52.5, 56);
glEnd();

    ///
  glBegin(GL_LINES);
glVertex2f(50, 57);
glVertex2f(48, 60);
glEnd();

    ///
        glBegin(GL_LINES);
glVertex2f(50, 60.3);
glVertex2f(51, 62.8);
glEnd();

    ///
  glBegin(GL_LINES);
glVertex2f(50, 62.2);
glVertex2f(49, 64);
glEnd();



// //leaves
for(int i=0;i<3;i++)
{
ctrlPointsX[i] = X3[i];
ctrlPointsY[i] = Y3[i];
}
drawline();


//


glFlush();
}


int main(int argc, char *argv[])
{
//takeInput();

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutCreateWindow("Leaf Drawing");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();
    return 0;
}


Output :


//try this ....:)


মন্তব্যসমূহ

একটি মন্তব্য পোস্ট করুন

Popular Posts

Simple 2D hut design in computer graphics using c++

HashMap in Java