• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pim-book / programmers-introduction-to-mathematics / #987

pending completion
#987

push

travis-ci

GitHub
build(deps): bump decode-uri-component in /waves/javascript_demo

910 of 910 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/hyperbolic_tessellation/hyperbolic.py
1
"""Classes and function related to the Poincare disk model of hyperbolic
2
geometry.
3
"""
4

5
from geometry import Circle
1✔
6
from geometry import Line
1✔
7
from geometry import Point
1✔
8
from geometry import orientation
1✔
9
from geometry import circle_through_points_perpendicular_to_circle
1✔
10
import math
1✔
11

12

13
def compute_fundamental_triangle(tessellation_configuration):
1✔
14
    """Compute the vertices of the hyperbolic triangle with the following
15
    properties:
16

17
     - Vertex A lies at the origin.
18
     - Vertex C lies on the x-axis.
19
     - Vertex B is chosen so that angle CAB is pi / p, and (hyperbolic) angle
20
       ABC is pi / q.
21

22
    Derivation:
23

24
    The desired point is B = (b_x, b_y). This point is on the line
25
    L: y = tan(pi / p) x and on an unknown circle C perpendicular to the unit
26
    circle with center G = (g_x, 0).
27

28
    If A = (0, 0) and D = (d_x, 0) is the intersection of the line [AG] with C,
29
    then we need (hyperbolic) angle ABD to be pi / q. This is the same as
30
    requiring that the tangent line to C at B forms an angle of pi / q with the
31
    line between A and (cos(pi / q), sin(pi / q)), which has slope
32
    tan(pi / p + pi / q).
33

34
    The slope of a tangent line to circle C at point B is given by
35

36
        y'(B) = -(b_x - g_x) / b_y
37

38
    Setting y'(B) = tan(pi / p + pi / q) and writing b_y in terms of b_x gives
39

40
        tan(pi / p + pi / q) tan(pi / p) = (g_x - b_x) / b_x
41

42
    Or, letting Z = tan(pi / p + pi / q) tan(pi / p),
43

44
        b_x(Z + 1) = g_x.
45

46
    Next, we use the fact that C and the unit circle are orthogonal to get a
47
    relationship between their radii (pythagorean theorem):
48

49
        1^2 + r^2 = g_x^2, where r^2 = (b_x - g_x)^2 + tan(pi / p)^2 b_x^2
50

51
    substituting in g = b_x (Z + 1) and solving for b_x,
52

53
        b_x = sqrt(1 / (1 + 2Z - (tan(pi / p))^2))
54

55
    We can then solve for b_y, g, and d_x trivially.
56
    """
57
    p = tessellation_configuration.num_polygon_sides
1✔
58
    q = tessellation_configuration.num_polygons_per_vertex
1✔
59

60
    tan_p = math.tan(math.pi / p)
1✔
61
    Z = math.tan(math.pi / p + math.pi / q) * tan_p
1✔
62

63
    b_x = math.sqrt(1 / (1 + 2 * Z - tan_p ** 2))
1✔
64
    b_y = b_x * tan_p
1✔
65
    g_x = b_x * (Z + 1)
1✔
66
    d_x = g_x - math.sqrt(b_y ** 2 + (b_x - g_x) ** 2)
1✔
67

68
    A = Point(0, 0)
1✔
69
    B = Point(b_x, b_y)
1✔
70
    D = Point(d_x, 0)
1✔
71

72
    return [A, B, D]
1✔
73

74

75
class PoincareDiskLine(Circle):
1✔
76
    """A representation of a Line in the Poincare disk. Implements reflect so
77
    that it can operate as if it were a Line for the purpose of tessellation.
78
    """
79

80
    def reflect(self, point):
1✔
81
        """Reflect a point across this line."""
82
        return self.invert_point(point)
1✔
83

84

85
class PoincareDiskModel(Circle):
1✔
86
    def line_through(self, p1, p2):
1✔
87
        """Return a PoincareDiskLine through the two given points.
88

89
        If the two points are collinear with the center of the underlying
90
        Poincare disk model, return a Line or a VerticalLine, as appropriate.
91
        """
92
        if orientation(p1, p2, self.center) == 'collinear':
1✔
93
            return Line.through(p1, p2)
1✔
94
        else:
95
            circle = circle_through_points_perpendicular_to_circle(
1✔
96
                p1, p2, self)
97
            return PoincareDiskLine(circle.center, circle.radius)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc