• Tidak ada hasil yang ditemukan

Lesson 5 – Putting It All Together

N/A
N/A
Protected

Academic year: 2018

Membagikan "Lesson 5 – Putting It All Together"

Copied!
91
0
0

Teks penuh

(1)

Lesson 5 – Putting It All Together

(2)

Chapter

 13 – Mathematics

(3)

Chapter

13 -

(4)

In This Chapter

 Probability

 Perlin Noise

 Trigonometry

(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 :

(6)

13.2 Modulus

Modulus is a very simple concept (one

(7)

13.2 Modulus

The modulo operator calculates the

remainder when one number is divided by enother.

(8)

13.2 Modulus

Example :

20 divided by 6 equals 3 reminder 2.

(in other words: 6*3 + 2 = 18 + 2 = 20)

Therefore

(9)

13.2 Modulus

Here are a few more, with some

(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

(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;

if (x >=

limit){

x = 0;

}

x = (x + 1) %

limit

(12)

13.2 Modulus

(13)

13.2 Modulus

(14)

13.2 Modulus

(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

(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.

(17)

13.3 Random Numbers

Example 13-2: Random number

(18)

13.3 Random Numbers

Example 13-2: Random number

(19)

13.3 Random Numbers

Example 13-2: Random number

(20)

13.4 Probability Review

 Given a system with a certain number of

(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 fip heads,

(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

(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 fipping up

heads three times in a row is:

(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 fipping up

heads three times in a row is:

(25)

13.4 Probability Review

Exercise 13-1: What is the probability

(26)

13.5 Event Probability in Code

 There are few diferent techniques for

using the random() function with probability in code.

 For example, if we fll an array with a

(27)

13.5 Event Probability in Code

(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 foat and check where it falls.  Between 0.00 and 0.60 (60%)

Between 0.60 and 0.70 (10%)

(29)

13.5 Event Probability in Code

 Example 13-3 draws a circle with a three

(30)

13.5 Event Probability in Code

(31)

13.5 Event Probability in Code

(32)

13.5 Event Probability in Code

(33)

13.5 Event Probability in Code

(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,

(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,

(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

(37)

13.6 Perlin Noise

 Figure 13.3 shows two graphs, -a graph

(38)

13.6 Perlin Noise

 Processing has a built-in implementation of

the Perlin noise algorithm with the function noise( ) .

The noise() 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

(39)

13.6 Perlin Noise

(40)

13.6 Perlin Noise

(41)

13.6 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,

(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 defnitions of the trigonometric functions, sine, cosine, and tangent.

(44)

13.8 Trigonometri

Soh : sine = opposite/hypotenuse

Cah : cosine = adjacent/hypotenuse

(45)
(46)

13.8 Trigonometri

(47)

13.8 Trigonometri

(48)

13.8 Trigonometri

(49)

13.8 Trigonometri

(50)
(51)

13.10 Recursion

 In 1975, Benoit Mandelbrot coined the term

fractal to describe self-similar shapes found in nature.

 Much of the stuf we encounter in our physical

(52)

13.10 Recursion

 Functions that call themselves are recursive and are appropriate for solving diferent types of problems.

 This occurs in mathematical calculations;

the most common example of this is “ factorial. ”

(53)
(54)

13.10 Recursion

(55)

13.10 Recursion

(56)

13.10 Recursion

(57)

13.10 Recursion

With a teeny bit more code, we

could add a circle above and below.

(58)
(59)
(60)

Chapter

(61)

Objectives

 In this chapter:

 2D and 3D translation

 Using P3D and OpenGL

 Vertex shapes

 2D and 3D rotation

 Saving the transformation state in the

(62)

14.1 The Z-Axis

 In three-dimensional space (such as the

actual, real-world space where you are reading this book), a third axis (commonly referred to as the Z-axis) refers to depth of any given point.

 In Processing sketch windos, a

(63)

14.1 The Z-Axis

 Scratching your head is a perfectly

reasonable response here. (after all, a computer window is only two dimensional)

 In this chapter, we will exemanie how

(64)

14.1 The Z-Axis

Example 14-1: A growing rectangle,

(65)

14.1 The Z-Axis

Example 14-1: A growing rectangle,

(66)

14.1 The Z-Axis

 Is this rectangle fying of the computer

screen about to bump into your nose?

 Technically, this is of course not the

case.

 It is simboly a rectangle growing in size.

(67)

14.1 The Z-Axis

 If we choose to use 3D coordinates, Processing will create will create the illusion for us.

 While the idea of a third dimension on a

fat computer monitor may seem imaginary, it is quite real for Processing

 Processing knows about perspective, and

(68)

14.1 The Z-Axis

 In order to specify points in three

dimensions, the coordinates are specifed in the order you would expect: x, y, z.

 Cartesian 3D systems can be described

as “left-handed” or “right-handed”.

 If you use your right hand to point your

(69)

14.1 The Z-Axis

 In Processing, the system is left-handed,

(70)

14.1 The Z-Axis

 Our frst goal is to rewrite Example 14-1

using 3D capabilities of Processing.

 Assume the following variables:

Int x = width/2;

Int y = height/2;

Int z = 0;

(71)

14.1 The Z-Axis

 In order to specify the location for a

rectangle, the rect() function takes four arguments: an x location, a y location, a width, and a height.

(72)

14.1 The Z-Axis

 Our frst instict might be to add another

(73)

14.1 The Z-Axis

 The Processing reference page for rect(), however, does not allow for this possibility.

 In order to specify 3D coordinates for shape in the Processing world, we must learn to use a new function, called translate().

(74)

14.1 The Z-Axis

 The function translate() moves the origin point-(0,0)- relative to its previous state.

 We know that when a sketch frst starts, the origin point lives on the top left of the window.

(75)

14.1 The Z-Axis

Where is the origin?

 The “origin” in a Processing sketch is the

poin (0,0) in two dimensions or (0,0,0) in three dimensions.

 It is always at the top left corner of the

windows unless you move it using

(76)

14.1 The Z-Axis

 You can think of it as moving a pen around the screen, where the pen indicates the origin point.

 In addition, the origin always resets itself back to the top left corner at the beginning of draw().

 Any calls to translate() only apply to the current cycle through the draw() loop.

(77)

14.1 The Z-Axis

(78)

14.1 The Z-Axis

(79)

14.1 The Z-Axis

(80)

14.1 The Z-Axis

(81)

14.1 The Z-Axis

 Now that we understand how

translate() words, we can return to the original problem of specifying 3D coordinates.

Translate(), unlike rect(), ellipse()

(82)

14.1 The Z-Axis

 The above code translates 50 units along the

Z-axis, and then draws rectangle at (100,100).

(83)

14.1 The Z-Axis

 Finally, we can use a variable for the Z

(84)

14.1 The Z-Axis

Example 14-3: A rectangle moving

(85)

14.1 The Z-Axis

Example 14-3: A rectangle moving

(86)

14.1 The Z-Axis

Example 14-3: A rectangle moving

(87)

14.1 The Z-Axis

Exercise 14-1: Fill in the appropriate

translate() function to create this pattern. Once you are fnished, try adding a third argument to translate()

(88)

14.1 The Z-Axis

 The translate() function is particularly

useful when you are drawing a collection of shapes relative to a given centerpoint.

 Harking back to a Zoog from the frst 10

(89)

14.1 The Z-Axis

 The display() function above draws all

Zoog’s parts (body and head, etc) relative to Zoog’s x,y location.

 It requires that x and y be used in both

(90)
(91)

Referensi

Dokumen terkait