-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.py
75 lines (61 loc) · 1.88 KB
/
shape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
import math
def get_nearby(x, y):
return (
(x, y+1),
(x-1, y), (x+1, y),
(x, y-1),
)
def get_value(x, y, shape):
return shape[y][x]
def get_width(shape):
return len(shape[0])
def get_height(shape):
return len(shape)
def get_nodes(shape):
polyomino = []
for y in range(get_height(shape)):
for x in range(get_width(shape)):
if shape[y][x] == 1:
polyomino.append((x, y))
return polyomino
def get_circumference(shape):
polyomino = get_nodes(shape)
circumference = 0
for x, y in polyomino:
if (x+1, y) not in polyomino:
circumference += 1
if (x-1, y) not in polyomino:
circumference += 1
if (x, y+1) not in polyomino:
circumference += 1
if (x, y-1) not in polyomino:
circumference += 1
return circumference
def mean_distance(points):
"""
Calculates the mean distance between a list of points in two-dimensional space.
Args:
points (list): A list of tuples representing (x, y) coordinates of points.
Returns:
float: The mean distance between the points.
"""
n = len(points)
total_distance = 0
for i in range(n):
for j in range(i+1, n):
distance = math.sqrt((points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2)
total_distance += distance
mean_distance = total_distance / (n*(n-1)/2)
return mean_distance
def shape_center(a):
"""
Calculate the center (i.e., midpoint) of a shape represented as a set of coordinate pairs on a 2D grid.
"""
epsilon = 1e-5
shape = get_nodes(a)
n = len(shape)
sum_y = sum(y for _, y in shape)
sum_x = sum(x for x, _ in shape)
center_x = sum_x / n + epsilon
center_y = sum_y / n + epsilon
return (center_x, center_y)