Question

In: Mechanical Engineering

Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im...

Draw a Koch Snowflake fractal image using MATLAB programming.

I'm pretty new to MATLAB and im not too familar with how all the plotting and drawing functions work.

Solutions

Expert Solution

Matlab CODE:

%KOCH: Plots 'Koch Curve' Fractal
% koch(n) plots the 'Koch Curve' Fractal after n iterations
% (be patient for n>8, depending on Computer speed)
%--------------------------------------------------------------------
function []=Koch(n)
if (n==0)
   x=[0;1];
   y=[0;0];
   line(x,y,'Color','r');
   axis equal
   set(gca,'Visible','off')
else
   levelcontrol=10^n;
   L=levelcontrol/(3^n);
   l=ceil(L);
   kline(0,0,levelcontrol,0,l);
   axis equal
   set(gca,'Visible','off')
   set(gcf,'Name','Koch Curve')
end
%--------------------------------------------------------------------
function kline(x1,y1,x5,y5,limit)
length=sqrt((x5-x1)^2+(y5-y1)^2);
if(length>limit)
   x2=(2*x1+x5)/3;
   y2=(2*y1+y5)/3;
   x3=(x1+x5)/2-(y5-y1)/(2.0*sqrt(3.0));
   y3=(y1+y5)/2+(x5-x1)/(2.0*sqrt(3.0));
   x4=(2*x5+x1)/3;
   y4=(2*y5+y1)/3;
   % recursive calls
   kline(x1,y1,x2,y2,limit);
   kline(x2,y2,x3,y3,limit);
   kline(x3,y3,x4,y4,limit);
   kline(x4,y4,x5,y5,limit);
else
   plotline(x1,y1,x5,y5);
end
%--------------------------------------------------------------------
function plotline(a1,b1,a2,b2)
x=[a1;a2];
y=[b1;b2];
line(x,y);
%--------------------------------------------------------------------


Result:

Command: Koch(3)

if you want full curve, use the below code:

clc, clearvars, close all;

length = 1; % Original side length of triangle
theta = 60*pi/180; % Angle of the equilateral triangle in radians

% Points of the original equilateral triangle
p1 = [0,0];
p2 = [1/2*length,length*sin(theta)];
p3 = [length,0];

flake_points = [p1; p2; p3]; % Vertices of the snowflake
nn= input('Enter number of iterations needed : ');
for iteration = 1:nn
    n_points = size(flake_points,1); % No. points currently in data
    new_triangle_points = nan(n_points*3,2); % New points to be added
    for i = 1:n_points
        p = flake_points(i,:);
        if i == n_points
            vector = flake_points(1,:)-flake_points(i,:);
        else
            % Vector between next point and current point
            vector = flake_points(i+1,:)-flake_points(i,:);
        end
        b1 = p + 1/3*vector; % Base point 1 of the new triangle
        b2 = p + 2/3*vector; % Base point 2 of the new triangle
        top = p + 1/2*vector + perp(vector)*length/3*sin(theta);
        new_triangle_points(3*i-2:3*i,:) = [b1; top; b2];
    end
    flake_points(end+1:end+9,:) = nan(9,2); % Add rows to data
    new_flake_points = nan(n_points*3+n_points,2);
    n = 1;
    for i = 1:n_points
        new_flake_points(n,:) = flake_points(i,:);
        index = 3*i-2;
        new_flake_points(n+1:n+3,:) = new_triangle_points(index:index+2,:);
        n = n + 4; % Three new points have been inserted,
                    % so we now skip to the next empty spaces
    end
    flake_points = new_flake_points;
    length = 1/3*length; % Revise length
end

plot(flake_points(:,1),flake_points(:,2),'w')
hold on; % Plot last data point with 1st (ie, close the loop)
plot([flake_points(1,1),flake_points(end,1)],...
    [flake_points(1,2),flake_points(end,2)],'w')
set(gca,'color',[0.1 0.1 0.1])
axis equal;
str = sprintf('Iteration: %d',iteration);
title(str, 'FontSize', 20);
%------------------------------------------------------------------


Save this function in your Matlab location,

function p_vec = perp(vec)
% Returns the unit vector p_vec, which is perpendicular to the input
% 2D vector vec
p_vec(1) = -vec(2);
p_vec(2) = vec(1);

% Convert to unit vector
p_vec = p_vec / norm(p_vec);

end

________________________________________

Result:


Related Solutions

I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
Using Matlab 1. Create a color image of size 200 × 200. The image should have...
Using Matlab 1. Create a color image of size 200 × 200. The image should have a blue background. 2. Create 100 yellow regions within the image that have a variable size. More specifically, their width should be an odd number and vary between 3 and 7 and their height should also be an odd number and vary between 3 and 7. For example, you may get rectangular regions of size 3 × 3, 3 × 5, 5 × 3,...
MATLAB code: using Laplacian mask enhance for image "ngc6543.jpg".
MATLAB code: using Laplacian mask enhance for image "ngc6543.jpg".
hi so i'm new to c++ i understand the language pretty good however no experience whatsoever...
hi so i'm new to c++ i understand the language pretty good however no experience whatsoever with visual studio here is the assigment Examine, understand, compile and run the FruitJuice Program (all files) and submit it (a copy is made available on LMS) i know what the program does and i get what is doing but i don't even know if i'm opening the thing the right way from visual studio it wont see it as a project at all....
[15 marks] Draw the flowchart of the following programming problem: You can draw the flowchart using...
[15 marks] Draw the flowchart of the following programming problem: You can draw the flowchart using a drawing tool, or draw the flowchart on a piece of paper, take a picture, insert it here or save it in the submission folder The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up...
MATLAB CODE FOR E xtreme learning machine using for classification task. image processing electrical. if you...
MATLAB CODE FOR E xtreme learning machine using for classification task. image processing electrical. if you know then try or leave for other
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
matlab What does the following: Draw two figures (or two pictures) side by side using the...
matlab What does the following: Draw two figures (or two pictures) side by side using the rules of drawing (names for axes, title for drawing etc ...) both in the same frame The first figure shows the exponential function (solid red line) and the natural logarithmic function (black dashed line) on the space [0,10]. The second figure shows the sine (black dotted line) and the cosine function (connected in blue and on it) on the space [0.15].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT