Last active
March 3, 2020 19:12
-
-
Save wcdbmv/267c35d63c99dd6d48aa2d725f98ced5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
def __init__(self, x: float, y: float): | |
self.x = x | |
self.y = y | |
def __sub__(self, other: 'Point') -> 'Point': | |
return Point(self.x - other.x, self.y - other.y) | |
def skew_product(a: Point, b: Point) -> float: | |
return a.x * b.y - a.y * b.x | |
def point_in_triangle(p: Point, a: Point, b: Point, c: Point) -> bool: | |
s1 = skew_product(p - a, b - a) | |
s2 = skew_product(p - b, c - b) | |
s3 = skew_product(p - c, a - c) | |
return s1 >= 0 and s2 >= 0 and s3 >= 0 \ | |
or s1 <= 0 and s2 <= 0 and s3 <= 0 | |
def triangle_exists(a: Point, b: Point, c: Point) -> bool: | |
return abs(skew_product(b - a, c - a)) > 1e-9 | |
def main(): | |
points = [Point(*list(map(float, input(f'Input x{i} y{i}: ').split()))) for i in range(4)] | |
for i in range(4): | |
ip, ia, ib, ic = i, (i + 1) % 4, (i + 2) % 4, (i + 3) % 4 | |
p, a, b, c = points[ip], points[ia], points[ib], points[ic] | |
if triangle_exists(a, b, c) and point_in_triangle(p, a, b, c): | |
print(f'Point (x{ip}, y{ip}) is in triangle (x{ia}, y{ia}), (x{ib}, y{ib}), (x{ic}, y{ic})') | |
break | |
else: | |
print('There is no point in triangle') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment