SharedNew Projects and Problems / Unfolding an n-gon / Unfold Function.ipynbOpen in CoCalc
Author: Peter Francis
In [115]:
def PlotFigures(Figure, FigSize): Plot = [] P = Figure.columns() Plot = Plot + [line([ [P[i][0], P[i][1]], [P[i+1][0], P[i+1][1]] ], color='blue') for i in [0..len(P)-2]] show(sum(Plot), axes=False, aspect_ratio=1, figsize=FigSize) def Vectorize(figure): C = figure.columns() vectorList = [] for i in [0..len(C)-2]: vectorList = vectorList + [vector(C[i+1]-C[i])] return vectorList def Cross(a,b): c = vector([a[1]*b[2]-a[2]*b[1], -a[0]*b[2]+a[2]*b[0], a[0]*b[1]-a[1]*b[0]]) return c def CheckConvex(figure): C = figure.columns() V = Vectorize(figure) L = len(C) CrossResults = [] for i in [0..L-2]: a = V[i % (L-1)] #vector([V[i][0], V[i][1], 0]) b = V[(i+1) % (L-1)] #vector([V[(i+1) % L-2][0], V[(i+1)% L-2][1],0]) c = Cross(a,b)[2] if c == 0: CrossResults = CrossResults + [m] else: m = abs(c)/c CrossResults = CrossResults + [m] for j in [1..len(CrossResults)-1]: if CrossResults[0] == CrossResults[j] or CrossResults[j] == 0: Test = True else: Test = False break return Test def FlipPoint(Adjacent1, point, Adjacent2): X_1 = Adjacent1[0] Y_1 = Adjacent1[1] X_2 = Adjacent2[0] Y_2 = Adjacent2[1] a = point[0] b = point[1] if (X_2 == X_1) and (Y_2 != Y_1): print 'A' c = a + 2*(X_2-a) d = b else: if (Y_2 == Y_1) and (X_2 != X_1): print 'B' c = a d = b + 2*(Y_2 - b) else: if (Y_2 == Y_1) and (X_2 == X_1): # the two adjacents should never be equal because flipping point should always be after a case like that print 'you know what this means' c = 0 d = 0 else: print 'C' m = (Y_2-Y_1)/(X_2-X_1) c = (m*X_1-Y_1+(a/m)+b)/(m+(1/m)) d =(-1/m)*(c-a)+b return vector([c,d,1]) def Unfold(Figure): n = 0 L = len(Vectorize(Figure)) # number of vectors from point to point in order = number of vertices M = L # mod number NewFigure = copy(Figure) V = Vectorize(NewFigure) a = V[0] b = V[1] c = Cross(a,b)[2] while c == 0: n = n+1 a = V[n] b = V[n+1] c = Cross(a,b)[2] s = abs(c)/c while CheckConvex(NewFigure) == False: n=n+1 V = Vectorize(NewFigure) a = V[n % M] b = V[(n+1) % M] c = Cross(a,b)[2] if c == 0: m = 0 else: m = abs(c)/c show(m) if m != s and m != 0: NewFigure[:,(n % M)] = FlipPoint(NewFigure.columns()[n % M], NewFigure.columns()[(n+1) % M], NewFigure.columns()[(n+2) % M]) print 'something' return NewFigure
In [57]:
twist = column_matrix([ [0,0,1], [1,1,1], [1,0,1], [0,1,1], [0,0,1] ]) PlotFigures(twist,4)
In [ ]:
In [116]:
PlotFigures(Unfold(twist),4)
1-1
11
B something
11
B something
1-1
1-1
00
11
C something
1-1
00
11
A something
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
11
C something
00
1-1
00
WARNING: Some output was deleted.
In [ ]:
CheckConvex(NewFigure)
In [ ]:
In [ ]:
In [68]:
show(twist)
(011000101011111)\left(\begin{array}{rrrrr} 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 0 & 1 & 0 \\ 1 & 1 & 1 & 1 & 1 \end{array}\right)
In [ ]: