How to Make a Digital Valentine's Day Card

How to Make a Digital Valentine's Day Card

Description

Valentine's Day is tomorrow. Here's how to make a digital card to capture the heart of your sweetie. Well, OK, that might be overstating things a bit, but Harley does show an algorithmic way using trigonometry to generate a beating heart animation in an easy to use programming environment called Processing. This was inspired by a video LeiosOS posted last week.

The original inspiration: LeiosOS: Drawing a heart
Wolfram algorithm site: Heart shaped graphs

For a written transcript, go to How to Make A Digital Valentine's Day Card

Intro/Exit: "Hot Swing" by Kevin MacLeod at http://incompetech.com
Incidental: “Carefree” by Kevin MacLeod at http://incompetech.com

livingroom_light_switch by AlienXXX at http://freesound.com

Transcript

Today at the House of Hacks we're going to make a high-tech Valentine's Day greeting.

[Music]

Hi Makers, Builders and Do-it-yourselfers. Harley here.

I recently ran across a new-to-me programming channel who's name I won't even begin to try to pronounce but I will link to in the cards and description below.

He did a video about drawing a heart where he used a bit of C code and his own graphics library.

I want to take his idea, expand a bit on why it works and show how to use the Processing programming environment to do the same thing.

First let's look at generating shapes...

Many people get overwhelmed by the thought of trigonometry with its sine and cosine, angles and other related things, but at its core, it’s really pretty simple.

Remember the old cartesian graph from math class?

It had an x-axis and a y-axis and points could be located on this graph using just two coordinates.

On this graph, let’s draw a circle with its center at the origin with a radius of one.

Now, let’s imagine a line starting at the origin and going through the circle.

We can draw this line at any angle from the x axis.

If it is on the axis it will be 0 degrees.

Regardless of where we draw it, it’s going to intersect the circle at only one place.

That one place will have an x and y coordinate that, by definition, is the value of the cosine and sine functions at that angle.

So, for 0 degrees, the place where the circle intersects the x-axis, cosine will be 1 and sine will be 0.

As the angle increases, the x value decreases and the y value increases until we get to 90 degrees.

This lies on the y axis where cosine is 0 and sine is 1.

As the angle continues to increase, the x and y values continue to change, always between 0 and 1, always on the circle.

And that’s the fundamental theory behind trigonometry, everything else is derived from this.

Now let’s plot these points a bit differently.

On the x axis, let’s plot the angle and on the y axis, let’s plot of value of sine remembering that sine is the y value on our original circle.

At 0 degrees, sine is 0 so we start at the origin.

As the angle increases, sine increases until we get to 90 degrees and reach 1, then it starts decreasing as we move into the second quadrant of the circle.

At 180 degrees sine is back to 0.

As the angle continues to increase, the y coordinate of our circle drops into the negative values in the third quadrant until it reaches -1 at 270 degrees.

Finally in the fourth quadrant, we see y coming back up to 0.

After this, the cycle simple repeats itself as higher angles are duplicates of the previous angles.

Now, doing the same thing with cosine, at 0 degrees, cosine is 1 since it represents the x value on the circle.

As the angle increases, x decreases to 0 at 90 degrees, -1 at 180, back up to 0 at 270 and finally 1 at 360 degrees, the place we started.

These functions are great for describing a lot of things that are periodic in nature, things that fluctuate between two values.

If we multiply the results of sine or cosine we can make these graphs fluctuate by a different amount.

For example, here’s the result of multiplying the sine by 2.

And if we multiply the angle by a value, the frequency of the fluctuation will increase or decrease.

Here’s what it looks like if we multiply the angle by two.

Things can get really wild if we start adding these two curves together.

Or subtracting them.

Or multiplying them.

Or doing anything else really.

Sines and Cosines are the basis for a lot of things in our modern world.

Today we’re going to see how they can be used to send a message to our sweetheart.

Processing is a programming environment that was developed about 15 years ago and designed to teach the basics of programming within a visual context.

Since it was designed with a visual context in mind, it’s great for experimenting with graphics and drawing things.

The Processing.org website has free installers for Windows, Linux and Macs.

This gives us a a Processing environment that, once started, we can start writing programs in.

Let's look at a simple program to draw a circle.

We first define the size of our canvas, the color of our background and the color we want to draw in.

Then we’re going to define a variable to scale our circle by and an offset for the x and y coordinates.

These are needed because sine and cosine return values around the cartesian origin but the coordinate system of the Processing screen is based with 0,0 in the upper left corner of the canvas.

Then we’re going to define a loop from 0 to twice PI, going in small increments.

We used twice PI here because in computers cosine and sine typically use what are called radians instead of degrees as their inputs.

This is just fine though because there’s a one to one mapping between degrees and radians where PI is 180 degrees and two PI is 360 degrees.

Inside the loop we set x to the cosine value of the loop variable and y to the sine value of the loop variable.

Then we’re simply going to plot that point, offset and scaled by the previously defined values.

Let's save our work at this point.

When we press the arrow button in the top tool bar, our program runs and we get this nice circle.

Now that we have the basics of creating a circle, how’s this help us with our Valentine Day project?

A web search for “heart drawing formula” leads us to the Wolfram site that has a number of different formulas for drawing a heart shape.

The sixth one in particular lends itself well to the program we have for drawing a circle.

All we have to do is change the assignments to x and y to get something that looks like a heart.

One of the cool things about the Processing environment is it works well for animating things.

If we put the statements before the loop in a function called setup they will get called one time when we run our program.

Then if we put the loop inside a method called draw, it will get continually called over and over again.

But because we’re drawing the same thing over and over, it's not going to be too exciting.

So before running this, let's add a line to change the scaling each time the draw method is called and adjust a control parameter to keep the scale between a couple values.

Finally, move the background call from setup into draw so it happens every time draw is called. This will clear our canvas each time.

Now let’s run this.

Ah, a nice beating heart.

This is cool and all, but it’s hard to show our special someone.

By adding a line to the end of our draw method, we can tell Processing to save our drawing to a file.

Now let's run this for a little bit, stop it and go look at where we saved our program.

We can see a bunch of individual snapshots of each frame of our animation.

In one last step, we can go to Processing’s Tools menu and select Movie Maker.

It gives us a dialog where we can select the directory containing the shots of our animations and press the Create Movie button.

This compiles our individual images into a MOV file that we can e-mail to our intended.

[Music]

If this is your first time here at House of Hacks: Welcome, I’m glad you’re here and would love to have you subscribe.

I believe everyone has a God-given creative spark.

Sometimes this manifests through making things with a mechanical and technical bent.

Through this channel I hope to inspire, educate and encourage these types of makers in their creative endeavors.

Usually this involves various physical media like wood, metal, electronics, photography but sometimes other things, like programming.

If this sounds interesting to you, go ahead and subscribe and I’ll see you again in the next video.

Now, go make something. Perfection’s not required. Fun is!