Pages

Saturday, March 12, 2011

CPP - Console Data Records

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void getHelp();
void writeData();
void readData();

typedef struct{
    string name;
    string city;
    string age;
}datas;

int main(int argc, char* argv[]){
    system("color 2f");

    datas data;
    string intro = "Welcome to console prompt: type help for info";
    cout << intro << endl;


    string command = "";
    while(command != "exit"){
        if(command == "write"){
            writeData();
        }else if(command == "help"){
            getHelp();
        }else if(command == "read"){
            readData();
        }else{
            getHelp();
        }

        cout << "Enter the command: ";
        cin >> command;
    }


    return 0;

}

void getHelp(){
    string help = "write - Enter the data\n";
    help = help + "exit - Exit from the console\n";
    help = help + "read - Read from text file\n";
    help = help + "help - Help command\n";
    cout << help << endl;
}

void writeData(){

    // reading a file
    ifstream infile("E:/lola/hello.txt");
    string fileRead = "";
    if(infile.is_open()){
        while(!infile.eof()){
            string temp;
            getline(infile, temp);
            fileRead = fileRead + temp + "\n";
        }
        infile.close();
    }else{
        cout << "Cannot Open File" << endl;
    }

    // write file
    ofstream outfile("E:/lola/hello.txt");
    datas wData;

    cout << "Enter a name: ";
    getline(cin, wData.name);
    getline(cin, wData.name);
    cout << "Enter a city: ";
    getline(cin, wData.city);
    cout << "Enter an age: ";
    getline(cin, wData.age);

    fileRead = fileRead + wData.name + "\t\t" + wData.city + "\t\t" + wData.age;
    outfile << fileRead;
    outfile.close();
}

void readData(){
    ifstream infile("E:/lola/hello.txt");
    if(infile.is_open()){
        while(!infile.eof()){
            string data;
            getline(infile, data);
            cout << data << endl;
        }
    }else{
        cout << "Cannot Open File" << endl;
    }
}

Thursday, March 10, 2011

OpenGL - Centering The Particles

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

void init();
void display();

#define N 10000

float xx[N];
float yy[N];

void setRand(){
    srand((unsigned) time(NULL));
    int i = 0;
    for(i=0; i< N; i++){
        xx[i] = rand() / 100.00;
        yy[i] = rand() / 100.00;
    }
}

void setPoints(){
    glBegin(GL_POINTS);
        glColor3f(1.0,0.0,0.0);
        int i = 0;
        for(i=0;i<N;i++){
            glVertex2f(xx[i], yy[i]);
        }
    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0.0, 400.0, 0.0, 300.0);
}

void display(){
    glClear( GL_COLOR_BUFFER_BIT);
    glPointSize(2.0);

    setPoints();


    glFlush();
}

void mouse(int mouse, int state, int x, int y){
    switch(mouse){
        case GLUT_RIGHT_BUTTON:
            if(state == GLUT_DOWN){
                cout << x << " -> " << y << endl;

                setRand();
                glutPostRedisplay();
            }
        break;

    }
}

void drag(int x, int y){
    y = 300.0 - y;

    int a = 0;
                for(a=0;a<N;a++){
                    if(xx[a] < x){
                        xx[a] += 1;
                    }
                    if(xx[a] > x){
                        xx[a] -= 1;
                    }
                    if(yy[a] < y){
                        yy[a] += 1;
                    }
                    if(yy[a] > y){
                        yy[a] -= 1;
                    }
                }

    glutPostRedisplay();
}

int main(int argc, char* argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("OpenGL Program");

    init();
    setRand();

    glutDisplayFunc(display);
    glutMotionFunc(drag);
    glutMouseFunc(mouse);



    glutMainLoop();

    return 0;

}

Monday, March 7, 2011

OpenGL - Break Away The Particles In 2D

/**
 * This program will do the particle generation. 
 * Then directs the particles move as the mouse moves.
 * The hope of the program is that the particle will move 
 * to the right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 3.0
 * @date     : March 7, 2011
 */


