Change language

Draw Indian flag using Matlab

Approach to drawing the Indian flag

  • First, we need to create a 300X600X3 matrix and fill it with white. 300 — the number of lines, and 600 — number of columns. Color image requires 3 channels, so 3 represents the RGB (red, green, blue) channel.
     img = uint8 (zeros (300, 600, 3)) 
  • Divide the matrix by three pieces (0-100) rows for saffron color, (101-200) rows for white and Ashoka chakra, (201-300) rows for green.
  • Fill in the matrix with saffron color (255, 153 , 51) from the 1st row to the 100th row.
     img (1: 100, 1: 600, 1) = 255; img (1: 100, 1: 600, 2) = 153; img (1: 100; 1: 600, 3) = 51; 
  • Fill the matrix with green (19, 136, 8) from line 201 to 300.
     img (201: 300, 1: 600, 1) = 19; img (201: 300, 1: 600, 2) = 136; img (201: 300, 1: 600, 3) = 8; 
  • To make the Ashoka chakra, first we need to understand the equation of distance between two given coordinates.
     (distance)  2  = (x2-x1)  2  + (y2-y1)  2  

    (x1, y1) and (x2, y2) — two given coordinates.

  • Using the above equation, we can make a circle of the ashok chakra. The coordinates of the center of the matrix and the circle (150, 300). To draw a circle, the inner radius is 40 and the outer radius is 45.
  • for i = 1 : 300

    for j = 1 : 600

    if ( sqrt (power ( 150 - i, 2 ) + power ( 150 - j , 2 ))" = 40 & amp; & amp; 

    sqrt (power ( 150 - i, 2 ) + power ( 150 - j, 2 )) " = 45 )

    img (i, j, 1 ) = 0

    img (i, j, 2 ) = 0

    img (i, j, 3 ) = 128

    end

    end

      end

  • The angle between two adjacent spokes of the ashoka chakra is (360 / n) = 15 o , where n (number of spokes) = 24. atand — this is a MATLAB function used to determine the angle.
  • for i = 110 : 190

    for j = 260 : 340

    dist = ( sqrt (power (i - 150 , 2 ) + power (j - 300 , 2 ))); 

    k = round (atand (( 300 - j) / ( 150 - i))); 

    if dist " = 40 & amp; & amp; mod (k, 15 ) = = 0

    img (i, j, 1 : 2 ) = 0

    end

    end

    end

The implementation is shown below:

% MATLAB code for drawing the Indian flag

 
% initialization of the zero matrix 300X600X3
flag = uint8 (zeros (300, 600, 3)); 
flag (:,:,:) = 255; 
% Saffron
flag (1: 100,:, 1) = 255; 
flag (1: 100,:, 2) = 153; 
flag (1: 100,:, 3) = 51; 

 
% Green
flag (200: 300,:, 1) = 19; 
flag (200: 300,:, 2) = 136; 
flag (200: 300,:, 3) = 8; 

 
% Ashok chakra

for i = 1: 300

for j = 1: 600

if sqrt (power (i-150, 2) + power (j-300, 2))" = 40

if sqrt (power (i-150, 2) + power (j-300, 2)) "= 45

flag (i, j, 1: 2) = 0; 

end

end

  end  
end

for i = 110: 190

for j = 260: 340

dist = ( sqrt (power (i-150, 2) + power (j-300, 2))); 

k = round (atand ((300-j) / (150-i)) ); 

if dist "= 40 & amp ; & amp; mod (k, 15) == 0

flag (i, j, 1: 2 ) = 0; 

end

end

end
% display the matrix as an image
figure, imshow (flag); 

Output:

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically