Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Friday, March 18, 2016

DDA Line Drawing C Program Using graphics.h

DDA Line Drawing C Program

Digital differential analyzer (DDA) is a floating-point operation based computer line drawing algorithm.

Follow The Instructions To Successfully Run The Program In Dev-Cpp/CodeBlock:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32


Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.

Code

#include <stdio.h>
#include <math.h>
#include <graphics.h>

void dda(int x1, int y1, int x2, int y2)
{
    initwindow(500, 500, "DDA");
    int step, xInc, yInc, x, y, dx, dy;
    dx = x2 - x1; dy = y2 - y1;
    step = (abs(dx) > abs(dy))? dx : dy;
    xInc = dx / step; 
    yInc = dy / step;
    x = x1; 
    y = y1;
    putpixel(round(x), round(y), 1);
    
    for (int i = 0; i < step; i++) {
        x += xInc; y += yInc;
        putpixel(round(x), round(y), 1);
    }
}

int main()
{
    int x1,y1, x2, y2;
    printf("Enter The Points:\n");
    printf("(x1,y1): ? ");
    scanf("%d%d",&x1,&y1);
    printf("(x2,y2): ? ");
    scanf("%d%d",&x2,&y2);
    dda(x1,y1,x2,y2);
    while(!kbhit());
    return 0;
}

Output



Saturday, March 5, 2016

FCFS CPU Scheduling Algorithm

FCFS Scheduling Algorithm

First Come First Serve is a CPU scheduling algorithm where CPU execute each process according there appearance.

Code

/****Scheduling algorithm for FCFS****/
#include <stdio.h>
#include >malloc.h<

int main()
{
	int n,*b,*w,i,j,h;
	float avg =0;
	
	printf("\nEnter number of jobs:");
	scanf("%d",&n);
	
	//Create an dynamic array for specified job
	b=(int *)malloc(n*sizeof(int)); //Array for holding the burst time of the jobs
	w=(int *)malloc(n*sizeof(int)); //Array for holding the waiting time of the jobs
	
	//Taking the burst time from the user
	printf("\nEnter the burst time for corresponding jobs:");
	for(i=0;i<n;i++)
	{
		printf("\nProcess %d:",i+1);
		scanf("%d",&b[i]);
	}
	
	w[0]=0;
	printf("\nProcess 1 waiting time is 0"); //First process waiting time is always 0
	for(i=1;<i++)
	{
		w[i]=b[i-1]+w[i-1]; //Calculate the waiting time of the ith process
		printf("\nProcess %d waiting time is %d",i+1,w[i]); //Print the waiting time for the corresponding process
		avg+=w[i]; //Calculate the total waiting time
	}
	
	printf("\nTotal waiting time:%f",avg); //Print the total waiting time
	printf("\nThe average waiting time:%f",avg); //Print the average waiting time

	return 0;
}

Output

Enter number of jobs:5

Enter the burst time for corresponding jobs:
Process 1:3

Process 2:2

Process 3:5

Process 4:7

Process 5:2

Process 1 waiting time is 0
Process 2 waiting time is 3
Process 3 waiting time is 5
Process 4 waiting time is 10
Process 5 waiting time is 17
Total waiting time:35.000000
The average waiting time:7.000000

Shortest Job First CPU Scheduling | Operating System

SJF Scheduling Algorithm

Shortest Job First is a CPU scheduling algorithm where the CPU execute the process first which have the shorest burst time.

Code

/***Scheduling algorithm for SJF***/
#include <stdio.h>
#include <malloc.h>


int main()
{
 int n,*b,*w,*a,i,j,h,t,tt;
 float avg =0;
 
 printf("\nEnter the numbers of jobs:");
 scanf("%d",&n);
 
 b=(int *)malloc(n*sizeof(int)); //Array for holding the burst time of the jobs
 w=(int *)malloc(n*sizeof(int)); //Array for holding the waiting time of the jobs
 a=(int *)malloc(n*sizeof(int)); //Array for holding the jobs number according their appearance
 
 //Taking the burst time from the user
 printf("\nEnter the burst time for corresponding jobs:");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d:",i);
  scanf("%d",&b[i]);
  a[i]=i;
 }
 
 for(i=1;i<=n;i++)
 {
  for(j=i;j<=n;j++)
  {
   //Shorting the process according to their burst time in ascending order
   if(b[i]>b[j]) 
   {
    t=b[i];tt=a[i];
    b[i]=b[j];a[i]=a[j];
    b[j]=t;a[j]=tt;
   }
   
   //If both process has same burst time then short them according their appearance
   if(b[i]==b[j]) 
   {
    if(a[i]>a[j])
    {
     t=a[i];
     a[i]=a[j];
     a[j]=t;
    }
   }
  }
 }
 
 w[1]=0;
 printf("\nProcess %d waiting time is 0",a[1]); //The first process waiting time is always 0
 for(i=2;i<=n;i++)
 {
  w[i]=b[i-1]+w[i-1]; //Calculate the waiting time of the i'th process
  printf("\nProcess %d waiting time is %d",a[i],w[i]); //Print the waiting time
  avg+=w[i]; //Calculate the total waiting time
 }
 
 printf("\nTotal waiting time :%f",avg); //Print the total waiting time
 printf("\nThe average waiting time :%f",avg/n);//Print the average waiting time

 return 0;
}

Output

Enter the numbers of jobs:5

Enter the burst time for corresponding jobs:
Process 1:4

Process 2:3

Process 3:6

Process 4:2

Process 5:1

Process 5 waiting time is 0
Process 4 waiting time is 1
Process 2 waiting time is 3
Process 1 waiting time is 6
Process 3 waiting time is 10
Total waiting time :20.000000
The average waiting time :4.000000

Wednesday, March 2, 2016

C Program For Rotation Using graphics.h

C Program For Rotation Of An Object

We have already described what is Rotation in previous posts. Here is the c code using graphics.h library functions.
Follow The Instructions To Successfully Run The Program:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32


Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.

Code

#include <stdio.h>
#include <math.h>
#include <graphics.h>

void print(int a[][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

}

void multiply(int a[][3], int b[][3], int c[][3])
{
    int sum = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                sum += (a[i][k] * b[k][j]);
            }
            c[i][j] = sum;
            sum = 0;
        }
    }
}

void rotation(int a[][3])
{
    int deg; 
    float c[3][3], sum = 0.0; 
    float t[3][3];
    printf("Enter The Angel: ");
    scanf("%d",&deg);
        
    t[0][0] = t[1][1] = cos(deg*3.14/180);
    t[0][1] = sin(deg*3.14/180);
    t[1][0] = -t[0][1];
    t[2][2] = 1; t[0][2] = t[2][0] = 0;
    t[2][1] = t[1][2] = 0;
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%g ", t[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                sum += (t[i][k] * a[k][j]);
            }
            c[i][j] = round(sum);
            sum = 0;
        }
    }
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%g ", c[i][j]);
        }
        printf("\n");
    }
    line(c[0][0], c[1][0], c[0][1], c[1][1]);
    line(c[0][1], c[1][1], c[0][2], c[1][2]);
    line(c[0][2], c[1][2], c[0][0], c[1][0]);
}

int main()
{
    initwindow(600, 600);
    int a[3][3];
    int x1, y1, x2, y2, x3, y3;
    int ch;
    printf("Enter The Initital Points: \n");
    printf("Enter X1, Y1: \n");
    scanf("%d%d",&x1,&y1);
    printf("Enter X2, Y2: \n");
    scanf("%d%d",&x2,&y2);
    printf("Enter X3, Y3: \n");
    scanf("%d%d",&x3,&y3);
    
    line(x1,y1,x2,y2);
    line(x2,y2,x3,y3);
    line(x3,y3,x1,y1);
    
    a[0][0] = x1;
    a[1][0] = y1;

    a[0][1] = x2;
    a[1][1] = y2;

    a[0][2] = x3;
    a[1][2] = y3;

    a[2][0] = a[2][1] = a[2][2] = 1;
    print(a);  
    
    rotation(a);

    while(!kbhit());
    return 0;
}

Output


C Program For Translation Using graphics.h

C Program For Translation Of An Object

We have already described what is translation in previous posts. Here is the c code using graphics.h library functions.

Follow The Instructions To Successfully Run The Program:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32


Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.

Code

#include <stdio.h>
#include <math.h>
#include <graphics.h>

void print(int a[][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

}

void multiply(int a[][3], int b[][3], int c[][3])
{
    int sum = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                sum += (a[i][k] * b[k][j]);
            }
            c[i][j] = sum;
            sum = 0;
        }
    }
}


void traslation(int a[][3])
{
    int ty, tx;
    int t[3][3], c[3][3];
    
    printf("Enter The Traslation Parameters tx and ty: ");
    scanf("%d%d",&tx, &ty);
    
    t[0][0] = t[1][1] = t[2][2] = 1;
    t[0][2] = tx; t[1][2] = ty;
    t[0][1] = t[1][0] = t[2][0] = t[2][1] = 0;
    print(t);
    multiply(t, a, c);
    
    line(c[0][0], c[1][0], c[0][1], c[1][1]);
    line(c[0][1], c[1][1], c[0][2], c[1][2]);
    line(c[0][2], c[1][2], c[0][0], c[1][0]);
    
    print(c);
}

int main()
{
    initwindow(600, 600);
    int a[3][3];
    int x1, y1, x2, y2, x3, y3;
    int ch;
    printf("Enter The Initital Points: \n");
    printf("Enter X1, Y1: \n");
    scanf("%d%d",&x1,&y1);
    printf("Enter X2, Y2: \n");
    scanf("%d%d",&x2,&y2);
    printf("Enter X3, Y3: \n");
    scanf("%d%d",&x3,&y3);
    
    line(x1,y1,x2,y2);
    line(x2,y2,x3,y3);
    line(x3,y3,x1,y1);
    
    a[0][0] = x1;
    a[1][0] = y1;

    a[0][1] = x2;
    a[1][1] = y2;

    a[0][2] = x3;
    a[1][2] = y3;

    a[2][0] = a[2][1] = a[2][2] = 1;
    print(a);  
    
    traslation(a);

    while(!kbhit());
    return 0;
}

Output


C Program For Scaling Using graphics.h

C Program For Scaling An Object

We have already described what is translation in previous posts. Here is the c code using graphics.h library functions.

Follow The Instructions To Successfully Run The Program:

Whenever you #include <graphics.h> in a program, you must instruct the linker to 
link in certain libraries. The command to do so from Dev-C++ is Alt-P. Choose the 
Parameters tab from the pop-up window and type the following into the Linker area:

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32


Note: for loops in the program is written using -std=c99 or -std=gnu99 syntex.

Code

#include <stdio.h>
#include <math.h>
#include <graphics.h>

void print(int a[][3])
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

}

void multiply(int a[][3], int b[][3], int c[][3])
{
    int sum = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                sum += (a[i][k] * b[k][j]);
            }
            c[i][j] = sum;
            sum = 0;
        }
    }
}


void scaling(int a[][3])
{
    int sx, sy;
    int t[3][3], c[3][3];
    
    printf("Enter The Scaling Parameters tx and ty: ");
    scanf("%d%d",&sx, &sy);
    
    t[0][0] = sx; t[1][1] = sy; t[2][2] = 1;
    t[0][1] = t[1][0] = t[0][2] = t[2][0] = 0;
    t[1][2] = t[2][1] = 0;
    
    print(t);
    multiply(t, a, c);
    print(c);   
    line(c[0][0], c[1][0], c[0][1], c[1][1]);
    line(c[0][1], c[1][1], c[0][2], c[1][2]);
    line(c[0][2], c[1][2], c[0][0], c[1][0]);
    
}
int main()
{
    initwindow(600, 600);
    int a[3][3];
    int x1, y1, x2, y2, x3, y3;
    int ch;
    printf("Enter The Initital Points: \n");
    printf("Enter X1, Y1: \n");
    scanf("%d%d",&x1,&y1);
    printf("Enter X2, Y2: \n");
    scanf("%d%d",&x2,&y2);
    printf("Enter X3, Y3: \n");
    scanf("%d%d",&x3,&y3);
    
    line(x1,y1,x2,y2);
    line(x2,y2,x3,y3);
    line(x3,y3,x1,y1);
    
    a[0][0] = x1;
    a[1][0] = y1;

    a[0][1] = x2;
    a[1][1] = y2;

    a[0][2] = x3;
    a[1][2] = y3;

    a[2][0] = a[2][1] = a[2][2] = 1;
    print(a);  
    
    scaling(a);

    while(!kbhit());
    return 0;
}

Output


Basic Transformations In Computer Graphics

Basic Transformation methods

Aim
To implement set of basic transformations on polygon i.e. Translation, Rotation, Scaling, Reflection and Shearing
Pre-requisite:
Knowledge of matrix fundamentals and basic transformations on polygon i.e. translation, rotation, scaling, reflection and shearing.
Description
Transformations allows us to uniformly alter the entire picture. The geometric transformations considered here - translation, scaling and rotation are expressed in terms of matrix multiplication. Homogeneous coordinates are considered to uniformly treat the translations

Scaling Transformation

A 2D point can be scaled by multiplication of the coordinate values (x,y) by scaling factors Sx and Sy to produce the transformed coordinates (x',y').

