Skip to main content

Example: Determining if a point is inside of a rectangle

Sequencing principles demonstrated

An extension with two variations.

Learner assumptions

  • Learner knows how to use variables.
  • Learner knows how to write if statements with multiple conditions.
  • Learner knows how to write a function that takes multiple inputs and returns an output.

Prerequisite content

Prior to asking the learner to solve a problem, the following content is provided.

The function in_between determines if a number is in between number_1 and higher_number:

def in_between(number, lower_number, higher_number)
if (number >= lower_number and number <= higher_number):
return True
else
return False

Problem

Write a function point_inside_rectangle that determines if the input point with coordinates x and y are inside of rectangle with the upper-left coordinate x1 and y1 and lower-right coordinate x2 and y2.

Solution

def coordinate_in_rectangle(x, y, x1, y1, x2, y2):
if (x >= x1 and x <= x2 and y >= y1 and y <>= y2):
return True
else:
return False

The extension

The learner needs to realize an and is needed in between these two range conditions (an extension).

x >= x1 and x <= x2
y >= y1 and y <= y2

The variations

x >= x1 and x <= x2 and y >= y1 and y <= y2 are variations of the in_between function because they are substitutions of (number >= and number <= higher_number)