nu met 50% meer edging en edges

This commit is contained in:
2023-11-18 18:04:31 +01:00
parent 9d9d6087c4
commit 7ad02f6cc9
+83 -5
View File
@@ -41,7 +41,7 @@ def printPixels(pixels):
n = 2000 # initial points n = 2000 # initial points
dim = 2 # in 2 of 3 dimensies. Alleen zinnig voor 2, maar werkt ook voor 3 dim = 2 # in 2 of 3 dimensies. Alleen zinnig voor 2, maar werkt ook voor 3
k = 10 # aantal clusters/kamers k = 4 # aantal clusters/kamers
domains = [(0,1000)]*dim domains = [(0,1000)]*dim
points = [tuple(round(random.uniform(lb, ub),2) for lb, ub in domains) for _ in range(n)] points = [tuple(round(random.uniform(lb, ub),2) for lb, ub in domains) for _ in range(n)]
@@ -60,10 +60,10 @@ while epochs < 5:
#print(points_per_cluster) #print(points_per_cluster)
#print("---") #print("---")
plotShit(points, centroids) #plotShit(points, centroids)
width= 64 width= 320
height = 24 height = 48
#pixels=[[0]*width]*height #kut python #pixels=[[0]*width]*height #kut python
pixels=[[0]*width for _ in range(height)] # aanspreken als pixels[y][x] (rows columns, verwarrend). pixels=[[0]*width for _ in range(height)] # aanspreken als pixels[y][x] (rows columns, verwarrend).
@@ -73,7 +73,7 @@ for i, c in enumerate(cluster_pixels):
pixels[c[1]][c[0]] = i+1 pixels[c[1]][c[0]] = i+1
epochs = 0 epochs = 0
while epochs < 20: while epochs < width*height/200:
for i, c in enumerate(cluster_pixels): for i, c in enumerate(cluster_pixels):
for y, row in enumerate(pixels): for y, row in enumerate(pixels):
for x, cell in enumerate(row): for x, cell in enumerate(row):
@@ -85,6 +85,84 @@ while epochs < 20:
epochs += 1 epochs += 1
printPixels(pixels) printPixels(pixels)
def checknb(x,y,v):
if x > 0:
if pixels[y][x-1] not in [v, 0]:
return True
if y > 0:
if pixels[y-1][x-1] not in [v, 0]:
return True
if x > 0:
if pixels[y-1][x-1] not in [v, 0]:
return True
if x < width-1:
if pixels[y][x+1] not in [v, 0]:
return True
if y < height-1:
if pixels[y+1][x] not in [v, 0]:
return True
if x < width-1:
if pixels[y+1][x+1] not in [v, 0]:
return True
return False
# Edging the caves
nb = 1
while nb > 0:
nb = 0
for y, row in enumerate(pixels):
for x, cell in enumerate(row):
if cell != 0:
if checknb(x,y,cell):
nb += 1
pixels[y][x] = 0
printPixels(pixels)
#centroid distances
c_dists = [[math.dist(p, c) for c in centroids] for p in centroids]
edges = []
print(centroids)
for i, cdist in enumerate(c_dists):
f = [x if x > 0 else 99999999 for x in cdist]
m = min(f)
one = f.index(m)
f[one] = 9999999
print(f"Centroid {i+1} is closest to centroid {one+1}")
m = min(f)
two = f.index(m)
edges.append((one, two))
print(f"Centroid {i+1} is second closest to centroid {two+1}")
print(cluster_pixels)
def draw(x0, y0, x1, y1):
dx = x1 - x0
dy = y1 - y0
D = 2*dy - dx
y = y0
#for x from x0 to x1
for x in range(x0-1, x1+1):
if pixels[y][x] <= 0:
pixels[y][x] = 9
#plot(x, y)
if D > 0:
y = y + 1
D = D - 2*dx
D = D + 2*dy
print(edges)
# draw edges:
for i, edge in enumerate(edges):
print(f"drawing edges for cluster {i+1}. To {edge[0]+1} and {edge[1]+1}")
draw(
cluster_pixels[edge[0]][0],
cluster_pixels[edge[0]][1],
cluster_pixels[i][0],
cluster_pixels[i][1])
draw(
cluster_pixels[edge[1]][0],
cluster_pixels[edge[1]][1],
cluster_pixels[i][0],
cluster_pixels[i][1])
printPixels(pixels)
#cluster_pixels = [(int(c[0]/domains[0][1]*width), int(c[1]/domains[1][1]*height))for c in centroids]