Path: blob/master/src/sage/plot/hyperbolic_triangle.py
8815 views
"""1Triangles in hyperbolic geometry23AUTHORS:45- Hartmut Monien (2011 - 08)6"""7#*****************************************************************************8# Copyright (C) 2011 Hartmut Monien <[email protected]>,9#10# Distributed under the terms of the GNU General Public License (GPL)11#12# This code is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15# General Public License for more details.16#17# The full text of the GPL is available at:18#19# http://www.gnu.org/licenses/20#*****************************************************************************21from sage.plot.bezier_path import BezierPath22from sage.plot.colors import to_mpl_color23from sage.plot.misc import options, rename_keyword24from sage.rings.all import CC2526class HyperbolicTriangle(BezierPath):27"""28Primitive class for hyberbolic triangle type. See ``hyperbolic_triangle?``29for information about plotting a hyperbolic triangle in the complex plane.3031INPUT:3233- ``a,b,c`` - coordinates of the hyperbolic triangle in the upper34complex plane3536- ``options`` - dict of valid plot options to pass to constructor3738EXAMPLES:3940Note that constructions should use ``hyperbolic_triangle``::4142sage: from sage.plot.hyperbolic_triangle import HyperbolicTriangle43sage: print HyperbolicTriangle(0, 1/2, I, {})44Hyperbolic triangle (0.000000000000000, 0.500000000000000, 1.00000000000000*I)45"""46def __init__(self, A, B, C, options):47"""48Initialize HyperbolicTriangle:4950Examples::5152sage: from sage.plot.hyperbolic_triangle import HyperbolicTriangle53sage: print HyperbolicTriangle(0, 1/2, I, {})54Hyperbolic triangle (0.000000000000000, 0.500000000000000, 1.00000000000000*I)55"""56A, B, C = (CC(A), CC(B), CC(C))57self.path = []58self._hyperbolic_arc(A, B, True);59self._hyperbolic_arc(B, C);60self._hyperbolic_arc(C, A);61BezierPath.__init__(self, self.path, options)62self.A, self.B, self.C = (A, B, C)6364def _repr_(self):65"""66String representation of HyperbolicArc.6768TESTS::6970sage: from sage.plot.hyperbolic_triangle import HyperbolicTriangle71sage: HyperbolicTriangle(0, 1/2, I,{})._repr_()72'Hyperbolic triangle (0.000000000000000, 0.500000000000000, 1.00000000000000*I)'73"""74return "Hyperbolic triangle (%s, %s, %s)" % (self.A, self.B, self.C)7576def _hyperbolic_arc(self, z0, z3, first=False):77"""78Function to construct Bezier path as an approximation to79the hyperbolic arc between the complex numbers z0 and z3 in the80hyperbolic plane.81"""82if (z0-z3).real() == 0:83self.path.append([(z0.real(),z0.imag()), (z3.real(),z3.imag())])84return85z0, z3 = (CC(z0), CC(z3))86if z0.imag() == 0 and z3.imag() == 0:87p = (z0.real()+z3.real())/288r = abs(z0-p)89zm = CC(p, r)90self._hyperbolic_arc(z0, zm, first)91self._hyperbolic_arc(zm, z3)92return93else:94p = (abs(z0)*abs(z0)-abs(z3)*abs(z3))/(z0-z3).real()/295r = abs(z0-p)96zm = ((z0+z3)/2-p)/abs((z0+z3)/2-p)*r+p97t = (8*zm-4*(z0+z3)).imag()/3/(z3-z0).real()98z1 = z0 + t*CC(z0.imag(), (p-z0.real()))99z2 = z3 - t*CC(z3.imag(), (p-z3.real()))100if first:101self.path.append([(z0.real(), z0.imag()),102(z1.real(), z1.imag()),103(z2.real(), z2.imag()),104(z3.real(), z3.imag())]);105first = False106else:107self.path.append([(z1.real(), z1.imag()),108(z2.real(), z2.imag()),109(z3.real(), z3.imag())]);110111@rename_keyword(color='rgbcolor')112@options(alpha=1, fill=False, thickness=1, rgbcolor="blue", zorder=2, linestyle='solid')113114def hyperbolic_triangle(a, b, c, **options):115"""116Return a hyperbolic triangle in the complex hyperbolic plane with points117(a, b, c). Type ``?hyperbolic_triangle`` to see all options.118119INPUT:120121- ``a, b, c`` - complex numbers in the upper half complex plane122123OPTIONS:124125- ``alpha`` - default: 1126127- ``fill`` - default: False128129- ``thickness`` - default: 1130131- ``rgbcolor`` - default: 'blue'132133- ``linestyle`` - (default: ``'solid'``) The style of the line, which is134one of ``'dashed'``, ``'dotted'``, ``'solid'``, ``'dashdot'``, or ``'--'``,135``':'``, ``'-'``, ``'-.'``, respectively.136137EXAMPLES:138139Show a hyperbolic triangle with coordinates 0, `1/2+i\sqrt{3}/2` and140`-1/2+i\sqrt{3}/2`::141142sage: hyperbolic_triangle(0, -1/2+I*sqrt(3)/2, 1/2+I*sqrt(3)/2)143144A hyperbolic triangle with coordinates 0, 1 and 2+i and a dashed line::145146sage: hyperbolic_triangle(0, 1, 2+i, fill=true, rgbcolor='red', linestyle='--')147"""148from sage.plot.all import Graphics149g = Graphics()150g._set_extra_kwds(g._extract_kwds_for_show(options))151g.add_primitive(HyperbolicTriangle(a, b, c, options))152g.set_aspect_ratio(1)153return g154155156