1 / 91

Lesson 5 – Putting It All Together

Explore the use of mathematics in Processing, including the concept of modulus, generating random numbers, and calculating probabilities.

short
Download Presentation

Lesson 5 – Putting It All Together

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ALGORITMA PEMROGRAMAN 2 PROCESSING Lesson 5 – Putting It All Together

  2. Chapter • 13 – Mathematics • 14 – Translation and Rotation (in 3D!)

  3. Chapter 13 - Mathematics

  4. In This Chapter • Probability • Perlin Noise • Trigonometry • Recursion

  5. 13.1 Mathematics and Programming • This chapter aims to take a relxed and friendly approach to a few useful topics mathematics that will help us aling the journey of developing Processing sketches. • For example : x = x + 1;

  6. 13.2 Modulus • Modulus is a very simple concept (one that you learned without reffering to it by name when you first studied division) that is incridebly useful for keeping a number within a certain boundary ( a shape on the screen, an index value within the range of an array, etc).

  7. 13.2 Modulus • The modulo operator calculates the remainder when one number is divided by enother. • It works with both integers and floats.

  8. 13.2 Modulus • Example : • 20 divided by 6 equals 3 reminder 2. (in other words: 6*3 + 2 = 18 + 2 = 20) • Therefore • 20 module 6 equals 2 or 20% 6 = 2

  9. 13.2 Modulus • Here are a few more, with some blanks for you to fill in.

  10. 13.2 Modulus • You will notice that if A = B % C, A can never be larger than C. • The remainder can never be larger than the divisor.

  11. 13.2 Modulus • Therefore, modulo can be used whenever you need to cycle a counter variable back to zero. • The following lines of code : can be replaced by : x = x + 1; if (x >= limit){ x = 0; } x = (x + 1) % limit

  12. 13.2 Modulus • Example 13-1: Modulo

  13. 13.2 Modulus • Example 13-1: Modulo

  14. 13.2 Modulus • Output Example 13-1: Modulo

  15. 13.3 Random Numbers • Processing random number generator produces what is known as a “uniform” distribution of numbers. • For example: • If we ask for a random number between 0 and 9, 0 will come up 10% of the time, 1 will come up 10% of the time, 2 will come up 10% of the time, and so on.

  16. 13.3 Random Numbers • Pseudo-Random Numbers • The randon number we get from random() function are not truly random and are known as “pseudo-random”. • They are the result of a mathematical function that simulates randomness. • This function would yield a pattern over time, but that time period is so long that for us, it is just as good as pure randomness!

  17. 13.3 Random Numbers • Example 13-2: Random number distribution

  18. 13.3 Random Numbers • Example 13-2: Random number distribution

  19. 13.3 Random Numbers • Example 13-2: Random number distribution

  20. 13.4 Probability Review • Given a system with a certain number of possible outcomes, the probability of any given event occurring is the number of outcomes which qualify as event divided by total number of possible outcomes.

  21. 13.4 Probability Review • The simples example is a coin toss. • There are a total of two possible outcomes (heads or tails). • There is only one way to flip heads, therefore the probability heads is one divided by two, that is, ½ or 50%.

  22. 13.4 Probability Review • Consider a deck of 52 cards. The probability of drawing an ace from that deck is: • Number of aces/number of cards = 4/52 = 0.077 = ~8% • The probability of drawing a diamond is : • Number of diamonds/number of cards = 13/52 = 0.25 = 25%

  23. 13.4 Probability Review • We can also calculate the propability of multiple events occuring in sequence as the product of the individual probabilities of each event. • The probability of a coin flipping up heads three times in a row is: (1/2)*(1/2)*(1/2) = 1/8 (or 0.125)

  24. 13.4 Probability Review • We can also calculate the propability of multiple events occuring in sequence as the product of the individual probabilities of each event. • The probability of a coin flipping up heads three times in a row is: (1/2)*(1/2)*(1/2) = 1/8 (or 0.125)

  25. 13.4 Probability Review • Exercise 13-1: What is the probability of drawing two aces in a row from the deck of cards?

  26. 13.5 Event Probability in Code • There are few different techniques for using the random() function with probability in code. • For example, if we fill an array with a selection of numbers (some repeated), we can randomly pick from that array generate events based on what we select.

  27. 13.5 Event Probability in Code • Example :

  28. 13.5 Event Probability in Code • This same technique can also be applied to multiple outcomes. • Outcome A – 60% | Outcome B – 10%| Outcome C – 30% • To implement this in code, we pick one random float and check where it falls. • Between 0.00 and 0.60 (60%) • Between 0.60 and 0.70 (10%) • Between 0.70 and 1.00 (30%)

  29. 13.5 Event Probability in Code • Example 13-3 draws a circle with a three different colors, each with the above probability (red: 60%, green: 10%, blue: 30%).

  30. 13.5 Event Probability in Code • Example 13-3 : Probabilities

  31. 13.5 Event Probability in Code • Example 13-3 : Probabilities

  32. 13.5 Event Probability in Code • Example 13-3 : Probabilities

  33. 13.5 Event Probability in Code • Example 13-3 : Probabilities

  34. 13.6 Perlin Noise • One of the qualities of a good random number generator is that the numbers produced appear to have no relationship. • If they exhibit no discernible pattern, they are considered random.

  35. 13.6 Perlin Noise • In programming behaviours that have an organic, almost lifelife quality, a little bit of randomness is a good thing. • This is the approach taken by Ken Perlin, who developen a function in the eraly 1980’s entitle “Perlin Noise” that produces a naturally ordered (i.e, “smooth”) sequence of pseudo-random numbers.

  36. 13.6 Perlin Noise • It was originally designed to create procedural textures, for which Ken Perlin won an Academy Award for Technical Achievement. • Perlin noise can be used to generate a variety of interesting effects inclucing clouds, landscapes, marble textures, and so on.

  37. 13.6 Perlin Noise • Figure 13.3 shows two graphs, -a graph of Perlin noise over time (the x-axis represents time; note how the curve is smooth) compared to a graph of pure random numbers over time.

  38. 13.6 Perlin Noise • Processing has a built-in implementation of the Perlin noise algorithm with the function noise( ) . • Thenoise() function takes one, two, or three arguments (referring to the “ space ” in which noise is computed:one, two, or three dimensions). • One-dimensional Perlin noise produces as a linear sequence of values over time. For Example :

  39. 13.6 Perlin Noise • Example 13-4: Perlin Noise

  40. 13.6 Perlin Noise • Example 13-4: Perlin Noise

  41. 13.6 Perlin Noise • Example 13-4: Perlin Noise

  42. 13.8 Trigonometri • Sohcahtoa. Strangly enough, this seemingly nonsense word, Sohcahtoa, is the foundation for a lot of computer graphics work. • Any time you need to calculate an angle, determine the distance between points, deal with circles, arcs, lines, and so on, you will find that a basic understanding of trigonometry is essential.

  43. 13.8 Trigonometri • Trigonometry is the study of the relationships between the sides and angles of triangles and Sohcahtoa is a mnemonic device for remembering the definitions of the trigonometric functions, sine, cosine, and tangent. • See figure 13.8 :

  44. 13.8 Trigonometri • Soh : sine = opposite/hypotenuse • Cah : cosine = adjacent/hypotenuse • Toa : tangent = opposite/adjacent

  45. 13.8 Trigonometri

  46. 13.8 Trigonometri • Example 13-5 : Polar to Cartesian

  47. 13.8 Trigonometri • Example 13-5 : Polar to Cartesian

  48. 13.8 Trigonometri • Example 13-5 : Polar to Cartesian

  49. 13.8 Trigonometri • Example 13-5 : Polar to Cartesian

  50. 13.10 Recursion

More Related