MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / contains_origin

Function contains_origin

project_euler/problem_102/sol1.py:38–57  ·  view source on GitHub ↗

Check if the triangle given by the points A(x1, y1), B(x2, y2), C(x3, y3) contains the origin. >>> contains_origin(-340, 495, -153, -910, 835, -947) True >>> contains_origin(-175, 41, -421, -714, 574, -645) False

(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int)

Source from the content-addressed store, hash-verified

36
37
38def contains_origin(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int) -> bool:
39 """
40 Check if the triangle given by the points A(x1, y1), B(x2, y2), C(x3, y3)
41 contains the origin.
42 >>> contains_origin(-340, 495, -153, -910, 835, -947)
43 True
44 >>> contains_origin(-175, 41, -421, -714, 574, -645)
45 False
46 """
47 point_a: tuple[int, int] = (x1, y1)
48 point_a_to_b: tuple[int, int] = (x2 - x1, y2 - y1)
49 point_a_to_c: tuple[int, int] = (x3 - x1, y3 - y1)
50 a: float = -vector_product(point_a, point_a_to_b) / vector_product(
51 point_a_to_c, point_a_to_b
52 )
53 b: float = +vector_product(point_a, point_a_to_c) / vector_product(
54 point_a_to_c, point_a_to_b
55 )
56
57 return a > 0 and b > 0 and a + b < 1
58
59
60def solution(filename: str = "p102_triangles.txt") -> int:

Callers 1

solutionFunction · 0.85

Calls 1

vector_productFunction · 0.85

Tested by

no test coverage detected