draw a beaver in matlab

We can draw a simple beaver with MATLAB using the patch function. Here's how to draw a beaver step-by-step:

  1. Define the beaver's body and head as two overlapping circles.
theta = linspace(0,2*pi,50);

% Body
body_center = [0,0];
body_radius = 40;
body_x = body_radius*cos(theta) + body_center(1);
body_y = body_radius*sin(theta) + body_center(2);

% Head
head_center = [-30,-10];
head_radius = 25;
head_x = head_radius*cos(theta) + head_center(1);
head_y = head_radius*sin(theta) + head_center(2);
327 chars
14 lines
  1. Define the beaver's tail as a triangle.
% Tail
tail_vertices = [-40,10; ...
                  -70,20; ...
                  -70,0; ...
                  -40,10];
122 chars
6 lines
  1. Define the beaver's ears as two small circles.
% Ears
ear1_center = [-25,25];
ear1_radius = 6;
ear1_x = ear1_radius*cos(theta) + ear1_center(1);
ear1_y = ear1_radius*sin(theta) + ear1_center(2);

ear2_center = [-15,23];
ear2_radius = 6;
ear2_x = ear2_radius*cos(theta) + ear2_center(1);
ear2_y = ear2_radius*sin(theta) + ear2_center(2);
290 chars
11 lines
  1. Combine all the shapes into a single patch object.
% Combine shapes into a single patch object
beaver_vertices = [body_x', body_y'; ...
                   head_x', head_y'; ...
                   tail_vertices; ...
                   ear1_x', ear1_y'; ...
                   ear2_x', ear2_y'];

beaver_faces = [1:length(body_x), ...
                length(body_x)+1:length(body_x)+length(head_x), ...
                length(body_x)+length(head_x)+1:length(body_x)+length(head_x)+size(tail_vertices,1), ...
                length(body_x)+length(head_x)+size(tail_vertices,1)+1:length(beaver_vertices)];

beaver_color = 'brown';

fig_handle = patch('Vertices', beaver_vertices, ...
                   'Faces', beaver_faces, ...
                   'FaceColor', beaver_color, ...
                   'EdgeColor', 'black');
767 chars
19 lines

And there you have it, a simple beaver drawn with MATLAB! Of course, you can customize the shape and color of the beaver as you wish.

related categories

gistlibby LogSnag