Pages

Thursday, March 31, 2011

PyQt - Data App

import sys
from PyQt4 import QtGui, QtCore


class DataApp(QtGui.QWidget):

    def __init__(self, parent=None):

        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(200, 100, 400, 300);
        self.setWindowTitle('Data App')
        self.setWindowIcon(QtGui.QIcon('e.png'))


        self.lName = QtGui.QLabel('Name', self)
        self.lCity = QtGui.QLabel('City', self)
        self.leName = QtGui.QLineEdit(self)
        self.leCity = QtGui.QLineEdit(self)
        self.pbAdd = QtGui.QPushButton('Add', self)

        self.lsName = QtGui.QLabel('Name', self)
        self.lsCity = QtGui.QLabel('City', self)
        self.lsNo = QtGui.QLabel('No', self)

        self.col1 = 10
        self.row1 = 10
        self.width = 150
        self.height = 20
        self.hozSpace = 10
        self.verSpace = 10

        self.col2 = self.col1 + self.width + self.hozSpace
        self.col3 = self.col1 + 2*(self.width + self.hozSpace)

        self.row2 = self.row1 + self.height + self.verSpace
        self.row3 = self.row1 + 2*(self.height + self.verSpace)

        self.lName.setGeometry(self.col1, self.row1, self.width, self.height)
        self.lCity.setGeometry(self.col2, self.row1, self.width, self.height)
        self.leName.setGeometry(self.col1, self.row2, self.width, self.height)
        self.leCity.setGeometry(self.col2, self.row2, self.width, self.height)
        self.pbAdd.setGeometry(self.col3, self.row2, self.width-81, self.height)

        self.colS1 = 10
        self.colS2 = self.colS1 + 30
        self.colS3 = self.colS1 + 30 + 150

        self.lsNo.setGeometry(self.colS1, self.row3, self.width, self.height)
        self.lsName.setGeometry(self.colS2, self.row3, self.width, self.height)
        self.lsCity.setGeometry(self.colS3, self.row3, self.width, self.height)

        self.connect(self.pbAdd, QtCore.SIGNAL('clicked()'), self.addData)

        self.n = 0;

        len = 5

        self.no = [QtGui.QLabel('', self) for i in range(len)]
        self.name = [QtGui.QLabel('', self) for i in range(len)]
        self.city = [QtGui.QLabel('', self) for i in range(len)]   



    def addData(self):

        try:
            self.no[self.n].setText(str(self.n + 1))
            self.name[self.n].setText(self.leName.text())
            self.city[self.n].setText(self.leCity.text())

            row = 90 + (20 * self.n)

            self.no[self.n].setGeometry(self.colS1, row, self.width, self.height)
            self.name[self.n].setGeometry(self.colS2, row, self.width, self.height)
            self.city[self.n].setGeometry(self.colS3, row, self.width, self.height)

            self.n = self.n + 1

        except IndexError:

            QtGui.QMessageBox.information(self, 'Maximum Capacity', "The List Is Maximum", QtGui.QMessageBox.Ok)

   

app = QtGui.QApplication(sys.argv)
one = DataApp()
one.show()
sys.exit(app.exec_())
       
               
       
       

Monday, March 28, 2011

Javascript - Like Google

index.php


<html>
<head>
<title>
Javascript Update
</title>

</head>
<body>
<select id="name">
    <option value="aura">Aura</option>
    <option value="luna">Luna</option>
    <option value="lady">Lady</option>
</select>
<input type="button" value="Get" onclick="getData()"/>
<br/>
<input type="button" value="Last Name" onclick="lastName()"/>
<input type="button" value="City" onclick="city()"/>
<input type="button" value="Age" onclick="age()"/>
<script>
function getData(){
    var script = document.createElement('script');
    var name = document.getElementById('name');
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(script);
    script.setAttribute('src','hello.php?fname='+name.value);
    alert('');
}
function lastName(){
    alert(data.lname);
}
function city(){
    alert(data.city);
}
function age(){
    alert(data.age);
}


</script>


</body>
</html>


hello.php

<?php

    $data = array(
        'luna' => array('maya','denpasar',24),
        'lady' => array('gaga','new york',24),
         'aura' => array('kasih','jakarta',25),
    );
   
    $fname = $_GET['fname'];
   
    $output = 'var data = {};';
    $out = array();
   
    foreach($data as $key => $value){
        if(strcmp($fname, $key) == 0){
            foreach($value as $v){
                $out[] = $v;
               
            }
        }
    }
    $output .= 'data.lname = "'.$out[0].'";';
                $output .= 'data.city = "'.$out[1].'";';
                $output .= 'data.age = "'.$out[2].'";';
   
    header('Content-type: text/javascript');
    echo $output;
