Ticket #14141: trac_14141-plot-edges-fs.patch

File trac_14141-plot-edges-fs.patch, 7.4 KB (added by saliola, 8 years ago)
  • sage/combinat/knutson_tao_puzzles.py

    # HG changeset patch
    # User Franco Saliola <saliola@gmail.com>
    # Date 1367425008 14400
    # Node ID 04466d78a476f743499a4a74a443585e93973ded
    # Parent  411348bd8c67d3d4ade40cccae5b9847099a850a
    [mq]: trac_14141-plot_edges-fs.patch
    
    diff --git a/sage/combinat/knutson_tao_puzzles.py b/sage/combinat/knutson_tao_puzzles.py
    a b class PuzzlePiece(object): 
    327327        else:
    328328            return Graphics()
    329329
    330     def _plot_piece(self, coords, border_color=(0.5,0.5,0.5), border_thickness=1):
     330    def _plot_piece(self, coords, border_color=(0.5,0.5,0.5), border_thickness=1, style='fill'):
    331331        """
    332332        TESTS::
    333333
    class PuzzlePiece(object): 
    335335            sage: delta = DeltaPiece('2','K','2')
    336336            sage: delta._plot_piece([(1,1),(1,2),(2,2)]) # not tested
    337337        """
    338         P = polygon(coords, color=self.color())
    339         P += polygon(coords, fill=False, color=border_color, thickness=border_thickness)
    340         return P
     338        if style == 'fill':
     339            P = polygon(coords, color=self.color())
     340            P += polygon(coords, fill=False, color=border_color, thickness=border_thickness)
     341            return P
     342        elif style == 'edges':
     343            if isinstance(self, DeltaPiece):
     344                edges = ('north_west', 'south', 'north_east')
     345            elif isinstance(self, NablaPiece):
     346                edges = ('south_west', 'north', 'south_east')
     347            else:
     348                edges = self.edges()
     349            P = Graphics()
     350            for (i, edge) in enumerate(edges):
     351                P += line([coords[i], coords[(i+1)%3]], color=self.edge_color(edge), thickness=border_thickness)
     352            return P
     353        else:
     354            return NotImplemented
     355
     356    def edge_color(self, edge):
     357        """
     358        Color of the specified edge of ``self`` (to be used when plotting the
     359        piece).
     360
     361        EXAMPLES::
     362
     363            sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece
     364            sage: delta = DeltaPiece('1','0','10')
     365            sage: delta.edge_color('south')
     366            'blue'
     367            sage: delta.edge_color('north_west')
     368            'red'
     369            sage: delta.edge_color('north_east')
     370            (1, 0.65, 0)
     371        """
     372        edge_label = self.edge_label(edge)
     373        colors = {'1': 'blue', '0': 'red'}
     374        if edge_label in colors:
     375            color = colors[edge_label]
     376        elif 'K' in edge_label:
     377            color = 'orange'
     378        elif edge_label.startswith('T'):
     379            color = 'yellow'
     380        else:
     381            color = 'white'
     382        return color
    341383
    342384    def edge_label(self, edge):
    343385        """
    class PartialPuzzle(object): 
    13711413            for k in range(d + 1):
    13721414                yield self[k + 1, self._n - d + k]
    13731415
    1374     def plot(self, labels=True, engine="fill triangles"):
     1416    def plot(self, labels=True, style="fill"):
    13751417        """
    13761418        Plots completed puzzle.
    13771419
    class PartialPuzzle(object): 
    13801422            sage: ps = PuzzleSolver("H")
    13811423            sage: puzzle = ps('0101','1001')[0]
    13821424            sage: puzzle.plot()  #not tested
    1383         """
    1384         if engine == "fill triangles":
    1385             return self._plot_fill_triangles(labels=labels)
    1386         elif engine == "color edges":
    1387             return self._plot_color_edges(labels=labels)
    1388 
    1389     def _plot_fill_triangles(self, labels=True):
    1390         """
    1391         Plots completed puzzle by filling triangles.
    1392 
    1393         EXAMPLES::
    1394 
    1395             sage: ps = PuzzleSolver("H")
    1396             sage: puzzle = ps('0101','1001')[0]
    1397             sage: puzzle._plot_fill_triangles()  #not tested
     1425            sage: puzzle.plot(style='fill')  #not tested
     1426            sage: puzzle.plot(style='edges')  #not tested
    13981427        """
    13991428        P = Graphics()
    14001429        coords = [(k, -d) for d in range(self._n) for k in range(-d, d + 1, 2)]
    14011430        for ((k, d), piece) in zip(coords, self):
    14021431            if isinstance(piece, RhombusPiece):
    14031432                for (i, triangle) in enumerate(piece):
    1404                     P += triangle._plot_piece([(k, d - 2*i), (k - 1, d - 1), (k + 1, d - 1)])
     1433                    P += triangle._plot_piece([(k, d - 2*i), (k - 1, d - 1), (k + 1, d - 1)], style=style)
    14051434                if labels:
    14061435                    P += piece._plot_label(piece['north_west'], (k - 0.5, d - 0.5), rotation=60)
    14071436                    P += piece._plot_label(piece['north_east'], (k + 0.5, d - 0.5), rotation=-60)
    14081437                    P += piece._plot_label(piece.north_piece()['south'], (k, d - 1))
    14091438            else:
    1410                 P += piece._plot_piece([(k, d), (k - 1, d - 1), (k + 1, d - 1)])
     1439                P += piece._plot_piece([(k, d), (k - 1, d - 1), (k + 1, d - 1)], style=style)
    14111440                if labels:
    14121441                    P += piece._plot_label(piece['north_west'], (k - 0.5, d - 0.5), rotation=60)
    14131442                    P += piece._plot_label(piece['north_east'], (k + 0.5, d - 0.5), rotation=-60)
    class PartialPuzzle(object): 
    14161445        P.axes(False)
    14171446        return P
    14181447
    1419     # FIXME: I don't think this method survived the refactoring
    1420     def _plot_color_edges(self, labels=True):
    1421         P = Graphics()
    1422         text_color = (0, 0, 0)
    1423         colors = {10: (1, 0.65, 0), 1: 'blue', 0: 'red'}
    1424         line_thickness = 3
    1425         coords = [(k, -d) for d in range(self._n) for k in range(-d, d + 1, 2)]
    1426         for ((k, d), piece) in zip(coords, self):
    1427             if isinstance(piece, RhombusPiece):
    1428                 triangle = piece.north_piece()
    1429                 P += line([(k, d), (k - 1, d - 1)], color=colors[triangle['north_west']], thickness=line_thickness)
    1430                 P += line([(k - 1, d - 1), (k + 1, d - 1)], color=colors[triangle['south']], thickness=line_thickness)
    1431                 P += line([(k + 1, d - 1), (k, d)], color=colors[triangle['north_east']], thickness=line_thickness)
    1432 
    1433                 triangle = piece.south_piece()
    1434                 P += line([(k, d - 2), (k - 1, d - 1)], color=colors[triangle['south_west']], thickness=line_thickness)
    1435                 P += line([(k - 1, d - 1), (k + 1, d - 1)], color=colors[triangle['north']], thickness=line_thickness)
    1436                 P += line([(k + 1, d - 1), (k, d - 2)], color=colors[triangle['south_east']], thickness=line_thickness)
    1437 
    1438                 if labels:
    1439                     P += text("%s" % piece['north_west'], (k - 0.5, d - 0.5), color=text_color, rotation=60)
    1440                     P += text("%s" % piece['north_east'], (k + 0.5, d - 0.5), color=text_color, rotation=-60)
    1441                     P += text("%s" % piece.north_piece()['south'], (k, d - 1), color=text_color)
    1442             else:
    1443                 P += line([(k, d), (k - 1, d - 1)], color=colors[piece['north_west']], thickness=line_thickness)
    1444                 P += line([(k - 1, d - 1), (k + 1, d - 1)], color=colors[piece['south']], thickness=line_thickness)
    1445                 P += line([(k + 1, d - 1), (k, d)], color=colors[piece['north_east']], thickness=line_thickness)
    1446                 if labels:
    1447                     P += text("%s" % piece['north_west'], (k - 0.5, d - 0.5), color=text_color, rotation=60)
    1448                     P += text("%s" % piece['north_east'], (k + 0.5, d - 0.5), color=text_color, rotation=-60)
    1449                     P += text("%s" % piece['south'], (k, d - 1), color=text_color)
    1450 
    1451         P.set_aspect_ratio(1.73)
    1452         P.axes(False)
    1453         return P
    1454 
    14551448    def _latex_(self):
    14561449        r"""
    14571450        Return latex version of ``self``.