0% found this document useful (0 votes)
3 views3 pages

Python Functions for Grid Creation

The document provides exercises for creating functions in Python, specifically focusing on a function that generates a grid of lines based on a starting point and lists of spacings in the x and y directions. It includes two implementations of the function 'gridline' and 'gridline2', along with examples of their usage and expected outputs. Additionally, it emphasizes the importance of testing functions with representative data.

Uploaded by

MING YANG
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Functions for Grid Creation

The document provides exercises for creating functions in Python, specifically focusing on a function that generates a grid of lines based on a starting point and lists of spacings in the x and y directions. It includes two implementations of the function 'gridline' and 'gridline2', along with examples of their usage and expected outputs. Additionally, it emphasizes the importance of testing functions with representative data.

Uploaded by

MING YANG
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Course Exercises - Session 4

Functions
The exercise is to create functions, so this requires you to define the name of the function and list the
arguments (parameters) in parentheses () , followed by a colon : . All the lines that follow should be
indented to show that they belong to the function.

The last line of the function should (normally) contain the keyword return followed by the variable (or
expression) that will give the answer.

Grasshopper & Dynamo Exercises


Create a geometrical function and insert it in a Grasshopper 'component' and a Dynamo 'node':

A function that creates a grid of lines when provided with a starting point and a list of spacings in x & y
direction. You should end up with a grillage defined by a list of lines represented by start and end points
(first x-dir, then y-dir and always in the positive direction).

gridline(start_point, x_list, y_list)

For example:

start_point = [1, 2, -1]


x_list = [3,2,2]
y_list = [3,3]

print(gridline(start_point, x_list, y_list))

The result is a list of seven pairs of triplets (list of lists of lists) [[[1,2,-1],[8,2,-1]], [[1,5,-1],
[8,5,-1]], [[1,8,-1],[8,8,-1]], [[1,2,-1],[1,8,-1]], [[4,2,-1],[4,8,-1]], [[6,2,-1],
[6,8,-1]], [[8,2,-1],[8,8,-1]]]
In [15]:

# start by grabbing an accumulation tool from a library


# list(accumulate([3,2,2])) -> [3,5,7]
from itertools import accumulate

def gridline(start_point, x_list, y_list):


# You could use
x0, y0, z0 = start_point
x_len = sum(x_list)
y_len = sum(y_list)
x_lines = [[[x0, y0, z0], [x0 + x_len, y0, z0]]]
y1 = y0
for y in y_list:
y1 += y
x_lines.append([[x0, y1, z0], [x0 + x_len, y1, z0]])
y_lines = [[[x0, y0, z0], [x0, y0 + y_len, z0]]]
x1 = x0
for x in x_list:
x1 += x
y_lines.append([[x1, y0, z0], [x1, y0 + y_len, z0]])
return x_lines + y_lines

# Alternatively we can use the accumulate function and list comprehension


def gridline2(start_point, x_list, y_list):
# You could use
x0, y0, z0 = start_point
x_len = sum(x_list)
y_len = sum(y_list)
x_coords = accumulate([x0] + x_list) # This adds the starting x-coordinate to th
e list before accumulating
y_coords = accumulate([y0] + y_list)
x_lines = [[[x0, y, z0], [x0 + x_len, y, z0]] for y in y_coords]
y_lines = [[[x, y0, z0], [x, y0 + y_len, z0]] for x in x_coords]
return x_lines + y_lines

Testing the Functions


It is always a good idea to test your functions with a full range of representative data to see whether they
operate correctly.

In [17]:

start_point = [1, 2, -1]


x_list = [3,2,2]
y_list = [3,3]
print(gridline(start_point, x_list, y_list))
print(gridline2(start_point, x_list, y_list))

[[[1, 2, -1], [8, 2, -1]], [[1, 5, -1], [8, 5, -1]], [[1, 8, -1], [8,
8, -1]], [[1, 2, -1], [1, 8, -1]], [[4, 2, -1], [4, 8, -1]], [[6, 2, -
1], [6, 8, -1]], [[8, 2, -1], [8, 8, -1]]]
In [19]:

start_point = [0, 0, 0]
x_list = [1,1,1]
y_list = [2,2,2]
print(gridline(start_point, x_list, y_list))
print(gridline2(start_point, x_list, y_list))

[[[0, 0, 0], [3, 0, 0]], [[0, 2, 0], [3, 2, 0]], [[0, 4, 0], [3, 4,
0]], [[0, 6, 0], [3, 6, 0]], [[0, 0, 0], [0, 6, 0]], [[1, 0, 0], [1, 6,
0]], [[2, 0, 0], [2, 6, 0]], [[3, 0, 0], [3, 6, 0]]]
[[[0, 0, 0], [3, 0, 0]], [[0, 2, 0], [3, 2, 0]], [[0, 4, 0], [3, 4,
0]], [[0, 6, 0], [3, 6, 0]], [[0, 0, 0], [0, 6, 0]], [[1, 0, 0], [1, 6,
0]], [[2, 0, 0], [2, 6, 0]], [[3, 0, 0], [3, 6, 0]]]

Python & Other Programs


Note that the session on Python and Other Programs does not have any exercises...

In [ ]:

You might also like