-
Notifications
You must be signed in to change notification settings - Fork 4
/
extraclasses.py
139 lines (123 loc) · 4.25 KB
/
extraclasses.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (c) 2020 ProceduralJigsaw
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
import numpy as np
import tkinter as tk
import time
import random
from numpy.random import uniform
class Cell():
def __init__(self, coords):
self.coords = coords
def vertices(self):
return [self.coords, (self.coords[0]+1, self.coords[1]),(self.coords[0], self.coords[1]+1),(self.coords[0]+1, self.coords[1]+1)]
def __eq__(self, other):
if isinstance(other, self.__class__):
return self is other or (self.coords == other.coords)
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
class DiagCon():
def __init__(self, p1, p2, p2_taken=True):
self.p1 =p1
self.p2 =p2
self.p2_taken = p2_taken
#slope is useless for now
self.slope = (p2[1]-p1[1])/(p2[0]-p1[0])
ccoords =(min([p2[0],p1[0]]),min([p2[1],p1[1]]))
self.cell = Cell(ccoords)
if(self.slope > 0):
if p2[1]>p1[1]:
self.quadrant = 3
else:
self.quadrant = 1
else:
if p2[1]>p1[1]:
self.quadrant = 2
else:
self.quadrant = 0
@classmethod
def frompointquad(cls,p1,quadrant,p2_taken=True):
self = cls.__new__(cls)
self.p1 = p1
self.quadrant = quadrant
if quadrant == 0:
p2 =(p1[0]+1,p1[1]-1)
elif quadrant == 1:
p2 =(p1[0]-1,p1[1]-1)
elif quadrant ==2 :
p2 =(p1[0]-1,p1[1]+1)
else:
p2 =(p1[0]+1,p1[1]+1)
self.p2 =p2
self.slope = (p2[1]-p1[1])/(p2[0]-p1[0])
ccoords =(min([p2[0],p1[0]]),min([p2[1],p1[1]]))
self.cell = Cell(ccoords)
self.p2_taken = p2_taken
return self
def __eq__(self, other):
if isinstance(other, self.__class__):
return self is other or (self.cell == other.cell and self.slope == other.slope and self.p2_taken == other.p2_taken)
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
"""[summary]
vertex(0,0) vertex(1,0)
*------*
| CELL |
| (0,0)|
vertex(0,1) *------* vertex(1,1)
"""
class Cellgrid():
def __init__(self, nrow, ncol):
self.nrow = nrow
self.ncol = ncol
self.reset()
def reset(self):
ncol = self.ncol
nrow = self.nrow
self.notvisitedvertices = [(x,y) for x in range(0,ncol) for y in range(0,nrow)]
self.vertexgrid = np.zeros((ncol,nrow), dtype=int)#([[0 for x in range(0,ncol)] for y in range(0,nrow)])
self.emptycells =[Cell((x,y)) for x in range(0,ncol-1) for y in range(0,nrow-1)]
def markvertex(self,v,num):
self.vertexgrid[v]=num
"""
Q1 Q0
Q2 Q3
"""
class CircleArc():
def __init__(self, gcp, rad, offs, quadrant, sign):
cp = (gcp[0]*2*rad+rad+offs, gcp[1]*2*rad+rad+offs)
self.cp = cp
self.quadrant = quadrant
self.rad = rad
self.sign = sign
if quadrant == 0:
pa = (cp[0]+rad, cp[1])
pb = (cp[0], cp[1]-rad)
elif quadrant == 1:
pa = (cp[0], cp[1]-rad)
pb = (cp[0]-rad, cp[1])
elif quadrant == 2:
pa = (cp[0]-rad, cp[1])
pb = (cp[0], cp[1]+rad)
else:
pa = (cp[0], cp[1]+rad)
pb = (cp[0]+rad, cp[1])
if self.sign == '-':
self.startpoint = pa
self.endpoint = pb
else:
self.startpoint = pb
self.endpoint = pa
def __eq__(self, other):
if isinstance(other, self.__class__):
return self is other or (self.cp == other.cp and self.quadrant == other.quadrant)
else:
return False
def painttocanvas(self,canvas, width=2):
bwidth = int(round(max(width,self.rad/5),0))
canvas.create_arc(self.cp[0]-self.rad, self.cp[1]-self.rad, self.cp[0]+self.rad, self.cp[1]+self.rad,start=90*(self.quadrant), width =bwidth, extent = 90, outline="black",style=tk.ARC)