Homework 1: Inside/Outside Polygon

This assignment introduce you to the fundamentals of python.

  • DUE DATE: TBD

  • POINTS: 20

Deliverables

Write a Python function named ‘is_inside’ that takes two inputs

  • an n by 2 numpy array which represent the x, y coordinates of a polygon

  • a length 2 numpy array which represent the x, y coordinates of some point

The function will return true if the point is inside the polygon. It will return false otherwise.

Provide an example of calling the function for a case where:

  • the point is inside the polygon

  • the point is outside the polygon

  • the point is on one of the sides of the polygon.

Python

Installation

To install Python on Windows, visit the official website: https://www.python.org/downloads/ and follow the installation instructions for your operating system.

On Ubuntu, Python should already be preinstalled.

Running a Script

To run a Python script, open a terminal and type:

python myscript.py

Replace myscript.py with the name of your file.

Important

If you are on Ubuntu and receive the error Command 'python' not found, try:

python3 myscript.py

Note

Additionally, VS Code has a play button that can quickly run your script as well.

NumPy

Installation

To install NumPy using pip on Windows, open a terminal and type:

pip install numpy

On Ubuntu, you can also install with:

sudo apt install python3-numpy

Note

Alternatively for Ubuntu (but not recommended), you can run:

pip3 install numpy --break

Adding numpy to a python script

To add numpy, enter the following to the beginning of the script:

import numpy as np

Here is an example code on how to use:

import numpy as np

# create a 2D array representing points of a triangle
polygon = np.array([[0, 0],
                    [2, 0],
                    [1, 2]])

# create a point
point = np.array([1, 1])

print("Polygon array shape:", polygon.shape)
print("Point array shape:", point.shape)

This should output:

Polygon array shape: (3, 2)
Point array shape: (2,))

Note

For more information, here are some useful links: