Skip to content

Wrote Grid Notebook #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 170 additions & 7 deletions grid.ipynb
Original file line number Diff line number Diff line change
@@ -1,26 +1,189 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"source": [
"# Grid\n",
"\n",
"The functions here are used often when dealing with 2D grids (like in TicTacToe).\n",
"\n",
"### Distance\n",
"\n",
"The function returns the Euclidean Distance between two points in the 2D space."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"collapsed": false
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import grid\n",
"import math\n",
"\n",
"print(grid.distance_squared((1, 2), (5, 5)))"
"def distance(a, b):\n",
" \"\"\"The distance between two (x, y) points.\"\"\"\n",
" return math.hypot((a[0] - b[0]), (a[1] - b[1]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n"
]
}
],
"source": [
"print(distance((1, 2), (5, 5)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Distance Squared\n",
"\n",
"This function returns the square of the distance between two points."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"def distance_squared(a, b):\n",
" \"\"\"The square of the distance between two (x, y) points.\"\"\"\n",
" return (a[0] - b[0])**2 + (a[1] - b[1])**2"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"For example:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25\n"
]
}
],
"source": [
"print(distance_squared((1, 2), (5, 5)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Vector Clip\n",
"\n",
"With this function we can make sure the values of a vector are within a given range. It takes as arguments three vectors: the vector to clip (`vector`), a vector containing the lowest values allowed (`lowest`) and a vector for the highest values (`highest`). All these vectors are of the same length. If a value `v1` in `vector` is lower than the corresponding value `v2` in `lowest`, then we set `v1` to `v2`. Similarly we \"clip\" the values exceeding the `highest` values."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"from utils import clip\n",
"\n",
"def vector_clip(vector, lowest, highest):\n",
" \"\"\"Return vector, except if any element is less than the corresponding\n",
" value of lowest or more than the corresponding value of highest, clip to\n",
" those values.\"\"\"\n",
" return type(vector)(map(clip, vector, lowest, highest))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 9)\n"
]
}
],
"source": [
"print(vector_clip((-1, 10), (0, 0), (9, 9)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The vector we wanted to clip was the tuple (-1, 10). The lowest allowed values were (0, 0) and the highest (9, 9). So, the result is the tuple (0,9)."
]
}
],
"metadata": {
Expand All @@ -39,7 +202,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.5.2"
}
},
"nbformat": 4,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy