Titik_Dalam_Segiempat/Punya_adlan_Titik Dalam Segiempat.py

56 lines
1.2 KiB
Python

print('Created by adlan')
print('Created by adlan')
import winsound
frequency = 2500
duration = 1000
winsound.Beep(frequency, duration)
print('Created by adlan')
print('')
def is_point_in_polygon(x, y, polygon):
n = len(polygon)
inside = False
px, py = x, y
for i in range(n):
j = (i + 1) % n
xi, yi = polygon[i]
xj, yj = polygon[j]
intersect = ((yi > py) != (yj > py)) and \
(px < (xj - xi) * (py - yi) / ((yj - yi) + 1e-10) + xi)
if intersect:
inside = not inside
return inside
def process_input(data_lines):
results = []
for line in data_lines:
line = line.replace(",", " ")
nums = list(map(float, line.strip().split()))
zx, zy = nums[0], nums[1]
polygon = [(nums[i], nums[i+1]) for i in range(2, len(nums), 2)]
if is_point_in_polygon(zx, zy, polygon):
results.append("YA")
else:
results.append("TIDAK")
return results
data_input = [
"1,1 2,2 4,2 4,4 2,4",
"0,0 0,5 8,5 8,0 0,0",
"0,0 0,5 8,5 8,0 0,0"
]
hasil = process_input(data_input)
for i, res in enumerate(hasil, 1):
print(f"h#{i}: {res}")