0% found this document useful (0 votes)
506 views26 pages

Computer Graphics (22318) : Diploma in Computer Technology

This document is a micro-project report on designing rangoli patterns using computer graphics in C language. It includes three unique rangoli designs coded using concepts like 2D transformations, polygon filling techniques, and Bresenham's circle drawing algorithm. The first rangoli consists of 8 rotated squares connected in the center. It is colored using flood fill. The second rangoli overlaps two triangles in red and yellow with a background circle. The third rangoli draws many circles at angles using nested loops. Code snippets are provided for the algorithms and rangoli designs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
506 views26 pages

Computer Graphics (22318) : Diploma in Computer Technology

This document is a micro-project report on designing rangoli patterns using computer graphics in C language. It includes three unique rangoli designs coded using concepts like 2D transformations, polygon filling techniques, and Bresenham's circle drawing algorithm. The first rangoli consists of 8 rotated squares connected in the center. It is colored using flood fill. The second rangoli overlaps two triangles in red and yellow with a background circle. The third rangoli draws many circles at angles using nested loops. Code snippets are provided for the algorithms and rangoli designs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Maharashtra Board of Technical Education, Mumbai

GOVERNMENT POLYTECHNIC,
SOLAPUR

DIPLOMA IN COMPUTER TECHNOLOGY


ACADEMIC YEAR 2021-22

COMPUTER GRAPHICS
(22318)

A
MICRO-PROJECT REPORT
ON

RANGOLI DESIGNS USING GRAPHICS IN C

Submitted by:
Roll No. 82 – Sharaneshwar Bharat Punjal

Submitted to:
Smt. S. P. Kadam

1
CERTIFICATE

This is to certify that the following student Sharaneshwar Bharat

Punjal, Roll No. 82 of Branch CM3I of the institute Government Polytechnic,

Solapur (0015) has completed the micro-project work satisfactorily under my

supervision/guidance for the subject Computer Graphics (22318) in the

academic year 2020-2021 as prescribed in the curriculum.

Project guide H.O.D Principal


(Smt. S. P. Kadam) (Mr. A.L. Tarange) (Mr. S.K. Hunasimarad)

ACKNOWLEDGEMENT
In the accomplishment of this micro-project successfully, many people
have best owned upon me their blessings and heart-privileged support.
Primarily, I would like to express a special thanks of gratitude to the Principal

2
Sir of the Government Polytechnic, Solapur for giving this golden opportunity
with all the required facilities for completing this micro-project of our group.

I would like to extend my gratitude to our CGR subject teacher, Smt. S.


P. Kadam mam, whose valuable guidance has been the ones that helped us
patch this project and make it full proof success. Their suggestions and
instructions has served as the major contributor towards the completion of the
micro-project.

I would also like to thank my parents who have helped with their valuable
suggestions and provided the required resources needed for the micro-project.

3
ABSTRACT
As a student enrolled in the Government Polytechnic, Solapur, every
semester we require to do a micro-project on any one topic in the syllabus of the
respective subjects. Hence, I have done a micro-project to design rangolis using
computer graphics in C language.

This micro-project mainly highlights the polygon filling techniques in


computer graphics. It also shows the implementation of 2D transformations in
polygons like translation, scaling and rotation. Other concepts like Bresenham’s
circle drawing algorithm, flood fill, boundary fill algorithm are also
implemented in this micro-project.

The micro-project is made in such a way that it is a user-friendly and is


easy to understand the code that is written in the programs. Hoping that the
readers would feel it creative. Any suggestions for the improvement of this
project are sincerely appreciated.

4
INTRODUCTION
In this micro-project, three unique and creative rangoli designs are coded
using computer graphics in C language. This first rangoli is designed using the
2D rotation of square. It consists of 8 squares each rotated at 45 degrees and all
the squares are connected at the centre. The design that gets formed after
drawing these 8 squares is coloured with the help of flood fill algorithm by light
and dark shades of red, cyan, green and magenta. A big circle is also drawn and
coloured with blue colour.

The second rangoli consists of a design in which two triangles are


overlapped upon each other coloured in red and yellow colours. In this rangoli
also a big circle is drawn using Bresenham’s circle drawing algorithm at the
background. The third rangloli is a design in which many circles are drawn with
each circle apart at a particular angle from the other. The rotating of circles is
done with the help of nested for loops and after all the circles are drawn, the
design looks so beautiful that shows uniformity in the rangoli.

The first rangoli code is of 260 lines, second rangoli code is of 140 lines
and the third rangoli code is of 60 lines.

5
REVIEW OF CONCEPTS
A short review of the important algorithms of computer graphics in C that
are used in this micro-project is here.

1. BRESENHAM’S CIRCLE DRAWING ALGORITHM



This algorithm uses the key feature of circle that it is highly symmetric.
So, for whole 360 degree of circle we will divide it in 8-parts each octant of 45
degree. In order to do that we will use Bresenham’s Circle Algorithm for
calculation of the locations of the pixels in the first octant of 45 degrees. It
assumes that the circle is centered on the origin. So for every pixel (x, y) it
calculates, we draw a pixel in each of the 8 octants of the circle. Now, at any
point (x, y) we have two option either to choose the next pixel in the east i.e.
(x+1, y) or in the south east i.e. (x+1, y-1). And this can be decided by using the
decision parameter d as:
 If d > 0, then (x+1, y-1) is to be chosen as the next pixel as it will be closer to
the arc.
 else (x+1, y) is to be chosen as next pixel.

Now to draw the circle for a given radius ‘r’ and centre (xc, yc) We will
start from (0, r) and move in first quadrant till x=y (i.e. 45 degree). We should
start from listed initial condition:
d = 3 - (2 * r)
x=0
y=r

Now for each pixel, we will do the following operations:

6
1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).
3. call drawCircle(int xc, int yc, int x, int y) function.
4. Repeat steps 5 to 8 until x < = y
5. Increment value of x.
6. If d < 0, set d = d + (4 * x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call drawCircle(int xc, int yc, int x, int y) function

drawCircle() function definition:


drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}

7
2. FLOOD FILL ALGORITHM –
In this method, a point or seed which is inside region is selected. This
point is called a seed point. Then four connected approaches or eight connected
approaches is used to fill with specified color.
The flood fill algorithm has many characters similar to boundary fill. But
this method is more suitable for filling multiple colors boundary. When
boundary is of many colors and interior is to be filled with one color we use this
algorithm.
In fill algorithm, we start from a specified interior point (x, y) and
reassign all pixel values are currently set to a given interior color with the
desired color. Using either a 4-connected or 8-connected approaches, we then
step through pixel positions until all interior points have been repainted.

Flood fill Algorithm:


floodfill (x, y, fill_ color, old_color)
{
If (getpixel (x, y) = old_color)
{
setpixel (x, y, fill_color);
fill (x+1, y, fill_color, old_color);
fill (x-1, y, fill_color, old_color);
fill (x, y+1, fill_color, old_color);
fill (x, y-1, fill_color, old_color);
fill (x+1, y+1, fill_color, old_color);
fill (x-1, y+1, fill_color, old_color);
fill (x+1, y-1, fill_color, old_color);
fill (x-1, y-1, fill_color, old_color);
}
}
 SIGNIFICANCE OF THE PROJECT -
The main significance of this micro-project is to use different algorithms like
Bresenham’s line and circle algorithms and polygon filling algorithms to create

8
computer graphics programs. The rangolis which we used to draw manually are
now being created with the help of computer graphics in C language.

 SCOPE OF THE PROJECT -


Many concepts of computer graphics in C are involved in this micro-project, but
this micro-project focuses on creating static drawings i.e., animation and
moving objects are not involved in this project. Also, the project is scope to 2D
transformation only.

 HARDWARE REQUIREMENTS -
Laptop or PC or Mobile with Basic Configurations.

 SOFTWARE REQUIREMENTS -
1. Operating System (Windows family)
2. IDE for C language (Turbo C, Visual Studio Code, Dev C++, etc.)
3. Text Editor

9
C PROGRAM CODE
- RANGOLI 01 -
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <dos.h>

void EightWaySymm(int xc, int yc, int x, int y);


void BresenhamCircle(int xc, int yc, int r);
void flood_fill(int x, int y, int n, int o);
void draw_square(int square[5][2]);
void rotate_square_x(int *sq, int dis);
void rotate_square_y(int *sq, int dis);
void rotate_square_x2(int *sq, int dis);
void rotate_square_y2(int *sq, int dis);
void reflect_square_x(int *sq);
void reflect_square_y(int *sq);
void reflect_square_x2(int *sq);
void copy_square(int *sq, int square[5][2]);
int calculate_distance(int x1, int y1, int x2, int y2);

void main()
{
    int gd = DETECT, gm, diagonal_distance, i;
    int square[5][2] = {{320, 240}, {320 + 100, 240}, {320 + 100,
240 + 100}, {320, 240 + 100}, {320, 240}};
    int sq_rotated[5][2] = {0};
    diagonal_distance = calculate_distance(square[0][0], square[0]
[1], square[2][0], square[2][1]);

    initgraph(&gd, &gm, "C:\\TC\\BGI");


    delay(500);
    setfillstyle(1, DARKGRAY);
    floodfill(1, 1, WHITE);
    delay(500);
    outtextxy(280, 10, "RANGOLI 01");
    delay(500);

10
    // 1st square
    draw_square(square);
    delay(500);
    // 2nd square
    copy_square(&sq_rotated[0][0], square);
    rotate_square_x(&sq_rotated[0][0], diagonal_distance);
    draw_square(sq_rotated);
    delay(500);
    // 3rd square
    reflect_square_x(&square[0][0]);
    draw_square(square);
    delay(500);
    // 4th square
    copy_square(&sq_rotated[0][0], square);
    rotate_square_y(&sq_rotated[0][0], diagonal_distance);
    draw_square(sq_rotated);
    delay(500);
    // 5th square
    reflect_square_y(&square[0][0]);
    draw_square(square);
    delay(500);
    // 6th square
    copy_square(&sq_rotated[0][0], square);
    rotate_square_x2(&sq_rotated[0][0], diagonal_distance);
    draw_square(sq_rotated);
    delay(500);
    // 7th square
    reflect_square_x2(&square[0][0]);
    draw_square(square);
    delay(500);
    // 8th square
    copy_square(&sq_rotated[0][0], square);
    rotate_square_y2(&sq_rotated[0][0], diagonal_distance);
    draw_square(sq_rotated);
    delay(500);

    // Filling objects
    setfillstyle(1, MAGENTA);
    floodfill(330, 290, WHITE);
    delay(500);
    setfillstyle(1, LIGHTMAGENTA);
    floodfill(380, 250, WHITE);

11
    delay(500);
    setfillstyle(1, CYAN);
    floodfill(410, 230, WHITE);
    delay(500);
    setfillstyle(1, LIGHTCYAN);
    floodfill(330, 190, WHITE);
    delay(500);
    setfillstyle(1, GREEN);
    floodfill(310, 220, WHITE);
    delay(500);
    setfillstyle(1, LIGHTGREEN);
    floodfill(270, 230, WHITE);
    delay(500);
    setfillstyle(1, RED);
    floodfill(230, 250, WHITE);
    delay(500);
    setfillstyle(1, LIGHTRED);
    floodfill(310, 280, WHITE);
    delay(500);
    setfillstyle(1, BLUE);
    floodfill(410, 330, WHITE);
    floodfill(410, 150, WHITE);
    floodfill(230, 150, WHITE);
    floodfill(230, 330, WHITE);
    floodfill(430, 240, WHITE);
    floodfill(330, 130, WHITE);
    floodfill(210, 240, WHITE);
    floodfill(310, 350, WHITE);

    // Outside circle
    delay(500);
    BresenhamCircle(320, 240, 200);
    delay(500);
    setfillstyle(1, LIGHTBLUE);
    floodfill(420, 345, WHITE);
    delay(500);

    getch();
    closegraph();
}

