Thursday, December 26, 2019

Solar System animation in c++ programming language using openGL api and glut and glew as utility


Solar System animation in C++



code:

//necessary header
#include<iostream>
#include<math.h>
//Opengl utilities
#include <GL\glew.h>
#include<GL/GLU.h>
#include<GL/GL.h>
#include <GL\freeglut.h>
//function prototype
void display();
void init();
void reshape(int, int);
void timer(int);
void DrawCircle(float cx, float cy, float r, int num_segments);
//custom function prototype
int *orbitXY(float);
int main(int argc, char** argv) {
//initializing glut library
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
//creating window
glutInitWindowPosition(0, 0);
glutInitWindowSize(3000, 3000);
//window name
glutCreateWindow("Solar system Simulation");
//function to hold the screen
glutDisplayFunc(display);
//function to reshape the window
glutReshapeFunc(reshape);
//for animation
glutTimerFunc(0, timer, 0);
//to change window background
init();
glutMainLoop();
return(0);
}
int xc_position = 600;
int yc_position = 0;
void display() {
//clear
glClear(GL_COLOR_BUFFER_BIT);
//reset
glLoadIdentity();
//draw things
glPointSize(100.0);
//circle
//for sun
glColor3f(1.0, 0.0, 0.0);
DrawCircle(0.0, 0.0, 150.0, 10000);
//for earth
glColor3f(0.0, 0.0, 1.0);
DrawCircle(xc_position, yc_position, 100.0, 1000);
//display
glutSwapBuffers();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 1);
glColor3f(1.0, 1.0, 1.0);
}
void reshape(int w, int h) {
//view port
glViewport(0, 0, w, h);
//projection
glMatrixMode(GL_PROJECTION);
//reset
glLoadIdentity();
gluOrtho2D(-1000, 1000, -1000, 1000);
glMatrixMode(GL_MODELVIEW);
}
void DrawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for (int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
float x = r * cosf(theta);//calculate the x component
float y = r * sinf(theta);//calculate the y component
glVertex2f(x + cx, y + cy);//output vertex
}
glEnd();
}
float angle = 0;
void timer(int) {
glutPostRedisplay();
glutTimerFunc(1000 / 60, timer, 0);
if (angle > 360) {
angle = 0;
}
angle += 0.001;
int *p = orbitXY(angle);
xc_position = *(p + 0);
yc_position = *(p + 1);
}
int *orbitXY(float angleInDegree) {
static int tempxy[2];
tempxy[0] = (int)(cos(angleInDegree) * 600 - sin(angleInDegree) * 0);
tempxy[1] = (int)(sin(angleInDegree) * 600 + cos(angleInDegree) * 0);
return tempxy;
}

Sorting of array in ascending order in 8085 assembly language


Sorting of array in 8085



Description:

Here all the array input is provided through address shown in simulator.



code:


LXI H 4200h
MOV C M
DCR C
REPEAT: MOV D C
LXI H 4201h
LOOP: MOV A M
INX H
CMP M
JC SKIP
MOV B M
MOV M A
DCX H
MOV M B
INX H
SKIP: DCR D
JNZ LOOP
DCR C
JNZ REPEAT
HLT

Addition of upper and lower nibble of a 8 bit number in 8085 assembly language

Addition of upper and lower nibble of a 8 bit number 


code:




LXI H 8B20h
LXI B 4000h
MVI E 0Ah
LOOP: MOV A M
ANI 0Fh
MOV D A
MOV A M
ANI F0h
RLC
RLC
RLC
RLC
ADD D
DAA
STAX B
INX H
INX B
DCR E
JNZ LOOP
HLT

Addition of two 8 bit number in 8085 microprocessor assembly language


Addition of two 8 bit number


code

MVI C 00h
LDA 4150h
MOV B A
LDA 4151h
ADD B
JNC LOOP
INR C
LOOP: STA 4152h
MOV A C
STA 4153h
HLT
view raw Add.html hosted with ❤ by GitHub

How to make Virtual Guitar in C++ programming language

Virtual Guitar

Audio file used in the project can be download through link below along with complete project:

Download here:





code
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <mmsystem.h>
using namespace std;
class virtualGuitar{
public:
//Tunings function
void tuningA();
void tuningB();
void tuningC();
void tuningD();
void tuningE();
void tuningF();
void tuningG();
void tuningH();
void tuningI();
void tuningJ();
void tuningK();
void tuningL();
void tuningM();
void tuningN();
void tuningP();
void tuningQ();
void tuningR();
void tuningS();
void tuningT();
void tuningU();
void tuningV();
void tuningW();
void tuningX();
void tuningY();
void tuningZ();
void tuning1();
void tuning2();
void tuning3();
void tuning4();
void tuning5();
//chord functions
void chordA();
void chordB();
void chordC();
void chordD();
void chordE();
void chordF();
void chordG();
void chordH();
void chordI();
void chordJ();
void chordK();
void chordL();
void chordM();
void chordN();
void chordP();
void chordQ();
void chordR();
void chordS();
void chordT();
void chordU();
void chordV();
void chordW();
void chordX();
void chordY();
void chordZ();
void chord1();
void chord2();
void chord3();
void chord4();
//user interface functions
void displayUI();
void music();
};
//tuning function declaration
void virtualGuitar::tuningA(){
PlaySound(TEXT("1st_String_E.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningB(){
PlaySound(TEXT("1st_String_E_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningC(){
PlaySound(TEXT("1st_String_E_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningD(){
PlaySound(TEXT("2nd_String_B_.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningE(){
PlaySound(TEXT("2nd_String_B__64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningF(){
PlaySound(TEXT("2nd_String_B__vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningG(){
PlaySound(TEXT("3rd_String_G.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningH(){
PlaySound(TEXT("3rd_String_G_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningI(){
PlaySound(TEXT("3rd_String_G_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningJ(){
PlaySound(TEXT("4th_String_D.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningK(){
PlaySound(TEXT("4th_String_D_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningL(){
PlaySound(TEXT("4th_String_D_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningM(){
PlaySound(TEXT("5th_String_A.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningN(){
PlaySound(TEXT("5th_String_A_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningP(){
PlaySound(TEXT("5th_String_A_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningQ(){
PlaySound(TEXT("6th_String_E.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningR(){
PlaySound(TEXT("6th_String_E_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningS(){
PlaySound(TEXT("6th_String_E_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningT(){
PlaySound(TEXT("C.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningU(){
PlaySound(TEXT("C_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningV(){
PlaySound(TEXT("C_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningW(){
PlaySound(TEXT("D.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningX(){
PlaySound(TEXT("D_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningY(){
PlaySound(TEXT("D_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuningZ(){
PlaySound(TEXT("Dm.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuning1(){
PlaySound(TEXT("Dm_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuning2(){
PlaySound(TEXT("Dm_vbr.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuning3(){
PlaySound(TEXT("E.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuning4(){
PlaySound(TEXT("E_64kb.WAV"),NULL,SND_SYNC);
}
void virtualGuitar::tuning5(){
PlaySound(TEXT("E_vbr.WAV"),NULL,SND_SYNC);
}
//chord function declaration
void virtualGuitar::chordA(){
tuningA();
}
void virtualGuitar::chordB(){
tuningB();
}
void virtualGuitar::chordC(){
tuningC();
}
void virtualGuitar::chordD(){
tuningD();
}
void virtualGuitar::chordE(){
tuningE();
}
void virtualGuitar::chordF(){
tuningF();
}
void virtualGuitar::chordG(){
tuningG();
}
void virtualGuitar::chordH(){
tuningH();
}
void virtualGuitar::chordI(){
tuningI();
}
void virtualGuitar::chordJ(){
tuningJ();
}
void virtualGuitar::chordK(){
tuningK();
}
void virtualGuitar::chordL(){
tuningL();
}
void virtualGuitar::chordM(){
tuningM();
}
void virtualGuitar::chordN(){
tuningN();
}
void virtualGuitar::chordP(){
tuningP();
}
void virtualGuitar::chordQ(){
tuningQ();
}
void virtualGuitar::chordR(){
tuningR();
}
void virtualGuitar::chordS(){
tuningS();
}
void virtualGuitar::chordT(){
tuningT();
}
void virtualGuitar::chordU(){
tuningU();
}
void virtualGuitar::chordV(){
tuningV();
}
void virtualGuitar::chordW(){
tuningW();
}
void virtualGuitar::chordX(){
tuningX();
}
void virtualGuitar::chordY(){
tuningY();
}
void virtualGuitar::chordZ(){
tuningZ();
}
void virtualGuitar::chord1(){
tuning1();
}
void virtualGuitar::chord2(){
tuning2();
}
void virtualGuitar::chord3(){
tuning3();
}
void virtualGuitar::chord4(){
tuning4();
}
//user interface declaration
void virtualGuitar::displayUI(){
//STARTING OF DISPLAY
cout<<"\n\n\tTHESE ARE THE KEY YOU NEED TO USE FROM KEYBOARD TO PRODUCE SOUND OF VARIOUS FREQUENCES:)\n\n\n";
for(int i = 1; i<=24; i++){
for(int j=1; j<=80;j++){
if(i==1||j==1||i==24||j==80){
cout<<"= ";
}else if(i==8){
cout<<"= ";
}else if(i==16){
cout<<"= ";
}else if(j==8){
cout<<"= ";
}else if(j==16){
cout<<"= ";
}else if(j==24){
cout<<"= ";
}else if(j==32){
cout<<"= ";
}else if(j==40){
cout<<"= ";
}else if(j==48){
cout<<"= ";
}else if(j==56){
cout<<"= ";
}else if(j==64){
cout<<"= ";
}else if(j==72){
cout<<"= ";
}else if(i==4&&j==4){
cout<<"Q ";
}else if(i==4&&j==12){
cout<<"W ";
}else if(i==4&&j==20){
cout<<"E ";
}else if(i==4&&j==28){
cout<<"R ";
}else if(i==4&&j==36){
cout<<"T ";
}else if(i==4&&j==44){
cout<<"Y ";
}else if(i==4&&j==52){
cout<<"U ";
}else if(i==4&&j==60){
cout<<"I ";
}else if(i==4&&j==68){
cout<<"O ";
}else if(i==4&&j==76){
cout<<"P ";
}else if(i==12&&j==4){
cout<<"A ";
}else if(i==12&&j==12){
cout<<"S ";
}else if(i==12&&j==20){
cout<<"D ";
}else if(i==12&&j==28){
cout<<"F ";
}else if(i==12&&j==36){
cout<<"G ";
}else if(i==12&&j==44){
cout<<"H ";
}else if(i==12&&j==52){
cout<<"J ";
}else if(i==12&&j==60){
cout<<"K ";
}else if(i==12&&j==68){
cout<<"L ";
}else if(i==12&&j==76){
cout<<"UP";
}else if(i==20&&j==4){
cout<<"Z ";
}else if(i==20&&j==12){
cout<<"X ";
}else if(i==20&&j==20){
cout<<"C ";
}else if(i==20&&j==28){
cout<<"V ";
}else if(i==20&&j==36){
cout<<"B ";
}else if(i==20&&j==44){
cout<<"N ";
}else if(i==20&&j==52){
cout<<"M ";
}else if(i==20&&j==60){
cout<<"LF";
}else if(i==20&&j==68){
cout<<"DN";
}else if(i==20&&j==76){
cout<<"RT";
}else{
cout<<" ";
}
}
cout<<"\n";
}
cout<<"\n\n\tIf you want to exit press ENTER :) ";
//ENDING OF DISPLAY
}
void virtualGuitar::music(){
char select;
while(1){
select = getch();
switch(select){
case 'q':
chordA();
break;
case 'w':
chordB();
break;
case 'e':
chordC();
break;
case 'r':
chordD();
break;
case 't':
chordE();
break;
case 'y':
chordF();
break;
case 'u':
chordG();
break;
case 'i':
chordH();
break;
case 'o':
chordI();
break;
case 'p':
chordJ();
break;
case 'a':
chordK();
break;
case 's':
chordL();
break;
case 'd':
chordM();
break;
case 'f':
chordN();
break;
case 'g':
chordP();
break;
case 'h':
chordQ();
break;
case 'j':
chordR();
break;
case 'k':
chordS();
break;
case 'l':
chordT();
break;
case 'z':
chordU();
break;
case 'x':
chordV();
break;
case 'c':
chordW();
break;
case 'v':
chordX();
break;
case 'b':
chordY();
break;
case 'n':
chordZ();
chord4();
break;
case 'm':
chord1();
chord2();
chord3();
break;
default:
exit(0);
}
}
}
int main(){
virtualGuitar dude;
dude.displayUI();
dude.music();
return 0;
}

How to make Maze in 2d using C programming language using concept of Data structure and Algorithm

MAZE Generator




ALGORITHM


  1.  Start
  2. Declaration of all the necessary variable.
  3. Display initial message to user to perform some task.
  4. Depending upon the user input perform task as below:

a)    if userInput == 1 then draw maze.
                                                                             i.          Take length and width of maze from user.
                                                                           ii.          Allocate memory segment for all maze dynamically.
                                                                         iii.          Set boundary  with “#” character.
                                                                         iv.           Link inner node with each other.
                                                                           v.          Check whether destination is wall or not.if not make it blank.
                                                                         vi.          Draw character of each node to generate maze.
b)    else if userInput == 2 then terminate  the program.
c)     else goto step 3.


  1.  Handle  all necessary exception.
  2.   stop.



      CODE

/*
##########################################################################################################################
# #
# MAZE GENERATOR PROGRAM IN C PROGRAMMING LANGUAGE USING CONCEPT OF DATA STRUCTURE AND ALGORITHM #
# #
##########################################################################################################################
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
typedef struct{
int x, y; //Node position - little waste of memory, but it allows faster generation
void *parent; //Pointer to parent node
char c; //Character to be displayed
char dirs; //Directions that still haven't been explored
}Node;
Node *nodes; //Nodes array or pointer that works similar to the array
int width, height; //Maze dimensions should be in odd integer number
int init(){
int i, j;
Node *n;
nodes = calloc( width * height, sizeof( Node ) ); //Allocate memory for maze
// printf("nodes = %d\n\n",nodes);
if ( nodes == NULL ){ //Exception handling
return 1;
}
// to create boundaries
for ( i = 0; i < width; i++ ){ //Setup crucial nodes
for ( j = 0; j < height; j++ ){
n = nodes + i + j * width;
if ( i * j % 2 ){ //for creating cell of maze
n->x = i;
n->y = j;
n->dirs = 15; //00001111 //Assume that all directions can be explored (4 youngest bits set)
n->c = ' ';
}else{ //Add walls between nodes
n->c = '#';
}
} //ending of inner for
}
//ending of outer for
return 0; // returning zero to say task successfully completed
}
Node *link( Node *n ){
//Connects node to random neighbor (if possible) and returns
//address of next node that should be visited
int x, y;
char dir;
Node *dest;
//Nothing can be done if null pointer is given - return
if ( n == NULL ){ //Exception handling
return NULL;
}
//While there are directions still unexplored
while ( n->dirs ){
/* rand function gives random value among 0,1,2,3.
depending upon the random value shifted 00000001 to left
for rand = 0, dir = 00000001 == 1
for rand = 1, dir = 00000010 == 2
for rand = 2, dir = 00000100 == 4
for rand = 3, dir = 00001000 == 8
00000001
*/
dir = ( 1 << ( rand() % 4 ) ); //Randomly pick one direction
/*
if dir = 00000001
~00001111 == 11110000 bitwise 00000001
ie 11110000 & 00000001 == 000000000
*/
//If it has already been explored - try again(exception handling)
if ( ~ n->dirs & dir ){
continue;
}
/*
if dir = 00000001 then ~dir == 11111110 == 254
a += b
== a = a + b
similarly
n->dirs &= ~dir;
==
n->dirs = n->dirs & ~dir;
14 == 00001110 = 00001111 & 11111110
*/
n->dirs &= ~dir; //Mark direction as explored
//Depending on chosen direction
switch ( dir ){
//Check if it's possible to go right
case 1:
/*
#####
#####
#####
*/
if ( n->x + 2 < width ){
x = n->x + 2;
y = n->y;
}else{
continue;
}
break;
//Check if it's possible to go down
case 2:
if ( n->y + 2 < height){
x = n->x;
y = n->y + 2;
}else{
continue;
}
break;
//Check if it is possible to go left
case 4:
if ( n->x - 2 >= 0 ){
x = n->x - 2;
y = n->y;
}else{
continue;
}
break;
//Check if it's possible to go up
case 8:
if ( n->y - 2 >= 0 ){
x = n->x;
y = n->y - 2;
}else{
continue;
}
break;
}
/*
nodes = 6500728
let x = 1 and y = 1
then x= 3, y =1
so dest = 6500728 + 3 + 1 * 21
= 6500752
*/
dest = nodes + x + y * width; //Get destination node into pointer (makes things a tiny bit faster)
if ( dest->c == ' ' ){ //Make sure that destination node is not a wall
//If destination is a linked node already - abort(terminated)
if ( dest->parent != NULL ){
continue;
}
dest->parent = n; //Otherwise, adopt node
/*
n->x + ( x - n->x ) / 2 + ( n->y + ( y - n->y ) / 2 ) * width
1+(3 - 1)/2 + (1 +(1 - 1)/2)*21 == 23
here address of nodes[0] = 6500728
address of nodes[23] = 6500728 + 23 = 6500751
*/
//Remove wall between nodes
nodes[ n->x + ( x - n->x ) / 2 + ( n->y + ( y - n->y ) / 2 ) * width ].c = ' ';
//Return address of the child node
return dest;
}
}
//If nothing more can be done here - return parent's address
return n->parent;
}
void draw(){
int i, j;
//Outputs maze to terminal - nothing special
for ( i = 0; i < height; i++ ){
for ( j = 0; j < width; j++ ){
printf( "%c", nodes[j + i * width].c );
}
printf( "\n" );
}
}
/* method to move the player for future plan
void player(Node *basant){
char select;
Node *currentBasant;
if(basant == NULL){
printf("Error in player !!!");
}else{
basant->c = '#';
printf(" A for Left\n");
printf(" W for Up\n");
printf(" D for Right\n");
printf(" S for Down\n");
while(1){
select = getch();
switch(select){
case 'a':
if(basant[0-1].c =='#'){
}else{
basant[0].c == ' ';
basant[0-1].c = '#';
currentBasant = & basant[0-1];
}
//left arrow
###
###
###
break;
case 'w':
//up arrow
if(basant[0-width].c == '#'){
}else{
basant[0].c == ' ';
basant[0-width].c = '#';
currentBasant = & basant[0-width];
}
break;
case 'd':
//right arrow
if(basant[0+1].c == '#'){
}else{
basant[0].c == ' ';
basant[0+1].c = '#';
currentBasant = &basant[0+1];
}
break;
case 's':
//down arrow
if(basant[0+width].c == '#'){
}else{
basant[0].c == ' ';
basant[0+width].c = '#';
currentBasant = &basant[0+width];
}
break;
default:
continue;
}
//to draw a maze
system("CLS");
draw();
basant = currentBasant;
}
}
}
*/
int main(){
Node *start, *last, *playerPosition;
int ch;
while(1){
printf("\nWE HAVE FOLLOWING SERVICES.");
printf("\n1.draw Maze");
printf("\n2.Exit");
printf("\nEnter tour choice : ");
scanf("%d",&ch);
switch(ch){
case 1 :
//taking user input
printf("Enter dimension of maze( add number only ):>\n");
printf("Width = ");
scanf("%d",&width);
printf("Height = ");
scanf("%d",&height);
printf("\n\n Generated maze is :\n\n\n\n");
//checking for dimension validity
if ( width < 3 || height < 3 ){
printf("invalid maze size value!\n");
exit( 1 );
}
//Allow only odd dimensions
if ( !( width % 2 ) || !( height % 2 ) ){
printf("dimensions must be odd!\n");
exit( 1 );
}
//Do not allow negative dimensions
if ( width <= 0 || height <= 0 ){
printf("dimensions must be greater than 0!\n");
exit( 1 );
}
//Seed random generator
srand( time( NULL ) );
//checking memory leak
if ( init() ){
printf("out of memory!\n");
exit( 1 );
}
//Setup start node
/*
start = nodes + 1 + width;
= 6500728 +1 + 21
= 6500750
#######################################
#start
*/
start = nodes + 1 + width;
playerPosition = start;
start->parent = start;
last = start;
//Connect nodes until start node is reached and can't be left
while( ( last = link(last) ) != start );
draw();
//code for player
//function call to move the player for future plan
//player(playerPosition);
break;
case 2:
exit(0);
break;
default:
printf("\nEnter correct choice !!!");
}
}
}