JS Lab 01- beginning

<!doctype html>
<html>
<head>
<title> Welcome to JS! </title>
<script type=”text/javascript”>
//JS Variables
const max = 10;
let  i = 10;
var x = 5;
/* loop and conditions */
for(x = 1; x <= 10; x++)
{
if(x%2 ==0)
document.write(‘The value of x is =’+x+'<br>’);
}
var  x = ‘Text’;
//JS Object
const person = {
  firstName: “John”,
  lastName: “Doe”,
  age: 50,
  eyeColor: “blue”
};
document.write(“<br> Age =”+ person.age );
//JS Array
const cars = [“Saab”, “Volvo”, “BMW”];
const cars2 = new Array(“Saab”, “Volvo”, “BMW”);
document.write(“<br> First element:”+cars[0]);
document.write(“<br> No. of Item:”+cars.length);
//Array Sorting
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.write(“<br> Ascending Sort: “+fruits.sort());
document.write(“<br> Descending: “+fruits.reverse());
// foreach iteration
var numbers = [45, 4, 9, 16, 25];
numbers[’20’] = ‘Test’;
let txt = “”;
numbers.forEach(myFunction);
function myFunction(value, index, array) {
  txt += index+’=>’+value + “, <br>”;
}
document.write(“<br>Foreach: “+txt);
//String operations
let text = “Please locate where ‘locate’ occurs!”;
let index = text.indexOf(“locate”);
document.write(“<br>Index: “+index);
//Number methods
var x = ‘2023’;
x = parseInt(x) + 5;
document.write(“<br>Sum: “+x);
//JS Functions
//alert(‘Welcome to JS!’);
function change_btn(){
//document.write(‘<textarea rows=”5″> </textarea>’);
document.getElementById(“demo”).innerHTML =  ‘<textarea rows=”5″> </textarea>’;
}
function change_color(){
document.getElementById(‘h1’).style.color = ‘green’;
}
</script>
</head>
<body>
<header>
<h1 onmouseover=”change_color();” id=”h1″> DCC CSE Lab </h1>
<nav>
<a href=””>Home</a>
<a href=””>About</a>
</nav>
</header>
<article>
<h2 class=”head”>Adding JS practice</h2>
<button onclick=”change_btn();”>Click here</button>
<div id=”demo”></div>
<p> Some text about applying JS to html webpage. </p>
</article>
<footer>
<h2 id=”comp”> MyWeb</h2>
<p> Footer links </p>
</footer>
</body>
</html>

Tree Search

/*
BST: Binary SeaRCh Tree, LC: Left Child, RC: Right Child, TN: Temp Node
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
   int data;
   struct node *LC;
   struct node *RC;
};
struct node *root = NULL;
void insert(int data) {
   struct node *TN = (struct node*) malloc(sizeof(struct node));
   struct node *current;
   struct node *parent;
   TN->data = data;
   TN->LC = NULL;
   TN->RC = NULL;
   if(root == NULL) { //if tree is empty
      root = TN;
   } else {
      current = root;
      parent = NULL;
      while(1) {
         parent = current;
         if(data < parent->data) {//go to left of the tree
            current = current->LC;
            if(current == NULL) {//insert to the left
               parent->LC = TN;
               return;
            }
         }  //go to right of the tree
         else {
            current = current->RC;
            if(current == NULL) {//insert to the right
               parent->RC = TN;
               return;
            }
         }
      }
   }
}
struct node* search(int data) {
   struct node *current = root;
   printf(“Visiting elements: “);
   while(current->data != data) {
      if(current != NULL)
         printf(“%d “,current->data);
      //go to left tree
      if(current->data > data) {
         current = current->LC;
      }
      //else go to right tree
      else {
         current = current->RC;
      }
      //not found
      if(current == NULL) {
         return NULL;
      }
   }
   return current;
}
int main() {
   int x;
   printf(“Enter tree nodes, -1 for exit.);
   do{
scanf(“%d”,&x);
insert(x);
   }while(x != -1);
   printf(“Enter item to search: “);
   scanf(“%d”,&x);
   struct node * temp = search(x);
   if(temp != NULL) {
      printf(“[%d] Element found.”, temp->data);
      printf(“\n”);
   }else {
      printf(“[ x ] Element not found (%d).\n”, x);
   }
   return 0;
}

Tree Traversal, In-order, Pre-order, Post-order

/*
BST: Binary SeaRCh Tree, LC: Left Child, RC: Right Child, TN: Temp Node, POT: Pre-Order Traversal,IOT: In-Order Traversal,PST: Post-Order Traversal,
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
   int data;
   struct node *LC;
   struct node *RC;
};
struct node *root = NULL;
void insert(int data) {
   struct node *TN = (struct node*) malloc(sizeof(struct node));
   struct node *current;
   struct node *parent;
   TN->data = data;
   TN->LC = NULL;
   TN->RC = NULL;
   if(root == NULL) { //if tree is empty
      root = TN;
   } else {
      current = root;
      parent = NULL;
      while(1) {
         parent = current;
         if(data < parent->data) {//go to left of the tree
            current = current->LC;
            if(current == NULL) {//insert to the left
               parent->LC = TN;
               return;
            }
         }  //go to right of the tree
         else {
            current = current->RC;
            if(current == NULL) {//insert to the right
               parent->RC = TN;
               return;
            }
         }
      }
   }
}
void POT(struct node* root) {
   if(root != NULL) {
      printf(“%d “,root->data);
      POT(root->LC);
      POT(root->RC);
   }
}
void IOT(struct node* root) {
   if(root != NULL) {
      IOT(root->LC);
      printf(“%d “,root->data);
      IOT(root->RC);
   }
}
void PST(struct node* root) {
   if(root != NULL) {
      PST(root->LC);
      PST(root->RC);
      printf(“%d “, root->data);
   }
}
int main() {
   int x;
   printf(“Enter tree nodes, -1 for exit.”);
   scanf(“%d”,&x);
   while(x != -1){
insert(x);
scanf(“%d”,&x);
   }
   printf(“\nPreorder traversal: “);
   POT(root);
   printf(“\nInorder traversal: “);
   IOT(root);
   printf(“\nPost order traversal: “);
   PST(root);
   return 0;
}

OOP Lab: File operation

/* Write to a file */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of<<name<<“\n”;
of.close();

return 0;
}

 

