polygon.pxi 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from cpp_polygon cimport *
  2. from cpp_vertex cimport cpp_Vertex
  3. from cpp_coefficients cimport cpp_Coefficients
  4. from libcpp.string cimport string
  5. from sage.rings.integer_ring import ZZ
  6. from sage.rings.real_mpfr import RR
  7. from sage.matrix.constructor import matrix
  8. from sage.plot.graphics import Graphics
  9. from sage.plot.line import line2d
  10. cdef class Polygon:
  11. cdef cpp_Polygon cpp
  12. def __cinit__(self,str=None):
  13. if str==None:
  14. self.cpp=cpp_Polygon()
  15. else:
  16. self.cpp=cpp_Polygon(str.encode('utf-8'))
  17. cpdef size(self):
  18. return self.cpp.size()
  19. cpdef graph_size(self):
  20. return self.cpp.graph_size()
  21. def vertex(self,i):
  22. assert(i<self.graph_size())
  23. cdef cpp_Vertex v=self.cpp.vertex(i)
  24. return (v.x,v.y)
  25. cpdef vertices(self):
  26. res=[]
  27. cdef ng=self.cpp.graph_size()
  28. cdef cpp_Vertex v
  29. for i in range(ng):
  30. v=self.cpp.vertex(i)
  31. res.append((v.x,v.y))
  32. return res
  33. @staticmethod
  34. def get_coefficients():
  35. C=Coefficients(init=False)
  36. C.cpp=cpp_Polygon.get_coefficients()
  37. return C
  38. cpdef matrix_B(self):
  39. cdef ng=self.graph_size()
  40. cdef double coeff
  41. B=matrix(ZZ,ng)
  42. for i in range(ng):
  43. for j in range(i,ng):
  44. coeff=self.cpp.get_coeff_B(i,j)
  45. B[i,j]=int(coeff)
  46. B[j,i]=int(coeff)
  47. return B
  48. cpdef matrix_C(self):
  49. cdef ng=self.graph_size()
  50. cdef double coeff
  51. C=matrix(RR,ng)
  52. for i in range(ng):
  53. for j in range(i,ng):
  54. coeff=self.cpp.get_coeff_C(i,j)
  55. C[i,j]=coeff
  56. C[j,i]=coeff
  57. return C
  58. cpdef matrix_M(self):
  59. cdef ng=self.graph_size()
  60. cdef double coeff
  61. M=matrix(RR,ng,ng+1)
  62. for i in range(ng):
  63. for j in range(ng+1):
  64. M[i,j]=self.cpp.get_coeff_M(i,j)
  65. return M
  66. def fp(self):
  67. return self.cpp.get_fp()
  68. def show(self):
  69. B=self.matrix_B()
  70. V=self.vertices()
  71. X=[v[0] for v in V]
  72. Y=[v[1] for v in V]
  73. xmin=min(X)
  74. xmax=max(X)
  75. ymin=min(Y)
  76. ymax=max(Y)
  77. G=Graphics()
  78. for x in range(xmin,xmax+1):
  79. G+=line2d([(x,ymin),(x,ymax)],thickness=0.5,rgbcolor=(0.8,0.8,0.8),linestyle="dotted")
  80. for y in range(ymin,ymax+1):
  81. G+=line2d([(xmin,y),(xmax,y)],thickness=0.5,rgbcolor=(0.8,0.8,0.8),linestyle="dotted")
  82. np=self.size()
  83. ng=self.graph_size()
  84. for i in range(ng):
  85. for j in range(i,ng):
  86. if B[i,j]==1:
  87. if j<np and j==i+1 or (i==0 and j==np-1):
  88. G+=line2d([V[i],V[j]],thickness=1.2,rgbcolor=(0.8,0,0))
  89. else:
  90. G+=line2d([V[i],V[j]],thickness=0.7,rgbcolor=(0.4,0.4,1))
  91. G.show(axes=False,aspect_ratio=1)
  92. def Square(l):
  93. sl=repr(l)
  94. return Polygon('u'+sl+'r'+sl+'d'+sl+'l'+sl)