Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 92681e5

Browse files
committed
refresh the file polyhedron/plot.py
1 parent 9d686f2 commit 92681e5

File tree

1 file changed

+79
-48
lines changed
  • src/sage/geometry/polyhedron

1 file changed

+79
-48
lines changed

src/sage/geometry/polyhedron/plot.py

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def cyclic_sort_vertices_2d(Vlist):
122122
break
123123
else:
124124
result += chain
125-
chain = [ Vlist.pop() ]
125+
chain = [Vlist.pop()]
126126
result += chain
127127
return result
128128

@@ -182,16 +182,16 @@ def __init__(self, projection_point):
182182
self.psize = norm(pproj)
183183
if (self.psize).is_zero():
184184
raise ValueError("projection direction must be a non-zero vector.")
185-
v = vector(RDF, [0.0]*(self.dim-1) + [-self.psize]) - pproj
185+
v = vector(RDF, [0.0] * (self.dim - 1) + [-self.psize]) - pproj
186186
polediff = matrix(RDF, v).transpose()
187-
denom = RDF((polediff.transpose()*polediff)[0][0])
187+
denom = RDF((polediff.transpose() * polediff)[0][0])
188188
if denom.is_zero():
189189
self.house = identity_matrix(RDF, self.dim)
190190
else:
191191
house = identity_matrix(RDF, self.dim) \
192192
- 2*polediff*polediff.transpose()/denom # Householder reflector
193193
# Make it preserve orientation (chirality):
194-
self.house = diagonal_matrix(RDF,[1]*(self.dim - 1) + [-1] ) * house
194+
self.house = diagonal_matrix(RDF, [1] * (self.dim - 1) + [-1]) * house
195195

196196
def __call__(self, x):
197197
"""
@@ -225,11 +225,11 @@ def __call__(self, x):
225225
(0.5, 0.0)
226226
"""
227227
img = self.house * x
228-
denom = self.psize-img[self.dim-1]
228+
denom = self.psize - img[self.dim - 1]
229229
if denom.is_zero():
230-
raise ValueError('Point cannot coincide with ' \
231-
'coordinate singularity at ' + repr(x))
232-
return vector(RDF, [img[i]/denom for i in range(self.dim-1)])
230+
raise ValueError('Point cannot coincide with '
231+
'coordinate singularity at ' + repr(x))
232+
return vector(RDF, [img[i] / denom for i in range(self.dim - 1)])
233233

234234

235235
class ProjectionFuncSchlegel():
@@ -359,7 +359,7 @@ def __init__(self, polyhedron, proj=projection_func_identity):
359359
self.parent_polyhedron = polyhedron
360360
self.coords = Sequence([])
361361
self.points = Sequence([])
362-
self.lines = Sequence([])
362+
self.lines = Sequence([])
363363
self.arrows = Sequence([])
364364
self.polygons = Sequence([])
365365
self.polyhedron_ambient_dim = polyhedron.ambient_dim()
@@ -478,7 +478,7 @@ def schlegel(self, facet=None, position=None):
478478
from the barycenter of ``facet``. A value close to 0 will place the
479479
projection point close to the facet and a large value further away.
480480
If the given value is too large, an error is returned.
481-
If no position is given, it takes the midpoint of the possible
481+
If no position is given, it takes the midpoint of the possible
482482
point of views along a line spanned by the barycenter of the facet
483483
and a valid point outside the facet.
484484
@@ -530,12 +530,12 @@ def schlegel(self, facet=None, position=None):
530530
if position is not None and position <= 0:
531531
raise ValueError("'position' should be a positive number")
532532

533-
barycenter = ZZ.one()*sum([v.vector() for v in facet.vertices()]) / len(facet.vertices())
533+
barycenter = ZZ.one() * sum([v.vector() for v in facet.vertices()]) / len(facet.vertices())
534534
locus_polyhedron = facet.stacking_locus()
535535
repr_point = locus_polyhedron.representative_point()
536536
if position is None:
537537
# Figure out a somehow canonical point of view inside the locus
538-
# polyhedron
538+
# polyhedron
539539
from sage.geometry.polyhedron.constructor import Polyhedron
540540
the_ray = Polyhedron(vertices=[barycenter],
541541
rays=[repr_point - barycenter],
@@ -682,7 +682,7 @@ def _init_points(self, polyhedron):
682682
[0, 1, 2, 3]
683683
"""
684684
for v in polyhedron.vertex_generator():
685-
self.points.append( self.coord_index_of(v.vector()) )
685+
self.points.append(self.coord_index_of(v.vector()))
686686

687687
def _init_lines_arrows(self, polyhedron):
688688
"""
@@ -703,9 +703,10 @@ def _init_lines_arrows(self, polyhedron):
703703
"""
704704
obj = polyhedron.Vrepresentation()
705705
for i in range(len(obj)):
706-
if not obj[i].is_vertex(): continue
706+
if not obj[i].is_vertex():
707+
continue
707708
for j in range(len(obj)):
708-
if polyhedron.vertex_adjacency_matrix()[i,j] == 0:
709+
if polyhedron.vertex_adjacency_matrix()[i, j] == 0:
709710
continue
710711
if i < j and obj[j].is_vertex():
711712
l = [obj[i].vector(), obj[j].vector()]
@@ -743,8 +744,10 @@ def _init_area_2d(self, polyhedron):
743744

744745
def adjacent_vertices(i):
745746
n = len(vertices)
746-
if vertices[(i-1) % n].is_vertex(): yield vertices[(i-1) % n]
747-
if vertices[(i+1) % n].is_vertex(): yield vertices[(i+1) % n]
747+
if vertices[(i-1) % n].is_vertex():
748+
yield vertices[(i-1) % n]
749+
if vertices[(i+1) % n].is_vertex():
750+
yield vertices[(i+1) % n]
748751

749752
for i in range(len(vertices)):
750753
v = vertices[i]
@@ -764,8 +767,8 @@ def adjacent_vertices(i):
764767
aline = next(polyhedron.line_generator())
765768
for shift in [aline(), -aline()]:
766769
for i in range(len(coords)):
767-
polygons.append( [ coords[i-1],coords[i],
768-
coords[i]+shift, coords[i-1]+shift ] )
770+
polygons.append([coords[i - 1], coords[i],
771+
coords[i] + shift, coords[i - 1] + shift])
769772

770773
if polyhedron.n_lines() == 2:
771774
[line1, line2] = [l for l in polyhedron.lines()]
@@ -856,9 +859,9 @@ def adjacent_vertices(i):
856859
l1 = line1()
857860
l2 = line2()
858861
for v in polyhedron.vertex_generator():
859-
polygons.append( [v()-l1-l2, v()+l1-l2, v()+l1+l2, v()-l1+l2] )
862+
polygons.append([v()-l1-l2, v()+l1-l2, v()+l1+l2, v()-l1+l2])
860863

861-
self.polygons.extend( [self.coord_indices_of(p) for p in polygons] )
864+
self.polygons.extend([self.coord_indices_of(p) for p in polygons])
862865

863866
def render_points_1d(self, **kwds):
864867
"""
@@ -938,10 +941,10 @@ def render_outline_2d(self, **kwds):
938941
wireframe = []
939942
for l in self.lines:
940943
l_coords = self.coordinates_of(l)
941-
wireframe.append( line2d(l_coords, **kwds) )
944+
wireframe.append(line2d(l_coords, **kwds))
942945
for a in self.arrows:
943946
a_coords = self.coordinates_of(a)
944-
wireframe.append( arrow(a_coords[0], a_coords[1], **kwds) )
947+
wireframe.append(arrow(a_coords[0], a_coords[1], **kwds))
945948
return sum(wireframe)
946949

947950
def render_fill_2d(self, **kwds):
@@ -958,7 +961,7 @@ def render_fill_2d(self, **kwds):
958961
0.8
959962
"""
960963
poly = [polygon2d(self.coordinates_of(p), **kwds)
961-
for p in self.polygons]
964+
for p in self.polygons]
962965
return sum(poly)
963966

964967
def render_vertices_3d(self, **kwds):
@@ -990,7 +993,7 @@ def render_wireframe_3d(self, **kwds):
990993
wireframe = []
991994
for l in self.lines:
992995
l_coords = self.coordinates_of(l)
993-
wireframe.append( line3d(l_coords, **kwds))
996+
wireframe.append(line3d(l_coords, **kwds))
994997
for a in self.arrows:
995998
a_coords = self.coordinates_of(a)
996999
wireframe.append(arrow3d(a_coords[0], a_coords[1], **kwds))
@@ -1011,7 +1014,7 @@ def render_solid_3d(self, **kwds):
10111014
N = max([-1] + [i for p in polys for i in p]) + 1
10121015
return polygons3d(polys, self.coordinates_of(range(N)), **kwds)
10131016

1014-
def render_0d(self, point_opts={}, line_opts={}, polygon_opts={}):
1017+
def render_0d(self, point_opts=None, line_opts=None, polygon_opts=None):
10151018
"""
10161019
Return 0d rendering of the projection of a polyhedron into
10171020
2-dimensional ambient space.
@@ -1032,15 +1035,21 @@ def render_0d(self, point_opts={}, line_opts={}, polygon_opts={}):
10321035
sage: print(Polyhedron(ieqs=[(1,)]).projection().render_0d().description())
10331036
Point set defined by 1 point(s): [(0.0, 0.0)]
10341037
"""
1038+
if point_opts is None:
1039+
point_opts = {}
1040+
if line_opts is None:
1041+
line_opts = {}
1042+
if polygon_opts is None:
1043+
polygon_opts = {}
10351044
if isinstance(point_opts, dict):
10361045
point_opts.setdefault('zorder', 2)
10371046
point_opts.setdefault('pointsize', 10)
10381047
if self.points:
1039-
return point2d([0,0], **point_opts)
1048+
return point2d([0, 0], **point_opts)
10401049
else:
10411050
return Graphics()
10421051

1043-
def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
1052+
def render_1d(self, point_opts=None, line_opts=None, polygon_opts=None):
10441053
"""
10451054
Return 1d rendering of the projection of a polyhedron into
10461055
2-dimensional ambient space.
@@ -1060,6 +1069,12 @@ def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
10601069
Graphics object consisting of 2 graphics primitives
10611070
"""
10621071
plt = Graphics()
1072+
if point_opts is None:
1073+
point_opts = {}
1074+
if line_opts is None:
1075+
line_opts = {}
1076+
if polygon_opts is None:
1077+
polygon_opts = {}
10631078
if isinstance(point_opts, dict):
10641079
point_opts.setdefault('zorder', 2)
10651080
point_opts.setdefault('pointsize', 10)
@@ -1069,7 +1084,7 @@ def render_1d(self, point_opts={}, line_opts={}, polygon_opts={}):
10691084
plt += self.render_line_1d(**line_opts)
10701085
return plt
10711086

1072-
def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
1087+
def render_2d(self, point_opts=None, line_opts=None, polygon_opts=None):
10731088
"""
10741089
Return 2d rendering of the projection of a polyhedron into
10751090
2-dimensional ambient space.
@@ -1088,6 +1103,12 @@ def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
10881103
Graphics object consisting of 17 graphics primitives
10891104
"""
10901105
plt = Graphics()
1106+
if point_opts is None:
1107+
point_opts = {}
1108+
if line_opts is None:
1109+
line_opts = {}
1110+
if polygon_opts is None:
1111+
polygon_opts = {}
10911112
if isinstance(point_opts, dict):
10921113
point_opts.setdefault('zorder', 2)
10931114
point_opts.setdefault('pointsize', 10)
@@ -1100,7 +1121,7 @@ def render_2d(self, point_opts={}, line_opts={}, polygon_opts={}):
11001121
plt += self.render_fill_2d(**polygon_opts)
11011122
return plt
11021123

1103-
def render_3d(self, point_opts={}, line_opts={}, polygon_opts={}):
1124+
def render_3d(self, point_opts=None, line_opts=None, polygon_opts=None):
11041125
"""
11051126
Return 3d rendering of a polyhedron projected into
11061127
3-dimensional ambient space.
@@ -1146,6 +1167,12 @@ def render_3d(self, point_opts={}, line_opts={}, polygon_opts={}):
11461167
pplt = None
11471168
lplt = None
11481169
pgplt = None
1170+
if point_opts is None:
1171+
point_opts = {}
1172+
if line_opts is None:
1173+
line_opts = {}
1174+
if polygon_opts is None:
1175+
polygon_opts = {}
11491176
if isinstance(point_opts, dict):
11501177
point_opts.setdefault('width', 3)
11511178
pplt = self.render_vertices_3d(**point_opts)
@@ -1341,10 +1368,10 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
13411368
edges = ''
13421369
for vert in self.points:
13431370
v = self.coords[vert]
1344-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1371+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
13451372
v_vect = v_vect.replace('[', '(')
13461373
v_vect = v_vect.replace(']', ')')
1347-
tag = '%s' %v_vect
1374+
tag = '%s' % v_vect
13481375
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
13491376
coord = '\\coordinate %s at %s;\n' % (tag, tag)
13501377
dict_drawing[vert] = node, coord, tag
@@ -1362,7 +1389,7 @@ def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis):
13621389
tikz_pic += '\t[scale=%f,\n' % scale
13631390
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
13641391
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1365-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1392+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
13661393
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
13671394
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
13681395

@@ -1480,10 +1507,10 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
14801507
edges = ''
14811508
for vert in self.points:
14821509
v = self.coords[vert]
1483-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1484-
v_vect = v_vect.replace('[','(')
1485-
v_vect = v_vect.replace(']',')')
1486-
tag = '%s' %v_vect
1510+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
1511+
v_vect = v_vect.replace('[', '(')
1512+
v_vect = v_vect.replace(']', ')')
1513+
tag = '%s' % v_vect
14871514
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
14881515
coord = '\\coordinate %s at %s;\n' % (tag, tag)
14891516
dict_drawing[vert] = node, coord, tag
@@ -1507,7 +1534,7 @@ def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color,
15071534
tikz_pic += '\tscale=%f,\n' % scale
15081535
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
15091536
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1510-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1537+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
15111538
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
15121539
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
15131540

@@ -1664,12 +1691,12 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
16641691

16651692
for vert in self.points:
16661693
v = self.coords[vert]
1667-
v_vect = str(['%.5f' % i for i in v]).replace('\'','')
1668-
v_vect = v_vect.replace('[','(')
1669-
v_vect = v_vect.replace(']',')')
1670-
tag = '%s' %v_vect
1694+
v_vect = str(['%.5f' % i for i in v]).replace('\'', '')
1695+
v_vect = v_vect.replace('[', '(')
1696+
v_vect = v_vect.replace(']', ')')
1697+
tag = '%s' % v_vect
16711698
node = "\\node[%s] at %s {};\n" % ('vertex', tag)
1672-
coord = '\\coordinate %s at %s;\n' %(tag, tag)
1699+
coord = '\\coordinate %s at %s;\n' % (tag, tag)
16731700
dict_drawing[vert] = node, coord, tag
16741701

16751702
# Separate the edges between back and front
@@ -1684,10 +1711,14 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
16841711

16851712
# The back edge has to be between two vertices in the Back
16861713
# AND such that the 2 facets touching them are in the Back
1687-
if index1 in back_vertices and index2 in back_vertices and len(H_v12)==2:
1688-
back_part += "\\draw[%s,back] %s -- %s;\n" % ('edge', dict_drawing[index1][2], dict_drawing[index2][2])
1714+
if index1 in back_vertices and index2 in back_vertices and len(H_v12) == 2:
1715+
back_part += "\\draw[%s,back] %s -- %s;\n" % ('edge',
1716+
dict_drawing[index1][2],
1717+
dict_drawing[index2][2])
16891718
else:
1690-
front_part += "\\draw[%s] %s -- %s;\n" % ('edge',dict_drawing[index1][2],dict_drawing[index2][2])
1719+
front_part += "\\draw[%s] %s -- %s;\n" % ('edge',
1720+
dict_drawing[index1][2],
1721+
dict_drawing[index2][2])
16911722

16921723
# Start to write the output
16931724
tikz_pic = ''
@@ -1701,7 +1732,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
17011732
tikz_pic += '\tscale=%f,\n' % scale
17021733
tikz_pic += '\tback/.style={loosely dotted, thin},\n'
17031734
tikz_pic += '\tedge/.style={color=%s, thick},\n' % edge_color
1704-
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color,opacity)
1735+
tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' % (facet_color, opacity)
17051736
tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' % vertex_color
17061737
tikz_pic += 'fill=%s!75!black,thick}]\n%%\n%%\n' % vertex_color
17071738

@@ -1737,7 +1768,7 @@ def _tikz_3d_in_3d(self, view, angle, scale, edge_color,
17371768
# Draw the vertices on top of the back-edges
17381769
tikz_pic += '%%\n%%\n%% Drawing vertices in the back\n%%\n'
17391770
for v in back_vertices:
1740-
if not v in front_vertices and v in dict_drawing:
1771+
if v not in front_vertices and v in dict_drawing:
17411772
tikz_pic += dict_drawing[v][0]
17421773

17431774
# Draw the facets in the front by going in cycles for every facet.

0 commit comments

Comments
 (0)