advertisement

Mathematical Expressions in Alice Some of the information about an Alice world, and its objects, is stored in properties. In general, only the properties used in setting up the world (e.g., color or opacity) are listed. There are many other properties (such as height, width, and position) that are very useful, particularly as our program executes. In order to make use of these other properties, Alice provides functions, which are programming statements that can be used to ask questions about various properties of objects (e.g., height and width), as well as relationships between objects (e.g., distance between objects). When the built-in functions do not provide the answers you need, it is also possible to form mathematical expressions that allow the programmer to refine their answer. These mathematical expressions can be used with functions or user input to provide necessary details for the animation of your program. Mathematical Expressions You are probably familiar with mathematical expressions from math class, where you use algorithms to perform calculations. Examples are calculations of the slope of a line, or the area of a circle. The most common mathematical operations are addition, subtraction, multiplication, and division. Most (perhaps all) programming languages, including Alice, provide a variety of mathematical operators, including the basic operators shown in the following table. Operator Description + Addition - Subtraction * Multiplication / Division For example, consider a simple calculation that relates the diameter of a circle to the radius. You may recall that the mathematical relationship is radius = diameter / 2. Page 1 of 5 Mathematical Expressions in Alice To perform this simple operation in Alice, we will need: 1. Variables to store the values for diameter and radius. 2. User input to provide the diameter of the circle. 3. A mathematical expression to calculate the radius from the given diameter. As you can see, we have created our variables (diameter, radius), and initialized them to both have values of zero. Although Alice, by default, initializes numeric values to one, it is a better programming practise to initialize numeric values to zero. Since a diameter of zero isn't very interesting, the user is asked to enter a value (number) for the diameter. We have also set the radius equal to the diameter, but this isn't the correct calculation. This was simply the starting point. In the “set value to” operation, click on the diameter to see additional options. There is a “Math” option, which shows mathematical operations that can be performed on diameter. We want to divide diameter by something, so choose the “diameter /”, and set the value to 2 (i.e., divide diameter by 2). Unfortunately, this program doesn't actually do anything useful, but we have looked at the way to use mathematical expressions in our program. Page 2 of 5 Mathematical Expressions in Alice Exercise 1 – Using Math to Avoid Collisions In graphics programming, a collision is when two graphical objects come into contact with each other. Quite often, the objects will continue and pass through each other, or end up occupying the same space. Here you will create an Alice world where one object moves to another object but does not collide with it. Consider the following problem: “A snowman and a snowWoman are standing, facing in the same direction. The snowman turns to face the snowWoman, and then the snowWoman turns to face the snowman. The snowman moves to the snowWoman and stops just in front of her. He engages her in a conversation” A simple design, using pseudocode, might look like: snowman turns to face snowWoman snowWoman turns to face snowman snowman moves forward to the front of the snowWoman snowman says something snowWoman says something The majority of this program contains concepts you are already familiar with. The real issue is the move instruction, shown in blue. As a first attempt, our Alice instruction would probably look like the one shown below. Unfortunately, this movement will result in a collision, where the snowman and snowWoman are completely overlapping each other. Since there doesn't seem to be a better primitive function to use, we will need to create a mathematical expression to refine our result. The problem is that the distance to function calculates and returns the distance between the centres of the objects. We need to calculate the distance between the front of the snowman and the front of the snowWoman. Page 3 of 5 Mathematical Expressions in Alice distance between centres snowman depth snowWoman depth From our simple diagram, the unwanted distance is related to the depth (front-to-back distance) for the snowman and snowWoman. If we can account for this, the movement should occur without a collision. To make things a bit easier (for now), we will notice that the depth of each figure is approximately the same, so instead of using half of each, we will simply use the full depth of one of them. The snowWoman seems to be slightly larger, so we'll use that one just to be on the safe side. Our new code will subtract the depth of the snowWoman from the distance between them. Now that our distance is getting complicated, it's probably a good idea to use a variable to store the distance between the snowPeople. 1. Create a new Alice world using the snow template. Add an instance of the Snowman class and the SnowWoman class (both from the People collection). 2. Create a variable named distance that is of type Number. Set the initial value to zero. 3. Create the instructions as shown. Since the distance variable is initialized to zero, the snowman won't move when you run the program. 4. Now we will add the distance calculation using a mathematical expression. Drag the distance variable between the second and third lines in the code. a) Start by setting the distance to the distance between their centres. Page 4 of 5 Mathematical Expressions in Alice b) Perform the subtraction operation by using the menu on the set value to instruction. You will need to use a default value for now (zero is suggested). click here c) Replace the default value from step (b) with the depth of the snowWoman, which is another function from the snowWoman object. 5. Play the world. The snowman should move close to the snowWoman, but they should not be touching. Try moving the snowman and snowWoman to random locations and ensure the program still works. Save your program as snowmanDistance.a2w Page 5 of 5