def test_1():
        p1 = Point(1, 2)
        p2 = Point(1, 3)
        assert geometry.distance(p1, p2) == 1

        p1 = Point(2.0, 1)
        p2 = Point(3.0, 1)
        assert geometry.distance(p1, p2) == 1
    def test_0():
        p1 = Point(1, 2)
        p2 = Point(3, 4)
        assert geometry.distance(p1, p2) == 2*(2**0.5)

        p1 = Point(4, -6)
        p2 = Point(-2, -5)
        assert geometry.distance(p2, p1) ==  (37**0.5)

        p1 = Point(1.3, 2.3)
        p2 = Point(1.4, 2.4)

        d_output = geometry.distance(p1, p2)
        d_actual = 0.1*(2**0.5)

        assert_array_almost_equal(d_output, d_actual, decimal=6)
Beispiel #3
0
    def _build_circle(a1, a2, circle_type, V, P = None, Q = None, S = None, power_factor = None, V_ref = None):

        k = (abs(V)**2)*abs(a1)/abs(a2)
        alpha = cmath.phase(a1)
        beta = cmath.phase(a2)

        if circle_type == "receiving_end":
            center = Point(-k*cmath.cos(alpha - beta), -k*cmath.sin(alpha - beta))

        elif circle_type == "sending_end":
            center = Point(k*cmath.cos(alpha -beta), -k*cmath.sin(alpha - beta))

        if V_ref != None and P != None and Q != None:
            radius = abs(V)*abs(V_ref)/(abs(a2))
            operation_point = Point(P, Q)

        elif V_ref != None and S != None:
            radius = abs(V)*abs(V_ref)/(abs(a2))
            operation_point = Point(S.real, S.imag)

        elif P != None and Q != None:
            radius = geometry.distance(center, Point(P, Q))
            operation_point = Point(P, Q)

        elif S != None:
            radius = geometry.distance(center, Point(S.real, S.imag))
            operation_point = Point(S.real, S.imag)

        elif P != None and power_factor != None:

            Q = P*cmath.sqrt(1/power_factor**2 - 1).real

            if power_factor < 0:
                Q = -Q

            radius = geometry.distance(center, Point(P, Q))
            operation_point = Point(P, Q)

        elif Q != None and power_factor != None:
            P = Q/cmath.sqrt(1/power_factor**2 - 1).real
            radius = geometry.distance(center, Point(P, Q))
            operation_point = Point(P, Q)

        else:
            raise AttributeError("Enought attributes to calculate not found")

        return radius, center, operation_point
    def __init__(self, *points: List[Point]):
        """Initialize the triangle."""
        if len(points) != 3:
            raise ValueError('Triangle must have 3 points')

        self.a = geometry.distance(points[0], points[1])
        self.b = geometry.distance(points[1], points[2])
        self.c = geometry.distance(points[0], points[2])

        self.l1 = geometry.line_equation(points[0], points[1])
        self.l2 = geometry.line_equation(points[1], points[2])
        self.l3 = geometry.line_equation(points[0], points[2])

        if not self.__is_valid():
            raise ValueError("Invalid triangle")
        else:
            self.points = points
Beispiel #5
0
def construct(p0: Point, p1: Point, p2: Point) -> Circle:
    """Return a circle which passes through the three points."""
    try:
        assert not geometry.colinear(p0, p1, p2)
    except AssertionError:
        raise AssertionError(
            "Circle can not be constructed from three points that are colinear"
        )

    l1 = geometry.perpendicular_bisector(p0, p1)
    l2 = geometry.perpendicular_bisector(p1, p2)

    center = l1.intersection(l2)
    radius = geometry.distance(center, p0)

    return Circle(center, radius)
Beispiel #6
0
    def intersetion(self, other) -> Union[Point, None]:
        """Return the intersection point of the circle and the other circle."""
        if isinstance(other, Circle):
            c1 = self.center
            c2 = other.center

            m = geometry.slope(c1, c2)
            theta = cmath.atan(m)

            d = geometry.distance(c1, c2)

            if d == self.radius + other.radius:
                """Two circles are touching each other"""
                x = c1.x + self.radius * cmath.cos(theta)
                y = c1.y + self.radius * cmath.sin(theta)
                return Point(x, y)

            elif d < self.radius + other.radius:
                """Two circles intersect"""
                r1 = self.radius
                r2 = other.radius

                theta = cmath.asin(r2 / d)

                x = c1.x + r1 * cmath.cos(theta)
                y = c1.y + r1 * cmath.sin(theta)

                p1 = Point(x, y)
                l = Line.construct(c1, c2)
                p2 = l.image(p1)

                return (p1, p2)
            else:
                return None

        else:
            raise ValueError("Can only intersect with another circle")
Beispiel #7
0
 def power(self, p: Point) -> float:
     """Return the power of the circle at point p."""
     return geometry.distance(self.center, p)**2 - self.radius**2