/* Read file */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
char name[30];
cout<<“File data:” <<endl;

ifs>>name;
while(ifs.eof() == 0){
cout << name <<” “;
ifs>>name;
}

ifs.close();

return 0;
}

 

/* Read as line */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
int SIZE = 80;
char name[SIZE];
cout<<“File data:” <<endl;

do{
ifs.getline(name,SIZE);
cout<< name <<endl;
}while(ifs.eof() == 0);

ifs.close();

return 0;
}

 

/* cursor movement */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of.seekp(-5,ios::cur);
of<<name<<“\n”;
of.close();

return 0;
}

Posted in CSE

Draw translated and rotated hierarchical models

/*
* robot.c
* This program shows how to composite modeling transformations
* to draw translated and rotated hierarchical models.
* Interaction: pressing the s and e keys (shoulder and elbow)
* alters the rotation of the robot arm.
*/

#include <GL/glut.h>
#include <stdlib.h>

static int shoulder = 0, elbow = 0;

void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘s’:
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case ‘S’:
shoulder = (shoulder – 5) % 360;
glutPostRedisplay();
break;
case ‘e’:
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case ‘E’:
elbow = (elbow – 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Demonstrates modeling transformations

/*
* model.c
* This program demonstrates modeling transformations
*/
#include <GL/glut.h>
#include <stdlib.h>

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void draw_triangle(void)
{
glBegin (GL_LINE_LOOP);
glVertex2f(0.0, 25.0);
glVertex2f(25.0, -25.0);
glVertex2f(-25.0, -25.0);
glEnd();
}

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

glLoadIdentity ();
glColor3f (1.0, 1.0, 1.0);
draw_triangle ();

glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0xF0F0);
glLoadIdentity ();
glTranslatef (-20.0, 0.0, 0.0);
draw_triangle ();

glLineStipple (1, 0xF00F);
glLoadIdentity ();
glScalef (1.5, 0.5, 1.0);
draw_triangle ();

