Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/hyperbolic_triangle.py
4034 views
1
"""
2
Triangles in hyperbolic geometry
3
4
AUTHORS:
5
6
- Hartmut Monien (2011 - 08)
7
"""
8
#*****************************************************************************
9
# Copyright (C) 2011 Hartmut Monien <[email protected]>,
10
#
11
# Distributed under the terms of the GNU General Public License (GPL)
12
#
13
# This code is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
# General Public License for more details.
17
#
18
# The full text of the GPL is available at:
19
#
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
from sage.plot.bezier_path import BezierPath
23
from sage.plot.colors import to_mpl_color
24
from sage.plot.misc import options, rename_keyword
25
from sage.rings.all import CC
26
27
class HyperbolicTriangle(BezierPath):
28
"""
29
Primitive class for hyberbolic triangle type. See ``hyperbolic_triangle?``
30
for information about plotting a hyperbolic triangle in the complex plane.
31
32
INPUT:
33
34
- ``a,b,c`` - coordinates of the hyperbolic triangle in the upper
35
complex plane
36
37
- ``options`` - dict of valid plot options to pass to constructor
38
39
EXAMPLES:
40
41
Note that constructions should use ``hyperbolic_triangle``::
42
43
sage: from sage.plot.hyperbolic_triangle import HyperbolicTriangle
44
sage: print HyperbolicTriangle(0, 1/2, I, {})
45
Hyperbolic triangle (0.000000000000000, 0.500000000000000, 1.00000000000000*I)
46
"""
47
def __init__(self, A, B, C, options):
48
"""
49
Initialize HyperbolicTriangle:
50
51
Examples::
52
53
sage: from sage.plot.hyperbolic_triangle import HyperbolicTriangle
54
sage: print HyperbolicTriangle(0, 1/2, I, {})
55
Hyperbolic triangle (0.000000000000000, 0.500000000000000, 1.00000000000000*I)
56
"""
57
A, B, C = (CC(A), CC(B), CC(C))
58
self.path = []
59
self._hyperbolic_arc(A, B, True);
60
self._hyperbolic_arc(B, C);
61
self._hyperbolic_arc(C, A);
62
BezierPath.__init__(self, self.path, options)
63
self.A, self.B, self.C = (A, B, C)
64
65
def _repr_(self):
66
"""
67
String representation of HyperbolicTriangle.
68
"""
69
return "Hyperbolic triangle (%s, %s, %s)" % (self.A, self.B, self.C)
70
71
def _hyperbolic_arc(self, z0, z3, first=False):
72
"""
73
Function to construct Bezier path as an approximation to
74
the hyperbolic arc between the complex numbers z0 and z3 in the
75
hyperbolic plane.
76
"""
77
if (z0-z3).real() == 0:
78
self.path.append([(z0.real(),z0.imag()), (z3.real(),z3.imag())])
79
return
80
z0, z3 = (CC(z0), CC(z3))
81
if z0.imag() == 0 and z3.imag() == 0:
82
p = (z0.real()+z3.real())/2
83
r = abs(z0-p)
84
zm = CC(p, r)
85
self._hyperbolic_arc(z0, zm, first)
86
self._hyperbolic_arc(zm, z3)
87
return
88
else:
89
p = (abs(z0)*abs(z0)-abs(z3)*abs(z3))/(z0-z3).real()/2
90
r = abs(z0-p)
91
zm = ((z0+z3)/2-p)/abs((z0+z3)/2-p)*r+p
92
t = (8*zm-4*(z0+z3)).imag()/3/(z3-z0).real()
93
z1 = z0 + t*CC(z0.imag(), (p-z0.real()))
94
z2 = z3 - t*CC(z3.imag(), (p-z3.real()))
95
if first:
96
self.path.append([(z0.real(), z0.imag()),
97
(z1.real(), z1.imag()),
98
(z2.real(), z2.imag()),
99
(z3.real(), z3.imag())]);
100
first = False
101
else:
102
self.path.append([(z1.real(), z1.imag()),
103
(z2.real(), z2.imag()),
104
(z3.real(), z3.imag())]);
105
106
@rename_keyword(color='rgbcolor')
107
@options(alpha=1, fill=False, thickness=1, rgbcolor="blue", zorder=2, linestyle='solid')
108
109
def hyperbolic_triangle(a, b, c, **options):
110
"""
111
Return a hyperbolic triangle in the complex hyperbolic plane with points
112
(a, b, c). Type ``?hyperbolic_triangle`` to see all options.
113
114
INPUT:
115
116
- ``a, b, c`` - complex numbers in the upper half complex plane
117
118
OPTIONS:
119
120
- ``alpha`` - default: 1
121
122
- ``fill`` - default: False
123
124
- ``thickness`` - default: 1
125
126
- ``rgbcolor`` - default: 'blue'
127
128
- ``linestyle`` - default: 'solid'
129
130
EXAMPLES:
131
132
Show a hyperbolic triangle with coordinates 0, `1/2+i\sqrt{3}/2` and
133
`-1/2+i\sqrt{3}/2`::
134
135
sage: hyperbolic_triangle(0, -1/2+I*sqrt(3)/2, 1/2+I*sqrt(3)/2)
136
137
A hyperbolic triangle with coordinates 0, 1 and 2+i::
138
139
sage: hyperbolic_triangle(0, 1, 2+i, fill=true, rgbcolor='red')
140
"""
141
from sage.plot.all import Graphics
142
g = Graphics()
143
g._set_extra_kwds(g._extract_kwds_for_show(options))
144
g.add_primitive(HyperbolicTriangle(a, b, c, options))
145
g.set_aspect_ratio(1)
146
return g
147
148