?>
   

OpenGL - Gridding

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

using namespace std;

#define WIDTH 400
#define HEIGHT 300
#define N 10
float dens[N][N] = {};
void init(){
    glClearColor(1.0, 1.0, 1.0,1.0);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glOrtho(0, WIDTH, HEIGHT, 0, 0, 1);
    //glMatrixMode(GL_MODELVIEW);
    //gluOrtho2D(0.0,1.0,0.0,1.0);
    //glDisable(GL_DEPTH_TEST);

}



void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(3.0);
    glBegin(GL_QUADS);
    float h = 1.00/N;
    for(int i=0; i<N; i++){
        float x = 1.0*i/N;
        for(int j=0; j<N; j++){
            float y = 1.0*j/N;
            glColor3f(1.0,0.0,0.0);
            glVertex2f(x, y);
            glColor3f(0.0,1.0,0.0);
            glVertex2f(x+h, y);
            glColor3f(0.0,0.0,1.0);
            glVertex2f(x+h, y+h);
            glColor3f(0.0,1.0,1.0);
            glVertex2f(x, y+h);
        }
    }
    glEnd();
    glFlush();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("Lucia OpenGL");

    glClearColor(1.0, 1.0, 1.0, 0.0);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    gluOrtho2D(0.0,1.0,0.0,1.0);
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

Friday, March 25, 2011

HTML5 - Lucia


var ctx = document.getElementById('canvas').getContext('2d');

grad = ctx.createLinearGradient(0,30,280,30);
grad.addColorStop(0.1, '#af8');
grad.addColorStop(0.5, '#fc0');
grad.addColorStop(1.0, '#faf');
ctx.fillStyle = grad;

ctx.fillRect(0,0,280,60)

ctx.fillStyle = '#a5f';
ctx.font = '30px arial';
ctx.fillText('Lucia Corporation',20, 40);

The above code will finish in like below:


Tuesday, March 22, 2011

OpenGL - Dragging Single Object


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

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

#define WIDTH 400.00
#define HEIGHT 300.00

typedef struct point{
    float x;
    float y;
}point;

point titik[4];


void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0,1,0,1);

    titik[0].x = 0.1;
    titik[0].y = 0.1;

    titik[1].x = 0.2;
    titik[1].y = 0.1;

    titik[2].x = 0.2;
    titik[2].y = 0.2;

    titik[3].x = 0.1;
    titik[3].y = 0.2;
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);

    glBegin(GL_QUADS);
        glVertex2f(titik[0].x, titik[0].y);
        glVertex2f(titik[1].x, titik[1].y);
        glVertex2f(titik[2].x, titik[2].y);
        glVertex2f(titik[3].x, titik[3].y);
    glEnd();

    glFlush();
}

void keyboard(unsigned char key, int x, int ){
    switch(key){
        case 27:
            exit(1);
            break;
    }
}

void mouse(int mouse, int state, int x, int y){
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

            }
            break;
    }
}


void motion(int x, int y){

    float xx = x / WIDTH;
    titik[0].x = xx;
    titik[1].x = xx + 0.1;
    titik[2].x = xx + 0.1;
    titik[3].x = xx;

    float yy = y / HEIGHT;

    yy = 1 - yy;

    titik[0].y = yy;
    titik[1].y = yy;
    titik[2].y = yy + 0.1;
    titik[3].y = yy + 0.1;

    cout << xx << endl;
    glutPostRedisplay();
}

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

    system("color 2f");

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

    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;
}

Sunday, March 20, 2011

OpenGL - Dragging Points

/**
 * 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  : 1.0
 * @date     : March 20, 2011
 */


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

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;
typedef struct{
    float x;
    float y;
}point;

#define nn 1000
point titik[nn];

// record the previous state
int prev_x = 0, prev_y=0;

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0,400,0,300);

    srand((unsigned)time(NULL));
    for(int i=0;i<nn;i++){
        titik[i].x = rand()/100;
        titik[i].y = rand()/100;
    }
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,1.0);

    glPointSize(2.0);
    glBegin(GL_POINTS);
        for(int i=0; i<nn; i++){
            glVertex2f(titik[i].x, titik[i].y);
        }
    glEnd();

    glFlush();
}

void keyboard(unsigned char key, int x, int ){
    switch(key){
        case 27:
            exit(1);
            break;
    }
}


void motion(int x, int y){

    // horizontal dragging
    if(x > prev_x){
        for(int i=0; i<nn; i++){
            titik[i].x++;
        }
    }else if(x < prev_x){
         for(int i=0; i<nn; i++){
            titik[i].x--;
         }
    }

    // vertical dragging: reverse
    if(y < prev_y){
        for(int i=0; i<nn; i++){
            titik[i].y++;
        }
    }else if(y > prev_y){
        for(int i=0; i<nn; i++){
            titik[i].y--;
        }
    }

    prev_x = x;
    prev_y = y;

    glutPostRedisplay();
}

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

    system("color 2f");

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

    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

Saturday, March 19, 2011

CI - Date Parser

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This class will provide parsing service for date time format:
 * month/date/year and hour:minute:second
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : February 20, 2011
 */

class Parser {

    function parsee($data)
    {
        $this->datee = $data['date'];
        $this->timee = $data['time'];
       
        $this->date = array();
        foreach(explode('/',$this->datee) as $value){
            $this->date[] = $value;
        }
       
        $this->time = array();
        foreach(explode(':', $this->timee) as $value){
            $this->time[] = $value;
        }
       
    }
   
    function get_date(){
        return $this->date[1];
    }
   
    function get_month(){
        return $this->date[0];
    }
   
    function get_year(){
        return $this->date[2];
    }
   
    function get_hour(){
        return $this->time[0];
    }
   
    function get_minute(){
        return $this->time[1];
    }
   
    function get_second(){
        return $this->time[2];
    }
}

?>

Friday, March 18, 2011

PHP - Using SQLite3

/**
 * index.php
 */

<html>
<head>
<script src="jquery.js"></script>
<script>
function save(){
    var ajax = new XMLHttpRequest();
    var name = document.getElementById('name');
    var city = document.getElementById('city');
    var age = document.getElementById('age');
    var mode = 'write';
    

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;
}

OpenGL - Dragging Object

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

#include <iostream>

using namespace std;
void draw(float, float);

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){
    y = 300 - y;
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

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

void draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x-100.0, y-50.0);  // x1, y1, x2, y2
    glFinish();
}

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;
}

OpneGL - Mouse Dragging

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

#include <iostream>

using namespace std;
void draw(float, float);

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){
    /**
     * The value of y must be converted, because the difference 
     * of coordinate space system.
     */     
    y = 300 - y;          
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

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

void draw(float x, float y){
    glColor3f(3.0, 0.9, 0.7);
    glRectf(10.0, 10.0, x,y);
    glFinish();
}

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;
}

OpenGL - Between Green And Blue

/**
 * This function must be called by glutMotionFunc(motion)
 */

void motion(int x, int y){
    float xx  = (x/400.00);
    float yy = (y/300.00);
    glClearColor(0.0, xx, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Blue

void motion(int x, int y){
    float xx  = (x/400.00);        // must be float divider
    float yy = (y/300.00);         // must be float divider
    glClearColor(xx, 0.0, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Green

void motion(int x, int y){
    float xx  = (x/400.00);      // must be float divider
    float yy = (y/300.00);       // must be float divider
    glClearColor(xx, yy, 0.0, 1.0);
    glutPostRedisplay();
}
/**
 * This snippet of code will change your background color from
 * pure of black to pure of white when your drag has been reached
 * 1000 event. This event will invoked when you drag your mouse button
 */

void motion(int x, int y){
    static float i = 0;
    glClearColor(i/1000, i/1000, i/1000, 1.0);
    glutPostRedisplay();
    i++;
}

Tuesday, March 1, 2011

OpenGL - Backgrouning Using Keyboard

/**
 * This function will backgrouning the background based on
 * the key you type.
 * for example, if you type y, you will get the yellow background
 */

void keyboard(unsigned char key, int x, int y){

    switch(key){
        case 'r':
            glClearColor(1.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();              // refresh window
            break;
        case 'b':
            glClearColor(0.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'g':
            glClearColor(0.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'y':
            glClearColor(1.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'm':
            glClearColor(1.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'a':
            glClearColor(0.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'u':
            glClearColor(0.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'w':
            glClearColor(1.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'h':
            cout << "Enter a key on keyboard to backgrouning!" << endl;
            cout << "a -> aqua" << endl;
            cout << "r -> red" << endl;
            cout << "b -> blue" << endl;
            cout << "g -> green" << endl;
            cout << "y -> yellow" << endl;
            cout << "m -> magenta" << endl;
            cout << "b -> black" << endl;
            cout << "w -> white" << endl;
            break;
    }
}