/
Mover.py
executable file
·103 lines (80 loc) · 3.56 KB
/
Mover.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
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
from PuzzlePiece import *
from filters import angle_between
from Pixel import *
import math
import numpy as np
file = open('angles.txt', 'w+')
def rotate(origin, point, angle):
"""
Rotate the pixel around `origin` by `angle` degrees
:param origin: Coordinates of points used to rotate around
:param angle: number of degrees
:return: Nothing
"""
ox, oy = origin
px, py = point
qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
if qx != qx or qy != qy:
print("NAN DETECTED: {} {} {} {} {}".format(ox, oy, px, py, qx, qy, angle))
return qx, qy
def stick_pieces(bloc_p, bloc_e, p, e, final_stick=False):
"""
Stick an edge of a piece to the bloc of already resolved pieces
:param bloc_p: bloc of pieces already solved
:param bloc_e: bloc of edges already solved
:param p: piece to add to the bloc
:param e: edge to stick
:return: Nothing
"""
f = open('./testfile.txt', 'w+')
vec_bloc = np.subtract(bloc_e.shape[0], bloc_e.shape[-1])
vec_piece = np.subtract(e.shape[0], e.shape[-1])
translation = np.subtract(bloc_e.shape[0], e.shape[-1])
angle = angle_between((vec_bloc[0], vec_bloc[1], 0), (-vec_piece[0], -vec_piece[1], 0))
# file = open('coords.txt','w+')
# file.write(str(angle) + '\n')
# file.close()
# First move the first corner of piece to the corner of bloc edge
for edge in p.edges_:
edge.shape += translation
# Then rotate piece of `angle` degrees centered on the corner
for edge in p.edges_:
for i, point in enumerate(edge.shape):
edge.shape[i] = rotate(bloc_e.shape[0], point, -angle)
if final_stick:
#prev bounding box
minX, minY, maxX, maxY = float('inf'), float('inf'), -float('inf'), -float('inf')
for i, pixel in enumerate(p.img_piece_):
x, y = p.img_piece_[i].translate(translation[1], translation[0])
minX, minY, maxX, maxY = min(minX, x), min(minY, y), max(maxX, x), max(maxY, y)
# pixel.rotate(bloc_e.shape[0], -angle)
#rotation center
img_p = np.full((maxX - minX + 1, maxY - minY + 1, 3), -1)
for pix in p.img_piece_:
x, y = pix.pos
x, y = x - minX, y - minY
img_p[x, y] = pix.color
#new bounding box
minX2, minY2, maxX2, maxY2 = float('inf'), float('inf'), -float('inf'), -float('inf')
for x in [minX, maxX]:
for y in [minY, maxY]:
x2, y2 = rotate((bloc_e.shape[0][1], bloc_e.shape[0][0]), (x,y), angle)
x2, y2 = int(x2), int(y2)
minX2, minY2, maxX2, maxY2 = min(minX2, x2), min(minY2, y2), max(maxX2, x2), max(maxY2, y2)
f.write(str(x2)+', '+str(y2)+'\n')
pixels = []
for px in range(minX2, maxX2 + 1):
for py in range(minY2, maxY2 + 1):
qx, qy = rotate((bloc_e.shape[0][1], bloc_e.shape[0][0]), (px,py), -angle)
qx, qy = int(qx), int(qy)
if minX <= qx <= maxX and minY <= qy <= maxY and img_p[qx - minX, qy - minY][0] != -1:
pixels.append(Pixel((px, py), img_p[qx - minX, qy - minY]))
finalAng = angle*(180/3.14159)
while finalAng > 360:
finalAng = finalAng - 360
while finalAng < 0:
finalAng = finalAng + 360
# print('Angle:', finalAng)
file.write(str(finalAng) + '\n')
p.img_piece_ = pixels