< Back

Lab 15: Logical Operators

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

    $ cd ~/cs120/labs
    $ mkdir lab15 
    $ cd lab15 


Practice 1

Write a function compute_value(x), which uses a single if statement to determine what value to return. This function should return the value of x if x is greater than 0 and less than 10. Otherwise, your program should return 0.



Practice 2

Write a function omit_values(x), which uses a single if statement to determine what value to return. This function should return the value of x as long as x is even or x is negative. Otherwise, your program should return 0.



Point in Rectangle

Collision detection has the reputation of being one of the most difficult parts of creating a good video game. There are many different ways to determine whether two entities are colliding. One of them involves circles (similar to the point in circle program from last class). Another, very trivial type of collision detection is simply determining if a point is located within a given rectangle.

Details

In a Python file point_in_rectangle.py, define a function is_point_in_rectangle(point_x, point_y, rectangle_center_x, rectangle_center_y, rectangle_width, rectangle_height). This function takes 6 parameters. The first two parameters are integers x and y, a point in 2 dimensional space. The next two parameters (rectangle_center_x and rectangle_center_y) are integers representing the point in the very center of a rectangle in 2 dimensional space. The last two parameters (rectangle_width and rectangle_height) are integers representing the width and height of the rectangle.

Your function should return True if the specified point is inside the specified rectangle. Consider a point exacty on the border of the rectangle as inside the rectangle. Your function should return False if the point is not inside the rectangle. Make sure your function always returns True or False! A None value should never be returned, and printing "True" or "False" does not count.

Test Cases

Function Call Expected Output
is_point_in_rectangle(0, 0, 0, 0, 2, 2) True
is_point_in_rectangle(1, 0, 0, 0, 2, 2) True
is_point_in_rectangle(-1, 0, 0, 0, 2, 2) True
is_point_in_rectangle(0, 1, 0, 0, 2, 2) True
is_point_in_rectangle(0, -1, 0, 0, 2, 2) True
is_point_in_rectangle(1, 1, 0, 0, 2, 2) True
is_point_in_rectangle(-1, -1, 0, 0, 2, 2) True
is_point_in_rectangle(-1, 1, 0, 0, 2, 2) True
is_point_in_rectangle(1, -1, 0, 0, 2, 2) True
is_point_in_rectangle(2, 0, 0, 0, 2, 2) False
is_point_in_rectangle(-2, 0, 0, 0, 2, 2) False
is_point_in_rectangle(0, 2, 0, 0, 2, 2) False
is_point_in_rectangle(0,-2, 0, 0, 2, 2) False
is_point_in_rectangle(-3, 2, -3, 2, 1, 1) True
is_point_in_rectangle(0, 1, -3, 2, 1, 1) False
is_point_in_rectangle(2, 0, 0, 0, 4, 2) True
is_point_in_rectangle(0, 2, 0, 0, 4, 2) False

Hint

  • To test if a point is in a rectangle, you must compare the point's location to the location of each of the sides of the rectangle.
  • You are given the center point of a rectangle, but to check if a point is inside of a given rectangle you need to be able to compute the x coordinate for the left and right sides of the rectangle, and the y coordinate of the top and bottom of the rectangle. Create variables for these, so your logical expressions will be more readable. Don't forget that y decreasing is up!
  • While you can write this function using an if statement, you don't have to write it that way. Remember that Boolean values are values just like integers, floats, and strings. Variables can be used to store them.

    	  x = 7
    	  
    	  if x < 10:
    	      single_digit = True
              else:
    	      single_digit = False
    

    Is functionally equivalent to

    	  x = 7
    
    	  single_digit = x < 10
    

Challenge

Modify your program to be visual like our Point in Circle program from last class. Your program should draw a rectangle in the center of the screen that takes up about half of the window. Allow the user to click on the screen for up to 10 points. If the point the user clicked was inside the rectangle, it should be colored green. Otherwise it should be colored red.


Paint

One of the more interesting things that conditionals give us is the ability to interact with a graphical program in different weays. We can tell the difference between a mouse button being down vs. up, we can tell when a keyboard key is pressed, etc. This can also allow us to create programs which allows a user to express their...creativity!

Details

Create a python program in a file called paint.py. Your program should detect when the left mouse button is pressed, and the point that the mouse button was previously at to draw lines when the user drags the mouse.

Example

Hint

  • The graphics module has a function called button_down, which will tell you if the specified mouse button is pressed. You will want to use 1 as the parameter to this function, to check for the left mouse button.
  • You need to keep track of two values, previous_x and previous_y. The variables should be defaulted to 0, and should be updated at the end of EVERY iteration of the animation loop using the values of mouse_x and mouse_y respectively.
  • When you determine the button is down, you should get the current position of the mouse cursor, and draw a line from the previous point stored and the current points.

Challenge

Modify your program so that you can chose between different line colors, based on whether the user clicked on a rectangle defining that color.

Example