Friday, December 14, 2012

Plotting a pentagram with MATLAB.


Figure: As seen after running the code

This is a MATLAB code to plot the symbol of pentagram on a Cartesian coordinate system. The symbol consists of 1 circle and 1 pentagram - a 5-pointed star drawn with 5 straight strokes.

Basically this algorithm focuses on the getting the coordinates to draw:

(1) the circle and
(2) the horizontal line.

Later the horizonzal line is manipulated to generate the other 4 lines to form the pentagram.



Contents

The variables

n = 2^8; % Number of sampling points
t = linspace(0,2*pi,n); % Interval
noSides = 5; % Number of sides of the polygon
innerAngle = 2*pi/noSides; % Interior angle of a pentagon.

% Variables for the cirle
r = 2; % Radius
h = 0; % Transition distance
k = 0; % Transition distance

xsfact = 1.3; % Factor to extend the display range of the xy-axes.

Equations for the circle

x = r*cos(t)+h;
y = r*sin(t)+k;

Equation(s) for the horizontal line

% The length of the horizontal line
lH = r*sin(innerAngle)*2;
% Distance to the horizontal line from the center
dist_lH = r*cos(innerAngle);
% The definition interval of the horizontal line
xH = linspace(-lH/2, lH/2, n);

Rotation

Here the horizontal line from the previous will be simply duplicated for 4 times. Every point (x,y) will be multiplied with a 2D-rotation matrix.

side = zeros(2,n);
side(1,:) = xH;
side(2,:) = -dist_lH;

side1rot = rot2d(innerAngle)*side;
side2rot = rot2d(innerAngle*2)*side;
side3rot = rot2d(innerAngle*3)*side;
side4rot = rot2d(innerAngle*4)*side;

Plotting

figure,
hold on
plot(x,y, 'r')
plot(xH, -dist_lH, 'r')
plot(side1rot(1,:), side1rot(2,:), 'r')
plot(side2rot(1,:), side2rot(2,:), 'r')
plot(side3rot(1,:), side3rot(2,:), 'r')
plot(side4rot(1,:), side4rot(2,:), 'r')
hold off
axis(xsfact.*[min(x) max(x) min(y) max(y)])
title('Das Pentagramm')


1 comment:

tommy.kozak said...
This comment has been removed by the author.