Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 07 Fall 2010.

Similar presentations


Presentation on theme: "Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 07 Fall 2010."— Presentation transcript:

1 Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 07 Fall 2010

2 Checking the Value We very often want to a selection of things depending on what the value of a variable is. In daily life: - if a person looks less than 25 years old then check their ID. (age < 25) -If the light is red then stop; if green then go. light=red light=green If it’s 9 AM then go to class, otherwise maybe another coffee. (time < 9)

3 Test Variable A common element in these questions is the use of the word ‘IF’. This is the word we use in most programming languages for the purpose of checking a value. There is very little that a program can do without and ‘IF’ testing ability. What does it look like in GameMaker? It’s a ‘test variable’ box.

4 Test Variable Asks for the variable name. Asks for a value to compare against Asks for the nature of the comparison: equal to, less than, greater than

5 Test Variable The Test Variable box is a part of the Control menu.

6 Let’s fix Atom Let the electrons move a bit before appearing somewhere else. Keep the electrons away from the nucleus. Perhaps the particles in the nucleus should move just a bit. Give the impression of reality.

7 Atom Random(100) gives a number between 0-99. Test is ‘random>30’ (=1/3 times) If true, then do the next statement, which is the random jump.

8 Random Jump The random jump is the real problem, because we wish to make it a bit less random. We want it to jump only to places where the nucleus is not. How could we arrange for the random jumps to avoid the nucleus?

9 One Solution (mine) Pick a random distance from the center of the image where the electron will move. Can’t be less than, say, 100. 100 + random (200) Pick a random angle. Know any geometry? Trig?

10 Trigonometry This is high school stuff, and is not essential. There are other solutions to this problem. [List some …] Still, an artist should have a fair knowledge of geometry. Trig - onometry = Triangle - measure

11 Trigonometry This is the most basic stuff. Below is a right triangle, meaning that it has one 90 degree angle. We can give the sides and angles names for convenient reference. Right angle angle a Hypotenuse Angle b Fun fact: The angles in any triangle add up to 180 degrees. Side y opposite angle a Side x opposite angle b Hypotenuse opposite right angle

12 Trigonometry There are ratios in a right triangle that are important. Keep the hypotenuse = 1 for standardization. We can always draw a right triangle within a circle like so. Remember Pythagoras: Hypotenuse 2 =x 2 + y 2 x y Hypotenuse

13 Trigonometry We will define some things and see how they can be useful later. Define sine (a) to be the ratio of y/hypotenuse, or the ratio of the side opposite a to the hypotenuse. In this case the hypotenuse Is always = 1, so sine(a) = y. How does sine(a) change As a changes? a y Hypotenuse

14 Trigonometry We will define some things and see how they can be useful later. Define sine (a) to be the ratio of y/hypotenuse, or the ratio of the side opposite a to the hypotenuse. Note: angles are in radians. Boo!

15 So what? As always, math is only memorable if we can do something useful with it. In this case, we can do the following: given an angle and a side of a right triangle we can figure out the rest. Right angle angle a Hypotenuse Angle b sine a = y/hypotenuse so y = (sine a)*hypotenuse a+b+90 = 180 so b = 90-a sine b = x/hypotenuse so x = (sine b)*hypotenuse x y

16 Back to the problem We want to avoid the nucleus, remember? My solution is to pick a radius (distance) bigger than 100 at random, and an angle at random, and put the electron at that point. a d y x

17 Back to the problem We have a right triangle, and sine a = y/d, or y = a*d. Given a random d and a we can get the x,y coordinates! a d y x

18 Coordinates The (x, y) values are coordinates, as you know, and are in fact Cartesian coordinates, named after Rene Decartes. The (d, a) values can also be thought of as coordinates, and are called polar coordinates. They are equivalent. x y

19 Aside: Star trek Ship direction is Star Trek is given as a pair of numbers: EG 45 mark 60 These are polar coordinates (spherical polar) in 3D. 45 angle units 60 Angle units There are 1000 angle units In a circle, rather than 360 degrees (they went metric, I guess)

20 Back to reality When many things are to be done we have to group the statements using the triangle thingys. ‘Begin’ – ‘End’

21 One Solution (mine) If this one thing is true (this is essentially a coin toss) Then do everything between the triangles Otherwise don’t do anything. Note the indenting – this is intended to show Which things are conditional (part of the IF)

22 And so …

23 A problem Why do the electrons only appear in the upper left of the room? Any ideas? The process of finding the problem in a program like this is called debugging. This kind of problem is called a ‘bug’ (why?)

24 A problem The problem is: We assumed that the coordinate system was centered at 0,0. It is not. We center the coordinates at the center of the screen, where the nucleus is. So the ‘jump to’ instruction needs to be changed.

25 A problem The center is (304, 208):

26 A problem fixed Fixed!

27 Another example Have the object’s sprite change every 120 steps (4 seconds) It means counting the steps – each step, add one to a variable. When the variable’s value reaches 30 (if value > 30) then change the sprite AND set the variable to zero again. Make sense? Change sprite is a click and drop item in the Main1 menu.

28 Another example So: create the variable when the object is created, call it ‘count’, set it to 0. Relative Is this right?

29 Another example It works. But it only changes the sprite once. Is that what we wanted? If we want to change every 4 seconds then we have to keep track of which sprite will be next. Changing to proton is only a change if the current sprite is not a proton. We can have a variable keep track of the current sprite, thus telling us about the next one.

30 Another example The object we’re working with is the electron. Create a variable called current_sprite and initialize it to 0. The value 0 means ‘the electron sprite’, the one it starts with. The variable and the sprite must always agree. Current_sprite=1 means proton_sprite Current_sprite=2 means neutron_sprite When the sprite changes, we add 1 to the variable and change the sprite to agree.

31 Another example If count is larger than 120 start of a block set variable count to 0 add 1 to current_sprite if current_sprite is greater than 2 set variable current_sprite to 0 if current_sprite is equal to 0 change sprite into spr_electron if current_sprite is equal to 1 change sprite into spr_proton if current_sprite is equal to 2 change sprite into spr_neutron end of block

32 Another example A problem. We keep adding 1 to current_sprite, and it gets to be 2, then 3, then 4, and so on. There are no sprites for numbers bigger than 2. Solution: When current_sprite becomes equal to 3, set it to 0 again. Then it goes 0, 1, 2, 0, 1, 2, … This is called modulo 3 counting.

33 Another example Here’s a first try: If count is larger than 120 start of a block set variable count to 0 add 1 to current_sprite if current_sprite is equal to 0 change sprite into spr_electron if current_sprite is equal to 1 change sprite into spr_proton if current_sprite is equal to 2 change sprite into spr_neutron end of block

34 It works (…naturally).

35 If We have seen: 1.How a variable can be tested and how code can be executed conditionally upon that test. 2.That collections of statements can be executed based on one IF. 3. That IF tests can be embedded in one another. … but there is more!

36 If We are driving south down a country road, and get to a ‘T’ intersection. If our destination is to the West, then we turn right. Otherwise we turn left. This can be done as 2 statements: If destination is to the West, turn right. If destination is to the East, turn left.

37 If We are driving south down a country road, and get to a ‘T’ intersection. It can also be done as a more obvious set of statements: If destination is to the West, turn right. else turn left.

38 Else I know of no case where the ELSE is necessary, but it is a more natural statement of the process, and is briefer than restating the condition. We use ‘else’ and ‘otherwise’ in conversation; it makes sense to have them in a computer language.

39 Else Our previous example using ELSE: If count is larger than 120 start of a block set variable count to 0 add 1 to current_sprite if current_sprite is equal to 0 change sprite into spr_electron else if current_sprite is equal to 1 change sprite into spr_proton else change sprite into spr_neutron end of block

40 Else Not clear this is better, but it is sometimes an advantage to have a binary (yes/no, true/false) choice. If (light is red) then stop else go If (count less than 10) do some thing else quit


Download ppt "Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 07 Fall 2010."

Similar presentations


Ads by Google