void EightWaySymm(int xc, int yc, int x, int y)


{

12
    putpixel(x + xc, y + yc, getcolor());
    putpixel(x + xc, -y + yc, getcolor());
    putpixel(-x + xc, -y + yc, getcolor());
    putpixel(-x + xc, y + yc, getcolor());
    putpixel(y + xc, x + yc, getcolor());
    putpixel(y + xc, -x + yc, getcolor());
    putpixel(-y + xc, -x + yc, getcolor());
    putpixel(-y + xc, x + yc, getcolor());
}

void BresenhamCircle(int xc, int yc, int r)


{
    int x = 0, y = r;
    int d = 3 - (2 * r);
    EightWaySymm(xc, yc, x, y);
    while (x <= y)
    {
        if (d <= 0)
        {
            d = d + (4 * x) + 6;
        }
        else
        {
            d = d + (4 * x) - (4 * y) + 10;
            y--;
        }
        x++;
        EightWaySymm(xc, yc, x, y);
    }
}

void flood_fill(int x, int y, int n, int o)


{
    if (getpixel(x, y) == o)
    {
        putpixel(x, y, n);
        flood_fill(x + 1, y, n, o);
        flood_fill(x - 1, y, n, o);
        flood_fill(x, y + 1, n, o);
        flood_fill(x, y - 1, n, o);
        flood_fill(x + 1, y + 1, n, o);
        flood_fill(x - 1, y - 1, n, o);
        flood_fill(x + 1, y - 1, n, o);

13
        flood_fill(x - 1, y + 1, n, o);
    }
}

void copy_square(int *sq, int square[5][2])


{
    int i, j;
    for (i = 0, j = 0; i < 10; i++)
    {
        if (i % 2 == 0)
            *(sq + i) = square[j][0];
        else
        {
            *(sq + i) = square[j][1];
            j++;
        }
    }
}

void rotate_square_x(int *sq, int dis)


{
    *(sq + 2) = *(sq) + (dis / 2);
    *(sq + 3) = *(sq + 1) - (dis / 2);
    *(sq + 4) = *(sq) + dis;
    *(sq + 5) = *(sq + 1);
    *(sq + 6) = *(sq + 2);
    *(sq + 7) = *(sq + 3) + dis;
}

void rotate_square_y(int *sq, int dis)


{
    *(sq + 2) = *sq - (dis / 2);
    *(sq + 3) = *(sq + 1) - (dis / 2);
    *(sq + 4) = *(sq);
    *(sq + 5) = *(sq + 1) - dis;
    *(sq + 6) = *(sq + 2) + dis;
    *(sq + 7) = *(sq + 3);
}

void rotate_square_x2(int *sq, int dis)


{
    *(sq + 2) = *(sq) - (dis / 2);
    *(sq + 3) = *(sq + 1) + (dis / 2);

14
    *(sq + 4) = *(sq)-dis;
    *(sq + 5) = *(sq + 1);
    *(sq + 6) = *(sq + 2);
    *(sq + 7) = *(sq + 3) - dis;
}

void rotate_square_y2(int *sq, int dis)


{
    *(sq + 2) = *sq + (dis / 2);
    *(sq + 3) = *(sq + 1) + (dis / 2);
    *(sq + 4) = *(sq);
    *(sq + 5) = *(sq + 1) + dis;
    *(sq + 6) = *(sq + 2) - dis;
    *(sq + 7) = *(sq + 3);
}

void reflect_square_x(int *sq)


{
    *(sq + 2) = *(sq + 2) - 100;
    *(sq + 3) = *(sq + 3) - 100;
    *(sq + 5) = *(sq + 5) - 200;
    *(sq + 6) = *(sq + 6) + 100;
    *(sq + 7) = *(sq + 7) - 100;
}

void reflect_square_y(int *sq)


