# Start with some planets...

Consider a circle. In 2D, you can plot a circle by setting **x1=cosine(angle)** and **y1=sine(angle)**, as you let angle go from 0 to 360 degrees. Let's say this is the orbit of a planet.

Now let's say there's a second planet rotating around the same sun, but it's closer and its orbital time is much faster. We can represent this by saying **x2= 0.9*cosine(6*angle) **and **y2 = 0.9*sine(6*angle)**, as angle gooes from 0 to 360 degrees.

Here's a video of the two 'planets' rotating past each other.

This is all well and good, but where does the art come in? Well, let's say we draw a line between the two orbiting planets for each angle increment (say, 0.5 degrees). With this simple program, we can generate an image that looks like this!

**Code**

Here's the code used to generate the preceding figure:

`% Create a 1000x1000 figure`

`fig1=figure('Position',[10,10,1000,1000]); `

`% Specify the basic plot parameters`

plot(0,0);

xlim([-1,1]);

ylim([-1,1]);

axis off

hold on

`% Draw the image`

for i=0.5:.5:360 % begin the for loop

`% For cyclical functions, you'll want to iterate across a full circle, which is 360 degrees. `

% For these four lines, choose any trigonometric functions you like

x1=sind(i);

y1=cosd(i);

x2=.9*sind(6*i);

y2=.9*cosd(6*i);

% Plot each line of the drawing

p=plot([x1,x2],[y1,y2],'Color',[0 0 0]);

% You can replace [0 0 0] with any RGB color (values from 0 to 1)

p.Color(4)=0.2; % this makes the drawing semi-transparent

pause(0.001) % pause after drawing each line

end

## A note on variation

Now let's take this one step further- away from reality and into the realm of geometric art. We can set x and y to be anything! Who says we need to have a circle? Try playing around with the parameters and seeing what kind of art you can create!

Remember, in Matlab, the commands for the trigonometric functions are as follows (for angles, not radians):

- Sine : sind()
- Cosine : cosd()
- Tangent : tand()
- Arcsine : asind()
- Arccosine : acosd()
- Arctangent : atand()

## Examples

# Adding more variation!

Now let's return to our original example and add a third planet to the mix! And let's add some color- why not? Now the code gives you:

## Code

`% Create a 1000x1000 figure`

`fig1=figure('Position',[10,10,1000,1000]); `

`% Specify the basic plot parameters`

plot(0,0);

xlim([-1,1]);

ylim([-1,1]);

axis off

hold on

`% Draw the image`

for i=0.5:.5:360 % For cyclical functions, you'll want to iterate across a

% full circle, which is 360 degrees.

% For these four lines, choose any trigonometric functions you like

xt=sind(i);

yt=cosd(i);

xt2=.9*sind(3*i);

yt2=.9*cosd(3*i);

xt3=.7*sind(5*i);

yt3=.7*cosd(5*i);

%p01=plot(sind(0:.5:360),cosd(0:0.5:360),'b-');

%p02=plot(.7*sind((0:.5:360)*2),.7*cosd((0:0.5:360)*2),'r-');

`% Plot each line of the drawing`

p=plot([xt,xt2],[yt,yt2],'Color',[0 0 1]);

p2=plot([xt2,xt3],[yt2,yt3],'Color',[1 0 0]);

p.Color(4)=0.2; % this makes the drawing semi-transparent

p2.Color(4)=0.2;

pause(0.001) % pause after drawing each line

end

## Examples

Now let's add variation to this as well! Here are some examples:

# Making a video!

So, the pause function is nice- it allows you to see your design develop. But how do you capture that process in a video format? Turns out, it's pretty easy in Matlab! Here's the above code, with the video commands added in blue.

Basically, it works like this. You create something called a writer object (**writerObj**) that is going to make your video. You tell it things like what to name your video, what the framerate should be, and what the quality should be. Then you open this object. Every time you want to add a frame to your video (so, every loop iteration), you 'capture' the current state of the figure using the **getframe **command. You then write that frame onto the writer object. Finally, when you're done you close the writer object. (This is an important step; your video won't play if you don't close the writer object.)

`writerObj = VideoWriter('3planetsDemo.avi'); % Name it.`

writerObj.FrameRate = 100;

writerObj.Quality=100;

open(writerObj)

% Create a 1000x1000 figure

fig1=figure('Position',[10,10,1000,1000]);

`% Specify the basic plot parameters`

plot(0,0);

xlim([-1,1]);

ylim([-1,1]);

axis off

hold on

`% Draw the image`

for i=0.5:.5:360 % For cyclical functions, you'll want to iterate across a

% full circle, which is 360 degrees.

% For these four lines, choose any trigonometric functions you like

xt=sind(i);

yt=cosd(i);

xt2=.9*sind(3*i);

yt2=.9*cosd(3*i);

xt3=.7*sind(5*i);

yt3=.7*cosd(5*i);

%p01=plot(sind(0:.5:360),cosd(0:0.5:360),'b-');

%p02=plot(.7*sind((0:.5:360)*2),.7*cosd((0:0.5:360)*2),'r-');

` % Plot each line of the drawing`

p=plot([xt,xt2],[yt,yt2],'Color',[0 0 1]);

p2=plot([xt2,xt3],[yt2,yt3],'Color',[1 0 0]);

p.Color(4)=0.2; % this makes the drawing semi-transparent

p2.Color(4)=0.2;

pause(0.001) % pause after drawing each line

frame = getframe(1);

writeVideo(writerObj, frame)

end

`close(writerObj)`