CPSC150A
Scientific Computing

Activity 17

Logical Operators

Trux Falsy

Complete generations 0, 1, and 2 of Trux Falsy. By default Trux Falsy uses the C programming language syntax. To change it to Python before starting, click the gear in the upper right corner and choose "and or not True False".

Challenge

Complete the game!

Min 3

Write the function min_3(number_1: float, number_2: float, number_3: float) -> float that returns the minimum of the 3 parameters to the function, number_1, number_2, number_3. The function should not use the built-in min function. The function is the same as the min_3 function you previously wrote. This version of it should use logical operators to simplify the code.

Test Cases

import test

def min_3(number_1: float, number_2: float, number_3: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(min_3(?, ?, ?), ?)
    # Put more test cases here
    return None

main()

Single Digit

Write the function is_single_digit(integer: int) -> bool that returns True if the parameter integer is a single digit number and returns False otherwise. Note, the function should not use any conditional statemetns.

Test Cases

import test

def single_digit(integer: int) -> bool:
    # Put your code here

def main() -> None:
    test.equal(single_digit(?), ?)
    # Put more test cases here
    return None

main()

Point in Rectangle

Computer animations where different elements interact, requires being able to tell if two elements overlap. 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, is determining if a point is located within a rectangle.

Details

Create a Python program that defines the function is_point_in_rect(point_x: float, point_y: float, rect_center_x: float, rect_center_y: float, rect_width: float, rect_height: float) -> bool. The function has 6 parameters. The first two parameters, point_x and point_y are the x and y coordinates of a point. The next two parameters rect_center_x and rect_center_y) the x and y coordinates of the center of a rectangle. The last two parameters rect_width and rect_height the width and height of the rectangle.

The function should return True if the point is inside the rectangle. Assume a point exacty on the border of the rectangle is inside the rectangle. The function should return False if the point is not inside the rectangle.

Test Cases

import test

def is_point_in_rect(point_x: float, point_y: float, rect_center_x: float, rect_center_y: float, rect_width: float, rect_height: float) -> bool:
    # Put your code here

def main() -> None:
    test.equal(is_point_in_rect(?, ?, ?, ?, ?, ?), ?)
    # Put more test cases here
    return None

main()
  • 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.
  • A point is in a rectangle if all of the following are true:
    1. it is to the left of the rectangle's right side
    2. it is to the right of the rectangle's left side
    3. it is below the rectangle's top side
    4. it is above the rectangle's bottom side

Challenge

Modify the program to be visual like the Point in Circle program from last class. The program should draw a rectangle in the center of the screen that takes up about half of the window. And it should randomly generate points that are colored green or red depending on whether they are inside of the rectangle.