Newer
Older
{
"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",
"collapsed": true,
"deletable": true,
"editable": true
"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",
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"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": [],
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"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": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",