Pages

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Monday 14 May 2012

source code procedure djikstra matrik c++ infinity

#include<iostream.h>
#include<stdlib.h>
#define MAX 20
#define INFINITY 9999
class dijkstra
{
private:
int n;
int graph[MAX][MAX];
int colour[MAX];
int start;
int distance[MAX];
int predecessor[MAX];
enum {green,yellow,red};
public:
void read_graph();
void initialize();
int select_min_distance_lable();
void update(int);
void output();
void function();
};
void dijkstra::read_graph()
{
cout<<"Enter the no. of nodes in the graph ::";
cin>>n;
cout<<"Enter the adjacency matrix for the graph ::\n";
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>graph[i][j];
for(i=1;i<=n;i++)
colour[i]=green;
cout<<"Enter the start vertex ::";
cin>>start;
}
void dijkstra::initialize()
{
for(int i=1;i<=n;i++)
{
if(i==start)
distance[i]=0;
else
distance[i]=INFINITY;
}
for(int j=1;j<=n;j++)
{
if(graph[start][j]!=0)
predecessor[j]=start;
else
predecessor[j]=0;
}
}
int dijkstra::select_min_distance_lable()
{
int min=INFINITY;
int p=0;
for(int i=1;i<=n;i++)
{
if(colour[i]==green)
{
if(min>=distance[i])
{
min=distance[i];
p=i;
}
}
}
return p;
}
void dijkstra::update(int p) // p is a yellow colour node
{
cout<<"\nupdated distances are ::\n";
for(int i=1;i<=n;i++)
{
if(colour[i]==green)
{
if(graph[p][i]!=0)
{
if(distance[i]>graph[p][i]+distance[p])
{
distance[i]=graph[p][i]+distance[p];
predecessor[i]=p;
}
}
}
cout<<distance[i]<<'\t';
}
}
void dijkstra::output()
{
cout<<"****** The final paths and the distacnes are ******\n\n";
for(int i=1;i<=n;i++)
{
if(predecessor[i]==0 && i!=start)
{
cout<<"path does not exists between "<<i<<" and the start vertex "<<start<<endl;
exit(1);
}
cout<<"path for node “<<i<<” is ::\n";
int j=i;
int array[MAX];
int l=0;
while(predecessor[j]!=0)
{
array[++l]=predecessor[j];
j=predecessor[j];
}
for(int k=l;k>=1;k=-k)
cout<<array[k]<<"->";
cout<<i<<endl;
cout<<"distance is "<<distance[i]<<endl<<endl<<endl;
}
}
void dijkstra::function()
{
cout<<"\n**********************************************************************\n";
cout<<"This program is to implement dijkstra’s algorithm using colour codes \n";
cout<<"**********************************************************************\n\n";
read_graph();
initialize();
//repeate until all nodes become red
int flag=0;
int i;
cout<<"\n\n******** The working of the algorithm is **********\n\n";
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<"The initial distances are ::\n";
for(i=1;i<=n;i++)
cout<<distance[i]<<'\t';
cout<<endl;
while(flag)
{
int p=select_min_distance_lable();
cout<<"\nThe min distance lable that is coloured yellow is "<<p;
colour[p]=yellow;
update(p);
cout<<"\nnode "<<p<<" is coloured red "<<endl;
colour[p]=red;
flag=0;
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<endl<<endl<<endl;
}
output();
}
void main()
{
dijkstra d;
d.function();
}




*pake borlan C++  ya gan
pake DEV c++ jg bisa tp di edit dkit :D

Friday 4 May 2012

membuat lingkaran

 /* Praktikum 05
* Membuat objek lingkaran sederhana
*/

#include <iostream>
#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdarg.h> 
#include <glut.h> 
#include<math.h>

using namespace std;

typedef unsigned char uchar;

// number of line segments
static int num_lines = 20;

// callback prototypes
void disp(void);
void keyb(uchar k, int x, int y);
void reshape(int x, int y);

// main
int main(int argc, char **argv){
 glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutInitWindowSize(400,400);
  glutInitWindowPosition(100,100);
  glutCreateWindow("circle.cpp");
  glClearColor(0.0,0.0,0.0,0.0);
  glutDisplayFunc(disp);
  glutKeyboardFunc(keyb);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;
}

// disp
void disp(void){
  double angle;
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_LINE_LOOP);
    for(int i =0;i<num_lines;i++){
            angle = i*2*3.14/num_lines;
        glVertex2f(cos(angle),sin(angle));
    }
  glEnd();
  glutSwapBuffers();
}

// keyb
void keyb(uchar k, int x, int y){
  switch (k){
  case 'q':
    exit(0);
    break;
  case '+':
    if(num_lines < 99){
      num_lines++;
      cout << "Circle consists of " << num_lines << " lines " << endl;
      glutPostRedisplay();
    }
    break;
 case '-':
    if(num_lines >3){
      num_lines--;
      cout << "Circle consists of " << num_lines << " lines " << endl;
      glutPostRedisplay();
    }
    break;
  }
}

// reshape
void reshape(int x,int y){
  if(x<y)
    glViewport(0,(y-x)/2,x,x);
  else
    glViewport((x-y)/2,0,y,y);
}

Tuesday 1 May 2012

Membuat objek garis dengan DDA dan Bresenham

* Menggunakan Visual studio 2008

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
#include <math.h>

void display(void)
{
//set display-window background color to white
glClearColor(1.0,1.0,1.0,0.0);
//set projection parameters
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel (GLint xCoordinate, GLint yCoordinate)
{
glBegin (GL_POINTS);
glVertex2i (xCoordinate, yCoordinate);
glEnd();
glFlush();
}
//Procedure Bresenham line-drawing untuk |m| < 1.0
void lineBres (GLint x0, GLint y0, GLint xEnd, GLint yEnd)
{
GLint dx = (float) fabs ((float) xEnd - x0);
GLint dy = (float) fabs ((float) yEnd - y0);
GLint p = 2*dy-dx;
GLint twoDy = 2*dy;
GLint twoDyMinusDx = 2*(dy-dx);
GLint x,y;
//determine which endpoint to use as start position
if(x0 > xEnd){
x=xEnd;
y=yEnd;
xEnd=x;
}
else{
x=x0;
y=y0;
}
setPixel(x,y);
while(x<xEnd){
x++;
if(p<0)
p+=twoDy;
else{
y++;
p+=twoDyMinusDx;
}
setPixel(x,y);
}
}
void drawMyLine (void)
{
    glClear (GL_COLOR_BUFFER_BIT);
  
    glColor3f(1.0,0.0,0.0);
    glPointSize(4.0);
    GLint x0 = 50;
    GLint y0 = 50;
    GLint xEnd = 150;
    GLint yEnd = 50;
    lineBres(x0,y0,xEnd,yEnd);
  
    glColor3f(0.0,1.0,0.0);
    glPointSize(4.0);
    GLint x1 = 50;
    GLint y1 = 100;
    GLint x2 = 150;
    GLint y2 = 100;
    lineBres(x1,y1,x2,y2);

    glColor3f(0.0,0.0,1.0);
    glPointSize(4.0);
    GLint x3 = 50;
    GLint y3 = 150;
    GLint x4 = 150;
    GLint y4 = 150;
    lineBres(x3,y3,x4,y4);

  
}

int main(int argc, char** argv)
{
//initialize GLUT
glutInit(&argc,argv);
//initialize display mode
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//set display-window width & height
glutInitWindowSize(400,400);
//set display-window upper-left position
glutInitWindowPosition(0,0);
//create diplay-window with a title
glutCreateWindow("Digital Differential Analyzer Algorithm");
//initialize OpenGL
display();
//call graphics to be displayed on the window
glutDisplayFunc(drawMyLine);
//display everything and wait
glutMainLoop();
return 0;
}