CPSC120
Fundamentals of Computer Science

Activity 17

Logic 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!

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.

Test Cases

import test

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

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

main()

Challenge

Write the function so that it does not use any conditional statements.

Point in Rectangle

Computer animations where different elements interact requires being able to tell if two regions overlap. There are many different ways to determine whether two entities are colliding. One is to test if a position is inside a circle like you previously wrote. Another is to determine if a point is 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, are the x and y coordinates of the center of a rectangle. The last two parameters, rect_width and rect_height, are the width and height of the rectangle.

The function should return True if the point is inside the rectangle. Assume a point precisely 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, rectangle_center_x: float, rectangle_center_y: float, rectangle_width: float, rectangle_height: float) -> bool:
    # Put your code here.

def main() -> None:
    test.equal(is_point_in_rect(0.0, 0.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(0.0, 0.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(1.0, 0.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(-1.0, 0.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(0.0, 1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(0.0, -1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(1.0, 1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(-1.0, -1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(-1.0, 1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(1.0, -1.0, 0.0, 0.0, 2.0, 2.0), True)
    test.equal(is_point_in_rect(2.0, 0.0, 0.0, 0.0, 2.0, 2.0), False)
    test.equal(is_point_in_rect(-2.0, 0.0, 0.0, 0.0, 2.0, 2.0), False)
    test.equal(is_point_in_rect(0.0, 2.0, 0.0, 0.0, 2.0, 2.0), False)
    test.equal(is_point_in_rect(0.0,-2.0, 0.0, 0.0, 2.0, 2.0), False)
    test.equal(is_point_in_rect(-3.0, 2.0, -3.0, 2.0, 1.0, 1.0), True)
    test.equal(is_point_in_rect(0.0, 1.0, -3.0, 2.0, 1.0, 1.0), False)
    test.equal(is_point_in_rect(2.0, 0.0, 0.0, 0.0, 4.0, 2.0), True)
    test.equal(is_point_in_rect(0.0, 2.0, 0.0, 0.0, 4.0, 2.0), False)
    return None

main()

Hint

  • To test if a point is in a rectangle, you must compare the point's location to each of the rectangle’s sides.

  • You are given the center point of a rectangle, you need to compute the coordinates of the rectangle's sides. Create variables for these, so your logical expressions will be more readable.

  • The rectangle’s side can be computed using the position and dimensions of the rectangle:

    1. The right side is the center's x plus half the width.
    2. The left side is the center's x minus half the width.
    3. The top side is the center's y plus half the height.
    4. The bottom side is the center's y minus half the height.
  • 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. 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 the rectangle.