Update Punya_adlan_Titik Dalam Segiempat.py
This commit is contained in:
parent
27706d6c0c
commit
79a0cbd602
@ -1,54 +1,55 @@
|
|||||||
|
print('Created by adlan')
|
||||||
import winsound
|
print('Created by adlan')
|
||||||
|
import winsound
|
||||||
frequency = 2500
|
|
||||||
duration = 1000
|
frequency = 2500
|
||||||
winsound.Beep(frequency, duration)
|
duration = 1000
|
||||||
print('Created by adlan')
|
winsound.Beep(frequency, duration)
|
||||||
print('')
|
print('Created by adlan')
|
||||||
|
print('')
|
||||||
def is_point_in_polygon(x, y, polygon):
|
|
||||||
n = len(polygon)
|
def is_point_in_polygon(x, y, polygon):
|
||||||
inside = False
|
n = len(polygon)
|
||||||
px, py = x, y
|
inside = False
|
||||||
|
px, py = x, y
|
||||||
for i in range(n):
|
|
||||||
j = (i + 1) % n
|
for i in range(n):
|
||||||
xi, yi = polygon[i]
|
j = (i + 1) % n
|
||||||
xj, yj = polygon[j]
|
xi, yi = polygon[i]
|
||||||
|
xj, yj = polygon[j]
|
||||||
intersect = ((yi > py) != (yj > py)) and \
|
|
||||||
(px < (xj - xi) * (py - yi) / ((yj - yi) + 1e-10) + xi)
|
intersect = ((yi > py) != (yj > py)) and \
|
||||||
if intersect:
|
(px < (xj - xi) * (py - yi) / ((yj - yi) + 1e-10) + xi)
|
||||||
inside = not inside
|
if intersect:
|
||||||
|
inside = not inside
|
||||||
return inside
|
|
||||||
|
return inside
|
||||||
def process_input(data_lines):
|
|
||||||
results = []
|
def process_input(data_lines):
|
||||||
for line in data_lines:
|
results = []
|
||||||
|
for line in data_lines:
|
||||||
line = line.replace(",", " ")
|
|
||||||
nums = list(map(float, line.strip().split()))
|
line = line.replace(",", " ")
|
||||||
|
nums = list(map(float, line.strip().split()))
|
||||||
|
|
||||||
zx, zy = nums[0], nums[1]
|
|
||||||
|
zx, zy = nums[0], nums[1]
|
||||||
|
|
||||||
polygon = [(nums[i], nums[i+1]) for i in range(2, len(nums), 2)]
|
|
||||||
|
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")
|
if is_point_in_polygon(zx, zy, polygon):
|
||||||
else:
|
results.append("YA")
|
||||||
results.append("TIDAK")
|
else:
|
||||||
return results
|
results.append("TIDAK")
|
||||||
|
return results
|
||||||
data_input = [
|
|
||||||
"1,1 2,2 4,2 4,4 2,4",
|
data_input = [
|
||||||
"0,0 0,5 8,5 8,0 0,0",
|
"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",
|
||||||
]
|
"0,0 0,5 8,5 8,0 0,0"
|
||||||
|
]
|
||||||
hasil = process_input(data_input)
|
|
||||||
for i, res in enumerate(hasil, 1):
|
hasil = process_input(data_input)
|
||||||
print(f"h#{i}: {res}")
|
for i, res in enumerate(hasil, 1):
|
||||||
|
print(f"h#{i}: {res}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user