![]() ![]() Set the green value to 75% of its original value (i.e.Suppose we want to change the yellow flowers to look orange.Now we can express what new value we want in terms of the old value, like "triple the red value", or "set the red value to 75% of what it was". tRed(pixel.getRed() * something) īefore we could only express ideas like "set the red value to 200". Bottom line, we'll use this pattern a lot:.double the red value (with set/get pattern): Now can express relative color number changes.Loops With Expressions - Set/Get Patterns If setRed() is called with a value a greater than 255, it just uses 255, and likewise if a value less than 0 is passed in, it just uses the value 0. functions automatically limit the value set to the range 0.255. Grade 2 : common core by Fuson, Karen C Publication date 2015 Topics Mathematics - Study and teaching (Elementary), Mathematics - Study and teaching (Primary) Publisher Orlando, Fla. This expression is evaluated first, resulting a number such as 240. The code pixel.getRed() * 2 is an expression, which is whatever the old red value was multiplied by 2. Whatever the red value, the line doubles it.Result is changing the pixel red from 120 to 240.Evaluates the pixel.getRed() * 2 expression. Q: What does running the line above do to the pixel?.The net effect is to double the red value of the pixel from 50 to 100. The second line computes red * 2 (100 in this case), and sets that value back into the pixel as its new red value. Say in this case that the red value is 50. The first line retrieves the red value from the pixel and stores that value in an variable named red. ![]() For example, the above code snippet doubles the pixel's red value. The pixel.getRed() can be combined with tRed( number) to operate on a pixel. This is a more realistic sort of operation.Suppose we want to double the red value of a pixel.Set/Get Pattern: combine tRed() and pixel.getRed() We also introduce variables and expressions into our word problem worksheets. A good example of the inflexible nature of computers. The syntax requires that calling a function includes the parenthesis, even if there's nothing inside the parenthesis. Notice that calling, say, pixel.getRed() has the pair of parenthesis () after it. Here is an example of getRed() - get the red number out of a pixel, store it in a variable named "red". pixel.getBlue() - retrieves the blue value.pixel.getGreen() - retrieves the green value.pixel.getRed() - retrieves the red value from a pixel.These will be very handy to use in expressions. We have not used them until now, but there are three pixel functions that get the red, green or blue value out of a pixel. Pixel.getRed() / pixel.getGreen() / pixel.getBlue() Anywhere in the code where we have used a fixed number like 0 or 255 of whatever, we can instead write an expression, letting the code compute a value when that line runs. Then in effect it calls print(42), passing in the computed value. When that line runs, the computer first computes the expression 11 + 31, yielding 42. For example you could write something like this: Instead of a plain number like 42, an " expression" written in the code like 11 + 13 computes the value to use. We have seen code that "calls" a function where we pass in a value within the parenthesis, such as the value 42 passed in to the print function below. ![]()
0 Comments
Leave a Reply. |