= 0 and x + deltaX < N and y + deltaY >= 0 and deltaY < N: matrix[x + deltaX, y + deltaY] += 0.2 for deltaX in range(-1, 2): for deltaY in range(-1, 2): if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N: matrix[x + deltaX, y + deltaY] += 0.3 matrix[x, y] += 0.5 for i in range(N): "> = 0 and x + deltaX < N and y + deltaY >= 0 and deltaY < N: matrix[x + deltaX, y + deltaY] += 0.2 for deltaX in range(-1, 2): for deltaY in range(-1, 2): if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N: matrix[x + deltaX, y + deltaY] += 0.3 matrix[x, y] += 0.5 for i in range(N): "> = 0 and x + deltaX < N and y + deltaY >= 0 and deltaY < N: matrix[x + deltaX, y + deltaY] += 0.2 for deltaX in range(-1, 2): for deltaY in range(-1, 2): if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N: matrix[x + deltaX, y + deltaY] += 0.3 matrix[x, y] += 0.5 for i in range(N): ">
import numpy as np
def openFile():
filename = "ex5_data.txt"
try:
with open(filename, 'r') as file:
content = file.read()
return content
except FileNotFoundError:
print(f"File '{filename}' not found.")
except Exception as e:
print(f"An error occured: {e}")
def numpyVersion(N, data):
matrix = np.zeros((N, N))
for spotlight in data:
x = int(spotlight.split()[0])
y = int(spotlight.split()[1])
for deltaX in range(-2, 3):
for deltaY in range(-2, 3):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.2
for deltaX in range(-1, 2):
for deltaY in range(-1, 2):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.3
matrix[x, y] += 0.5
for i in range(N):
for j in range(N):
print(f"{matrix[i, j]:.1f} ", end = "")
print()
def main():
fileContent = openFile()
N = int(fileContent.split('\\n')[0])
data = fileContent.split('\\n')[1:]
numpyVersion(N, data)
if __name__ == "__main__":
main()
openFile()
functiondef openFile():
filename = "ex5_data.txt"
try:
with open(filename, 'r') as file:
content = file.read()
return content
except FileNotFoundError:
print(f"File '{filename}' not found.")
except Exception as e:
print(f"An error occured: {e}")
'r'
) and reads all the text into a string called content
.numpyVersion(N, data)
functiondef numpyVersion(N, data):
matrix = np.zeros((N, N))
for spotlight in data:
x = int(spotlight.split()[0])
y = int(spotlight.split()[1])
for deltaX in range(-2, 3):
for deltaY in range(-2, 3):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.2
for deltaX in range(-1, 2):
for deltaY in range(-1, 2):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.3
matrix[x, y] += 0.5
for i in range(N):
for j in range(N):
print(f"{matrix[i, j]:.1f} ", end = "")
print()
This is where the main action happens. Let’s break down the logic:
Create a 2D NumPy array:
matrix = np.zeros((N, N))
N x N
matrix filled with zeros. This will hold the total light intensity on each tile.Loop over each spotlight:
for spotlight in data:
x = int(spotlight.split()[0])
y = int(spotlight.split()[1])
data
list has two numbers: the coordinates of a spotlight, x
and y
.Add 0.2 intensity in a 5 x 5 region:
for deltaX in range(-2, 3):
for deltaY in range(-2, 3):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.2
range(-2, 3)
means deltaX
and deltaY
will run from -2
to 2
, inclusive.if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
to make sure we don’t go out of bounds if the spotlight is near to the edge of the matrix.0.2
to that tile’s intensity.Add 0.3 intensity in a 3 x 3 region:
for deltaX in range(-1, 2):
for deltaY in range(-1, 2):
if x + deltaX >= 0 and x + deltaX < N and y + deltaY >= 0 and y + deltaY < N:
matrix[x + deltaX, y + deltaY] += 0.3
0.3
.Add 0.5 intensity to the spotlight’s own tile:
matrix[x, y] += 0.5
0.5
.Print the matrix:
for i in range(N):
for j in range(N):
print(f"{matrix[i, j]:.1f} ", end = "")
print()
i
) and every column (j
) of the matrix
, printing out the final intensity value formatted with 1 decimal place (:.1f
).end = ""
in the inner loop keeps the numbers in the same line, and the outer loop’s print()
moves to the next line after finishing each row.What’s the big idea?