New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Interactive Data Visualization with Processing
Illustration by

Modifying variables


From:

Interactive Data Visualization with Processing

with Barton Poulson

Video: Modifying variables

In this movie, I want to talk about how you can work with variables specifically, modify their values over time, which of course is the flexibility that variables give you. What I want to demonstrate is how you can assign values several times to the same variable where the latest one trumps the previous one. And also, how you can change a variable from one type to another, and you can also create code to systematically change variables, as well as constrain some of their flexibility to keep them within a particular range.
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Interactive Data Visualization with Processing
7h 43m Beginner Sep 25, 2012

Viewers: in countries Watching now:

Start communicating ideas and diagramming data in a more interactive way. In this course, author Barton Poulson shows how to read, map, and illustrate data with Processing, an open-source drawing and development environment. On top of a solid introduction to Processing itself, this course investigates methods for obtaining and preparing data, designing for data visualization, and building an interactive experience out of a design. When your visualization is complete, explore the options for sharing your work, whether uploading it to specialized websites, embedding the visualizations in your own web pages, or even creating a desktop or Android app for your work.

Topics include:
  • Exploring the need for creative data visualization
  • Drawing basic lines and shapes
  • Introducing variables, strings, and arrays
  • Modifying drawing attributes such as color
  • Making drawings more dynamic with animation loops and spirals
  • Creating keyboard- and mouse-based interactions
  • Adding images, video, and sound
  • Reading in text or XML data
  • Creating plots and charts
  • Publishing and sharing your work
Subjects:
Developer Programming Languages
Software:
Processing
Author:
Barton Poulson

Modifying variables

In this movie, I want to talk about how you can work with variables specifically, modify their values over time, which of course is the flexibility that variables give you. What I want to demonstrate is how you can assign values several times to the same variable where the latest one trumps the previous one. And also, how you can change a variable from one type to another, and you can also create code to systematically change variables, as well as constrain some of their flexibility to keep them within a particular range.

So what I'm going to do is I'm going to start by titling this sketch and then, I'm going to declare a few variables that we saw in the last video. So I have an integer variable called x, I'm giving it an initial value of 10. We have a floating-point variable, a float variable called y, I'm giving it an initial value of 50 and then, we also have a float variable z.

I'm simply declaring it without initializing it. Next I'm going to create a drawing with two blocks of code. Now, we're going to talk more about the dynamic drawings in a later movie, but for right now, you need to know that I'm going to have one block that's called setup. By the way, the reason this says void at the beginning is because every function, and setup is a function, needs to have a return value type. If it doesn't return a variable, and this one doesn't, then you put void to indicate that there's nothing that comes back.

Also, it has empty parentheses because the function is supposed to have arguments and this indicates that there is a space there, but it doesn't have any. Instead, we're going to put everything about this function within these curly brackets. The first thing, I'm going to do is declare the size of the window. So I'm going to do size 600x200. I'm going to turn on anti-aliasing again to smoothen things out. I'm also going to turn off stroke, so there's no border outline around shapes. Now what I'm going to do is I'm going to assign a value to a variable.

Watch how we do this. Assign a new value to an existing variable. What I'm going to do is I'm going to take y, which previously I've initialized at 50, and I'm going to redefine it to be height. Height is a built-in variable and Processing knows that height is the height of the window, in this case, it's 200 pixels and width is how wide the window is, it would be 600.

So right now, I'm just setting y, changing it from the initial 50 here to be the height of the window. Next, I want to show you something about what's called casting a variable and that is sometimes a variable comes in one particular type and you want to change it to a different type. The most common is taking float variables which are decimal points and converting them to integer variables or int which don't.And so, the way I'm going to do is I'm first going to create a float variable. So it says randomFloat and I'll give it a random number from 0 to 10.

Now, the way the random function works here is it will take any number in-between there, and it uses decimal, so it's a floating point thing. 0, the lower end is inclusive, the upper end 10, is exclusive. So you can get a zero, you can't get a 10. You can get anything in-between. If I want to specify the lower value, if I want it to be like 5, I can simply type it in, 5 to 10 and I'll show you that in a later movie. What I'm going to do right now though is I'm going to do this little print line. Now, if I hit Run, you see in the Console that I have my randomFloat variable, that's good.

So that's down there. Now, let's say that I want to get a random integer from this that I want an actual number from 0 to 10. The way that I would do that is closely related. In fact, I'm just going to copy this text, paste it right here and then, this time I'm going to create, instead of randomFloat, I want a randomInteger variable. Now if I want to have 10 be a choice, the way Processing works, I actually need to go up to 11, because it doesn't include the 11, it goes up to 10.9999 and what the casting is going to do is it's going to chop off the decimal places and leave me with the part in front of that.

So all I need is this. And then what I'm going to do is I'm going to come over here, and tell it's an integer variable. I'm going to get an error message if I run this, see what happens. Because this is a float variable, random has decimal places, but I've declared an int over here, so that's just not going to work. So instead, what I do is I need to cast it into a different type. That's pretty easy. All you need to do in Processing is I put int and put the rest of this in parentheses, and it says take whatever is in there and turn it into an integer variable and it does this by simply chopping off the decimal places.

And so now, when I run this code, you see I now have a random number and it's just a 2. If you want to do something like roll dice, one option is to do it this way. I simply take this down here and I'll say I'm going to get a random die. Now, the difference here is that the dies only go from 1 to 6, whereas the numbers I've been using so far go from 0 to 10.

So I want a different range. What I'm going to do is because I don't want zeros, I want one ones, but I want to have 6 be an option, is if I put the limit at 6, it'll go from 0 to 5.99, it'll chop off the decimal places which may means it goes from 0 to 5. All I have to do then is add 1 at the end of it. And then if I take that and run it like this, you can see now that I have a number from 1 to 6, I can run this a few extra times and you'll see that I'll get different values, there is a 2 for the die, and there is 4.

It will stay within that range. Now I'm going to show you, you can actually get much more fancy in how you deal with your variables here. If we go to the top of the sketch, you will see I declared a variable z, but I didn't initialize it. Right down here, I'm going to get z and I'm just going to show you that things can get rather fancy. So I might say that z = 3*x, I have a value for x earlier, then I'm going to also add the arc tangent of the square root of y, it could come up you don't know and then, I run that variable, run the whole thing and there I have the arc tangent of the square root of y plus three times x, there maybe a situation which you need that, I don't know what it is right now.

The next thing is to show how to increment a variable overtime. What I want to show is in a separate block of code called draw. So I'm going to draw an ellipse and actually, if I want the ellipse to show, I need to refresh the background every time we go through, and then I show the ellipse, and then I'm going to give it these variable values of x, y and then make the ellipse 40 pixels tall and when I do this, it will draw a circle, you see my circle there in the bottom-left, but what I want to do is I want to make it that the circle can move and one way to do that is through incrementing it.

An increment takes a variable and adds a value to it every time it runs through a cycle. Here the ++ simply means add 1 to it. Also, you can do other kinds of increments. So for instance on this one, I'm going to take the value for y, the height, and I'm going to multiply it times a particular time, every time we go through it this one. I'm actually going to do a 0.99 and what this means is that the height is going to actually decrease. It's going to be 99% of its initial value every time. And when I draw that, you see that this now moves.

Now it's going to keep moving until it disappears. If I want to try to keep it in place, all I need to do then is what's called the constrain and I can go x = constrain(x -- so x is referring back to itself. I'm going to constrain x, the lowest value it can have is 0 and the highest it can have is the width of the window divided by 2.

So it'll stop when it gets halfway. I save this and run it, and you see it goes up until it gets halfway and it's just going bonk into it and stop. Anyhow, these are a few different ways of working with variables and modifying them to make it, so they can take on new values and give some flexibility to how you work with your information in your sketch.

There are currently no FAQs about Interactive Data Visualization with Processing.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.