Senin, 25 Mei 2015
Rabu, 20 Mei 2015
3D visual studio 2008
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
//#include <glu.h>
#include <iostream>
void init(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glShadeModel (GL_FLAT);
}
float _angle = 45.0f;
//draws the 3D scene
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT|GL_DEFTH_COLOR_BUFFER_BIT)
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//kubus 1
glTranslatef(0.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glutWireCube(0.6);
//kubus 2
glTranslatef(0.0,-0.6,0.0);
glColor3f(0.0,1.0,0.1);
glutWireCube(0.6);
//poci
glTranslatef(0.0,0.6,0.0);
glColor3f(0.0,1.0,0.0);
glutWireTeapot(0.2);
//kubus 3
glTranslatef(0.6,-0.6,0.0);
glColor3f(1.0,1.0,0.0);
glutWireCube(0.6);
//kubus 4
glTranslatef(0.6,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glutWireCube(0.6);
//kubus 5
glTranslatef(0.6,0.0,0.0);
glColor3f(1.0,0.0,1.0);
glutWireCube(0.6);
//kubus 6
glTranslatef(0.0,0.6,0.0);
glColor3f(0.0,0.0,1.0);
glutWireCube(0.6);
//poci
glTranslatef(0.0,0.0,0.0);
glColor3f(0.0,1.0,1.0);
glutWireTeapot(0.2);
//ring
glRotatef(_angle, 0.0f, 1.0f, 1.0f);
glTranslatef(-1.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);
glutWireTorus(0.09,0.4,40,40);
glFlush ();
glutSwapBuffers();//send the 3D scene to the screen
}
void update(int value){
_angle += 9.0f;
if(_angle > 360){
_angle -= 360;
}
glutPostRedisplay();//tell glut that the display changed
//Tell glut to call update
glutTimerFunc(30, update, 0);
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
//glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update,0);
glutMainLoop();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glut.h>
//#include <glu.h>
#include <iostream>
void init(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glShadeModel (GL_FLAT);
}
float _angle = 45.0f;
//draws the 3D scene
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT|GL_DEFTH_COLOR_BUFFER_BIT)
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//kubus 1
glTranslatef(0.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glutWireCube(0.6);
//kubus 2
glTranslatef(0.0,-0.6,0.0);
glColor3f(0.0,1.0,0.1);
glutWireCube(0.6);
//poci
glTranslatef(0.0,0.6,0.0);
glColor3f(0.0,1.0,0.0);
glutWireTeapot(0.2);
//kubus 3
glTranslatef(0.6,-0.6,0.0);
glColor3f(1.0,1.0,0.0);
glutWireCube(0.6);
//kubus 4
glTranslatef(0.6,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glutWireCube(0.6);
//kubus 5
glTranslatef(0.6,0.0,0.0);
glColor3f(1.0,0.0,1.0);
glutWireCube(0.6);
//kubus 6
glTranslatef(0.0,0.6,0.0);
glColor3f(0.0,0.0,1.0);
glutWireCube(0.6);
//poci
glTranslatef(0.0,0.0,0.0);
glColor3f(0.0,1.0,1.0);
glutWireTeapot(0.2);
//ring
glRotatef(_angle, 0.0f, 1.0f, 1.0f);
glTranslatef(-1.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);
glutWireTorus(0.09,0.4,40,40);
glFlush ();
glutSwapBuffers();//send the 3D scene to the screen
}
void update(int value){
_angle += 9.0f;
if(_angle > 360){
_angle -= 360;
}
glutPostRedisplay();//tell glut that the display changed
//Tell glut to call update
glutTimerFunc(30, update, 0);
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
//glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update,0);
glutMainLoop();
return 0;
}
Selasa, 19 Mei 2015
KOMBINASI DAN PERMUTASI
arieuad@gmail.com
#include <cstdlib>
#include <iostream>
using namespace std;
int fak(int nilai)
{
int hasil = nilai;
while (nilai>1)
{
hasil = hasil * (nilai-1);
nilai = nilai -1;
}
return hasil;
}
class Madis{ //deklarasi kelas
public:
void pilih(); //pendeklarasian fungsi pilihan
void permutasi(); //pendeklarasian fungsi permutasi
void kombinasi(); //pendeklarasian fungsi kombinasi
private:
int q[100];
};
void Madis::pilih(){
int n;
cout<<"\n1. Permutasi\n2. kombinasi";
cout<<"\n\npilihan anda : ";
cin>>n;
switch(n)
{
case 1:(n==1);permutasi();
break;
case 2:(n==2);kombinasi();
break;
case 3 :cout<<"\n\n***selesai***\n\n";
break;
}}
void Madis::permutasi(){
// system("cls");
int n,N,k,K,p;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari [1] n";permutasi();}
p=n-k;
N=fak(n);K=fak(p);
cout<<"\nMaka hasil permutasi : "<<N/K;
cout<<"\n\n";pilih();
}
void Madis::kombinasi(){
// system("cls");
int n,N=1,k,K=1,p,P=1;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari [2] n";permutasi();}
p=n-k;N=fak(n);K=fak(k);cout<<"\n(n-r)!-> ";P=fak(p);
cout<<"\nMaka hasil kombinasi : "<<N/(K*P);
cout<<"\n\n";pilih();
}
int main(int argc, char *argv[]) //fungsi main
{
Madis z;
z.pilih();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int fak(int nilai)
{
int hasil = nilai;
while (nilai>1)
{
hasil = hasil * (nilai-1);
nilai = nilai -1;
}
return hasil;
}
class Madis{ //deklarasi kelas
public:
void pilih(); //pendeklarasian fungsi pilihan
void permutasi(); //pendeklarasian fungsi permutasi
void kombinasi(); //pendeklarasian fungsi kombinasi
private:
int q[100];
};
void Madis::pilih(){
int n;
cout<<"\n1. Permutasi\n2. kombinasi";
cout<<"\n\npilihan anda : ";
cin>>n;
switch(n)
{
case 1:(n==1);permutasi();
break;
case 2:(n==2);kombinasi();
break;
case 3 :cout<<"\n\n***selesai***\n\n";
break;
}}
void Madis::permutasi(){
// system("cls");
int n,N,k,K,p;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari [1] n";permutasi();}
p=n-k;
N=fak(n);K=fak(p);
cout<<"\nMaka hasil permutasi : "<<N/K;
cout<<"\n\n";pilih();
}
void Madis::kombinasi(){
// system("cls");
int n,N=1,k,K=1,p,P=1;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari [2] n";permutasi();}
p=n-k;N=fak(n);K=fak(k);cout<<"\n(n-r)!-> ";P=fak(p);
cout<<"\nMaka hasil kombinasi : "<<N/(K*P);
cout<<"\n\n";pilih();
}
int main(int argc, char *argv[]) //fungsi main
{
Madis z;
z.pilih();
system("PAUSE");
return EXIT_SUCCESS;
}
Selasa, 12 Mei 2015
This program is to implement dijkstra’s algorithm using colour codes
#include<iostream>
#include<stdlib.h>
#define MAX 20
#define INFINITY 9999
using namespace std;
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();
}
int main()
{
dijkstra d;
d.function();
}
#include<stdlib.h>
#define MAX 20
#define INFINITY 9999
using namespace std;
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();
}
int main()
{
dijkstra d;
d.function();
}
Langganan:
Postingan (Atom)