Simple GUI Notepad Using Ruby

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

Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Saturday, April 2, 2016

Boundary Fill C Program Using graphics.h

Boundary Fill Algorithm

Unlike Flood Fill Algorithm, Boundary Fill Algorithm starts at a point inside a region and paint the interior outward toward the boundary. If the boundary is specified in a single color, the fill algorithm proceeds outward pixel by pixel until the boundary color is encountered. This method, called the boundary-fill 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

Code

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

/* fc = fill color and bc = boundary color */
void boundaryfill(int x, int y, int fc, int bc)
{
    int c = getpixel(x, y);
    if (c != fc && c != bc) {
        putpixel(x, y, fc);
        boundaryfill(x, y + 1, fc, bc);
        boundaryfill(x, y - 1, fc, bc);
        boundaryfill(x - 1, y, fc, bc);
        boundaryfill(x + 1, y, fc, bc);
    }
}

int main()
{
    int arr[] = {10, 10, 100,10, 10, 100, 10, 10};
    initwindow(300,300,"BoundaryFill");
    drawpoly(4, arr);
    
    delay(3000);
    
    boundaryfill(40, 40, 4, 15);
    
    while(!kbhit());
    
    return 0;
}

Output


Friday, April 1, 2016

Flood Fill C Program Using graphics.h

Flood Fill Algorithm

Sometimes we want to fill in (or recolor) an area that is not defined within a single color boundary. Here is a figure describing the situation.

We can paint such areas by replacing a specified interior color instead of searching for a boundary color value. This approach is called a flood-fill algorithm. We can use either a 4-connected or 8-connected approach,

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

Code

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


/* oc = old color and fc = fill color */
void floodfill(int x, int y, int fc, int oc)
{
    int c = getpixel(x, y);
    if (c == oc) {
        putpixel(x, y, fc);
        floodfill(x, y + 1, fc, oc);
        floodfill(x, y - 1, fc, oc);
        floodfill(x - 1, y, fc, oc);
        floodfill(x + 1, y, fc, oc);
    }
}

int main()
{
    int arr[] = {10, 10, 100,10, 10, 100, 10, 10};
    initwindow(300,300,"FloodFill");
    drawpoly(4, arr);
    
    delay(3000);
    
    floodfill(40, 40, 4, 0);
    
    while(!kbhit());
    
    return 0;
}

Output


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



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]