OpenGL - Break Away The Particles In 1D

/**
 * This program will do the particle generation. 
 * Then directs the particles move as the mouse moves.
 * The hope of the program is that the particle will move
 * to the right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 3.0
 * @date     : March 7, 2011
 */
 

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;


#define N 200 // particle size
void grid();
void particle(float p);

float aa[N];
float bb[N];
float bbb = 0.0;

void mouse(int mouse, int state, int x, int y){

    float a = x/1000.00;
    float b = y/1000.00;
    bbb = a;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
    }
}

void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);


    glEnd();
}




void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);

    // creating position of the particles
    // starting position of the particle
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<N;i++){
            aa[i] = rand()*4.0/100000.00;
            bb[i] = rand()*5.0/100000.00;
        }

}

void particle(){

    glPointSize(2.0);
    glColor3f(0.0, 0.0, 0.0);

    float *x = aa;
    float *y = bb;

    glBegin(GL_POINTS);
        int i=0;
        for(i;i<N;i++){
            x[i] ;
            y[i] ;

            // if mouse position is greater than
            // particle position, move the particle right away.
            if(bbb<x[i]){
                x[i] += 0.01;
            }else if(bbb>x[i]){
                x[i] -= 0.01;
            }
            cout << "bbb: " << bbb << endl;

            glVertex2f(x[i], y[i]);
        }

    glEnd();


}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    // particle rendering
    particle();
    grid();

    glFlush();

}


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


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();



    return 0;
}

OpenGL - Particle Moving

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

#define N 2000 // particle size

void grid();
void particle();

float aa[N];
float bb[N];

void mouse(int mouse, int state, int x, int y){
    float a = x/1000.00;
    float b = y/1000.00;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){
                particle();
                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
    }
}

void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);

    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);

    // creating position of the particles
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<N;i++){
            aa[i] = rand()*4.0/100000.00;
            bb[i] = rand()*5.0/100000.00;
        }

}

/**
 * You can also this code inside display function directly
 */
void particle(){

    glPointSize(1.0);
    glColor3f(0.0, 0.0, 0.0);

    float *x = aa;
    float *y = bb;

    glBegin(GL_POINTS);
        int i=0;
        for(i;i<N;i++){
            x[i] ;
            y[i] ;

            x[i] += 0.01;
            y[i] += 0.03;

            glVertex2f(x[i], y[i]);
        }

    glEnd();


}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    particle();
    grid();

    glFlush();
    cout << "lol" << endl;
}


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


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
   
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;
}

OpenGL - Particle Generation

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>
using namespace std;

#define PARTICLE_SIZE 2000


void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);


    glEnd();
}

void particle(){
    glPointSize(1.0);
    glColor3f(0.0, 0.0, 0.0);

    glBegin(GL_POINTS);
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<PARTICLE_SIZE;i++){
            float x = rand()*4.0/100000.00;
            float y = rand()*5.0/100000.00;
            glVertex2f(x, y);
            //cout << x << " " << y << endl;
        }
    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    particle();
    grid();

    glFlush();
}


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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();



    return 0;
}

Wednesday, March 2, 2011

OpenGL - Dragging Object Within It

/**
 * this program moving the geometric object with the mouse dragging
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : alpha
 */


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

using namespace std;
float* draw(float x=100.0, float y=100.0);

void init(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-1.0, 400.0, -1.0, 300.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
}

void motion(int x, int y){

    float* bb;

    bb = draw();

    if(bb[0] < x && x < bb[0]+100 && bb[1] < y && y < bb[1]+100){
        y = 300 - y;
        float xx = (float) x/1.00;
        float yy = (float) y/1.00;
        draw(xx,yy);

        glutPostRedisplay();

        cout << x << " " << y << endl;
    }
}

float* draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x+100.0, y+100.0);
    glFinish();
    float b[] = {x,y};
    return b;
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("iPod OpenGL");
    init();
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}