{
    *(sq + 2) = *(sq + 2) - 100;
    *(sq + 3) = *(sq + 3) + 100;
    *(sq + 4) = *(sq + 4) - 200;
    *(sq + 6) = *(sq + 6) - 100;
    *(sq + 7) = *(sq + 7) - 100;
}

void reflect_square_x2(int *sq)


{
    *(sq + 2) = *(sq + 2) + 100;
    *(sq + 3) = *(sq + 3) + 100;
    *(sq + 5) = *(sq + 5) + 200;
    *(sq + 6) = *(sq + 6) - 100;
    *(sq + 7) = *(sq + 7) + 100;
}

15
void draw_square(int square[5][2])
{
    int i;
    for (i = 0; i < 4; i++)
        line(square[i][0], square[i][1], square[i + 1][0], square[i
+ 1][1]);
}

int calculate_distance(int x1, int y1, int x2, int y2)


{
    return ceil(sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)));
}

- RANGOLI 02 -
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>

void BresenhamCircle(int xc, int yc, int r);


void EightWaySymm(int xc, int yc, int x, int y);
void flood_fill(int x, int y, int n, int o);

void main()
{
    int gd = DETECT, gm, i;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    delay(500);
    setfillstyle(1, DARKGRAY);
    floodfill(1, 1, WHITE);
    delay(500);
    outtextxy(280, 10, "RANGOLI 02");
    delay(500);
    putpixel(320, 240, getcolor());
    putpixel(321, 241, getcolor());
    putpixel(321, 239, getcolor());
    putpixel(319, 241, getcolor());
    putpixel(319, 239, getcolor());
    for (i = 1; i <= 3; i++)
        BresenhamCircle(320, 240, i);
    delay(500);

16
    line(320, 290, 420, 240);
    delay(500);
    line(370, 265, 370, 165);
    delay(500);
    line(370, 215, 270, 165);
    delay(500);
    line(320, 190, 220, 240);
    delay(500);
    line(270, 215, 270, 315);
    delay(500);
    line(270, 265, 370, 315);

    delay(500);
    line(420, 240, 420, 90);
    delay(500);
    line(370, 165, 220, 90);
    delay(500);
    line(270, 165, 120, 240);
    delay(500);
    line(220, 240, 220, 390);
    delay(500);
    line(270, 315, 420, 390);
    delay(500);
    line(370, 315, 520, 240);

    delay(500);
    line(420, 90, 320, 140);
    delay(500);
    line(220, 90, 220, 190);
    delay(500);
    line(120, 240, 220, 290);
    delay(500);
    line(220, 390, 320, 340);
    delay(500);
    line(420, 390, 420, 290);
    delay(500);
    line(520, 240, 420, 190);

    setfillstyle(1, RED);
    delay(500);
    floodfill(370, 290, WHITE);
    delay(500);
    floodfill(320, 165, WHITE);

17
    delay(500);
    floodfill(245, 240, WHITE);

    setfillstyle(1, YELLOW);
    delay(500);
    floodfill(385, 240, WHITE);
    delay(500);
    floodfill(200, 240, WHITE);
    delay(500);
    floodfill(320, 315, WHITE);

    delay(500);
    BresenhamCircle(320, 240, 210);
    delay(500);
    setfillstyle(1, BLUE);
    floodfill(320, 350, WHITE);
    floodfill(335, 240, WHITE);

    getch();
    closegraph();
}

void EightWaySymm(int xc, int yc, int x, int y)


{
    putpixel(x + xc, y + yc, getcolor());
    putpixel(x + xc, -y + yc, getcolor());
    putpixel(-x + xc, -y + yc, getcolor());
    putpixel(-x + xc, y + yc, getcolor());
    putpixel(y + xc, x + yc, getcolor());
    putpixel(y + xc, -x + yc, getcolor());
    putpixel(-y + xc, -x + yc, getcolor());
    putpixel(-y + xc, x + yc, getcolor());
}

void BresenhamCircle(int xc, int yc, int r)


{
    int x = 0, y = r;
    int d = 3 - (2 * r);
    EightWaySymm(xc, yc, x, y);
    while (x <= y)
    {
        if (d <= 0)
        {

18
            d = d + (4 * x) + 6;
        }
        else
        {
            d = d + (4 * x) - (4 * y) + 10;
            y--;
        }
        x++;
        EightWaySymm(xc, yc, x, y);
    }
}

void flood_fill(int x, int y, int n, int o)


{
    if (getpixel(x, y) == o)
    {
        putpixel(x, y, n);
        flood_fill(x + 1, y, n, o);
        flood_fill(x - 1, y, n, o);
        flood_fill(x, y + 1, n, o);
        flood_fill(x, y - 1, n, o);
        flood_fill(x + 1, y + 1, n, o);
        flood_fill(x - 1, y - 1, n, o);
        flood_fill(x + 1, y - 1, n, o);
        flood_fill(x - 1, y + 1, n, o);
    }
}

19
- RANGOLI 03 -
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <dos.h>

void flood_fill(int x, int y, int n, int o);

void main()
{
    int gd = DETECT, gm, xc, yc, a = 200, b = 75, c = 1;
    int i, j, k;
    double rad1, rad2, x[10], y[10], angle1 = 0, angle2 = 0, pi =
3.141592654;
    char ch;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    delay(500);
    setfillstyle(1, DARKGRAY);
    floodfill(1, 1, WHITE);
    delay(500);
    outtextxy(280, 10, "RANGOLI 03");
    xc = 320;
    yc = 240;
    for (j = 0; j < 1440; j++)
    {
        for (k = 0; k < 8; k++)
        {
            rad1 = (pi * angle1) / 180;
            rad2 = (pi * angle2) / 180;
            x[k] = a * cos(rad1) * cos(rad2) - b * sin(rad1) *
sin(rad2);
            y[k] = a * sin(rad1) * cos(rad2) + b * cos(rad1) *
sin(rad2);
            circle(xc, yc, 5);
            setfillstyle(1, WHITE);
            floodfill(xc, yc, WHITE);
            circle(xc + x[k], yc - y[k], c);
            setfillstyle(1, WHITE);
            floodfill(xc + x[k], yc - y[k], WHITE);

20
            angle1 += 40.0;
        }
        angle2 += 0.25;
        delay(1);
    }
    getch();
    closegraph();
}

void flood_fill(int x, int y, int n, int o)


{
    if (getpixel(x, y) == o)
    {
        putpixel(x, y, n);
        flood_fill(x + 1, y, n, o);
        flood_fill(x - 1, y, n, o);
        flood_fill(x, y + 1, n, o);
        flood_fill(x, y - 1, n, o);
        flood_fill(x + 1, y + 1, n, o);
        flood_fill(x - 1, y - 1, n, o);
        flood_fill(x + 1, y - 1, n, o);
        flood_fill(x - 1, y + 1, n, o);
    }
}

21
PROGRAM OUTPUT
- RANGOLI 01 –

22
- RANGOLI 02 –

23
- RANGOLI 03 –

24
CONCLUSION
The main outcome behind this micro-project was to implement different
algorithms in computer graphics and produce a creative and colourful graphic
using C language. After completing this micro-project, I think that this outcome
has been achieved.

The reason behind choosing this topic is that if we think of rangoli


designs, then this opens up our creativity to do anything with the colours. There
is not any specification for this graphics as from the choosing of design to
colouring it is in our hands. We are free to create our own idea of design and
produce a colourful output and that is the reason why these three rangolis are
different from each other.

This micro-project also made me to revise and implement many


important algorithms in computer graphics like the Bresenham’s line and circle
algorithm, polygon filling and also the 2D transformations. It increased my
knowledge about these algorithms. Hoping that the readers would also
appreciate these rangoli designs.

25
REFERENCES
 www.google.com
 www.youtube.com
 www.geeksforgeeks.org
 www.tutorialspoint.com
 www.generalnote.com
 www.javatpoint.com
 www.programiz.com
 www.w3schools.in

26

You might also like