Matrix format: [X'] = [S]x[X]
Where [X']- Transformed Matrix; 
[X]- Point or Object Matrix; and 
[S]- Scaling Matrix

[x1' x2' x3']   [sx 0 0]   [x1 x2 x3]  
[y1' y2' y3'] = [0 sy 0] x [y1 y2 y3]
[1    1   1 ]   [0 0  1]   [1   1  1]

Translation Transformation

A 2D point can be translated by adding the coordinate values (x,y) by Translation distances tx and ty to produce the transformed coordinates (x',y').

Matrix format: [X'] = [T]x[X]
Where [X']- Transformed Matrix; 
[X]- Point or Object Matrix; and 
[T]- Translation Matrix

[x1' x2' x3']   [1 0 tx]   [x1 x2 x3]  
[y1' y2' y3'] = [0 1 ty] x [y1 y2 y3]
[1    1   1 ]   [0 0  1]   [1   1  1]

Rotaion Transformation

A 2D point can be rotated by re-positioning it along a circular path in the xy plane. We specify the rotation angle and the position of the rotation point about which the object is to be rotated. Multiplication of the coordinate values (x,y) by rotation matrix produce the transformed coordinates (x',y').

Matrix format: [X'] = [R]x[X]
Where [X']- Transformed Matrix; 
[X]- Point or Object Matrix; and 
[R]- Rotation Matrix


[x1' x2' x3']   [cos(Θ) -sin(Θ) 0]   [x1 x2 x3]    
[y1' y2' y3'] = [sin(Θ)  cos(Θ) 0] x [y1 y2 y3]  
[1    1   1 ]   [0          0   1]   [1   1  1]    


Calculator Application Using Java

Calculator Application Using Java AWT

Here is the java code for a simple calculator application that will look like this.

Code:

import java.awt.*;
import java.awt.event.*;

public class CalGUI implements ActionListener, KeyListener {

    String[] s =  {"", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "=", "+", "C"};
    Frame f;
    Button[] b = new Button[16];
    TextField text;
    int x = 35, y = 100;
    float res = 0.0f;   
    String msg = "";
    char op;

    public CalGUI() {
        f = new Frame("Calculator");
        f.setSize(400, 450);
        f.setResizable(false);
        f.setLayout(null);
        f.setBackground(Color.gray);
        text = new TextField(45);
        text.setFocusable(false);
        text.setBounds(25, 40, 350, 40);
        text.setEditable(false);
        text.setFont(new Font("Consolas",1,20));
        f.add(text);
        for (int i = 1; i < s.length; i++) {
            b[i-1] = new Button(s[i]);
            b[i-1].setFont(new Font("Consolas",1,20));
            b[i-1].setBackground(Color.LIGHT_GRAY);
            b[i-1].setBounds(x, y, 80, 80);
            x=x+80;
            if(i % 4 == 0 && i != 0) {y = y + 80; x = 35;}
            f.add(b[i-1]);
        }
        
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        for(int i = 0; i < b.length; i++) {
            b[i].addActionListener(this);
        }
        f.addKeyListener(this);
        
    }
    
    public void keyPressed(KeyEvent ke)
    {
        char ch = ke.getKeyChar();
        if (ch >= '0' && ch <= '9') {
            msg += ch;
            text.setText(msg);
        }
        else if(ch == '=') {
            switch(op) {
                case '+': 
                    res = res + Integer.parseInt(text.getText()); 
                    break;
                case '-':
                    res = res - Integer.parseInt(text.getText()); 
                    break;
                case '*':
                    res = res * Integer.parseInt(text.getText()); 
                    break;
                case '/':
                    res = res / (float)Integer.parseInt(text.getText()); 
                    break;
                    
            }
            text.setText("Ans. " + res);
            msg="";
        }
        else if(ch == 'C') {
            text.setText("");
            res = 0;
            msg ="";
        }
        else {
            res = Integer.parseInt(text.getText());
            text.setText("");
            msg = "";
            switch(ch) {
                case '+': op = '+'; break;
                case '*': op = '*'; break;
                case '/': op = '/'; break;
                case '-': op = '-'; break;
            }
        }
        
    }
    
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        char ch = str.charAt(0);
        if (ch >= '0' && ch <= '9') {
            msg += str;
            text.setText(msg);
        }
        else if(ch == '=') {
            switch(op) {
                case '+': 
                    res = res + Integer.parseInt(text.getText()); 
                    break;
                case '-':
                    res = res - Integer.parseInt(text.getText()); 
                    break;
                case '*':
                    res = res * Integer.parseInt(text.getText()); 
                    break;
                case '/':
                    res = res / (float)Integer.parseInt(text.getText()); 
                    break;
                    
            }
            text.setText("Ans. " + res);
            msg="";
        }
        else if(ch == 'C') {
            text.setText("");
            res = 0;
            msg ="";
        }
        else {
            res = Integer.parseInt(text.getText());
            text.setText("");
            msg = "";
            switch(ch) {
                case '+': op = '+'; break;
                case '*': op = '*'; break;
                case '/': op = '/'; break;
                case '-': op = '-'; break;
            }
        }
    }
    
    public static void main(String[] arg) {
        new CalGUI();
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        char ch = arg0.getKeyChar();
        if (ch >= '0' && ch <= '9') {
            msg += ch;
            text.setText(msg);
        }
        else if(ch == '=') {
            switch(op) {
                case '+': 
                    res = res + Integer.parseInt(text.getText()); 
                    break;
                case '-':
                    res = res - Integer.parseInt(text.getText()); 
                    break;
                case '*':
                    res = res * Integer.parseInt(text.getText()); 
                    break;
                case '/':
                    res = res / (float)Integer.parseInt(text.getText()); 
                    break;
                    
            }
            text.setText("Ans. " + res);
            msg="";
        }
        else if(ch == 'C') {
            text.setText("");
            res = 0;
            msg ="";
        }
        else {
            res = Integer.parseInt(text.getText());
            text.setText("");
            msg = "";
            switch(ch) {
                case '+': op = '+'; break;
                case '*': op = '*'; break;
                case '/': op = '/'; break;
                case '-': op = '-'; break;
            }
        }
        
    }

    @Override
    public void keyReleased(KeyEvent arg0) {}
}