glLineStipple (1, 0x8888);
glLoadIdentity ();
glRotatef (90.0, 0.0, 0.0, 1.0);
draw_triangle ();
glDisable (GL_LINE_STIPPLE);

glFlush ();
}

/*void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}*/

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);
// glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

Line Triangle Transformation

#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
void triangle()
{
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex2i (80, 150);
glVertex2i (80, 250);
glVertex2i (130,200);
glEnd();
}
void line(int sx,int sy,int dx,int dy)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2i (sx, sy);
glVertex2i (dx, dy);
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
line(100,150,100,250);
triangle();
glTranslatef(70.0, 20.0,0.0);
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xF0F0);
glScalef (1.5, 0.5, 1.0);
    line(100,150,100,250);
triangle();
glFlush ();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
gluOrtho2D(0.0, 350.0, 0.0, 350.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow (“Transformation”);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Rectangle drawing program

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
glColor3f(0.4,0.0,0.0);
  glRecti(200,150,500,330);
 /* glBegin(GL_LINES); //draw lines
  {
  glVertex2i(200,150);
  glVertex2i(200,330);
  glVertex2i(500,330);
  glVertex2i(500,150);
  glVertex2i(500,330);
  glVertex2i(500,150);
  glVertex2i(200,150);
  glVertex2i(500,150);
  glVertex2i(200,330);
  glVertex2i(500,330);
  }
  glEnd();*/
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  //glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}

Polygon

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);   //clear the screen
  // glRecti(10,10,150,60); //draw a rectangle with background color
 /*glBegin(GL_POINTS); //draw three points
  {
    for(GLint x=0; x<50; x+=2){
      if(x<25)
      glVertex2i(100+x,x);
    else
       glVertex2i(x,x+100);
    }
    }
  glEnd();*/
    glBegin(GL_POLYGON); //draw lines
  {
   glColor3f(1.0,0.0,0.0);
  glVertex2i(100,50);
  glVertex2i(200,130);
  glVertex2i(200,70);
 // glVertex2i(200,130);
  glVertex2i(500,170);
  glVertex2i(300,130);
    }
  glEnd();
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  //glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}

myMouse program

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
GLdouble A,B,C,D; // values for scaling and shifting
void myInit(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0);  //background color is white
  glColor3f(0.0f, 0.0f, 0.0f);  //drawing color is black
  glPointSize(4.0); //a dot is 2 by 2 pixels
  //glLineWidth(4.0); //a line is 4 times thicker
//initialize view (simple orthographic projection)
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
  A = screenWidth/4.0; //Set values for scaling and shifting
  B= 0.0;
  C=D=screenHeight/2.0;
  //set the viewing coordinates
  gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myMouse(int button, int state, int x ,int y)
{
  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
      glColor4f(0.0,1.0,0.0,1.0);
      glRecti(200,150,500,330);  // create a rectangle
      glFlush();
    }
  else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    {
    //glPointSize(2.0);
      glColor3f(1.0,0.0,0.0);
  GLint h = 350,k = 240, x, y, r, d;
  y = r = 60;
  d = 3 – 2*r;
  for(x=0; x<=y; x++)
    {
  glBegin(GL_POINTS);
      {
glVertex2i(x+h,y+k);
glVertex2i(x+h,k-y);
glVertex2i(y+h,x+k);
glVertex2i(y+h,k-x);
glVertex2i(-y+h,x+k);
glVertex2i(-y+h,k-x);
glVertex2i(-x+h,y+k);
glVertex2i(-x+h,k-y);
      }
      glEnd();
  if(d<0)
  d = d + 4*x + 6;
  else{
  d = d + 4*(x-y) + 10;
  y–;
  }
}
  glFlush();
  } //elseif
}
void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush(); //send all output to display
}
int  main(int argc, char** argv)
{
  glutInit(&argc, argv);  //initialize the toolkit
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
  glutInitWindowSize(640,480);  //set window size
  //set window position on screen
  glutInitWindowPosition(100,150);
  //open the screen window and set the name
  glutCreateWindow(“My First Window”);
  //register the callback functions
  glutDisplayFunc(myDisplay);
  glutMouseFunc(myMouse);
  myInit();
  glutMainLoop();  //go into a perpetual loop
  //fprintf(stderr, “Hello!\n”);
  return 1;
}