Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
2558 - Shootings
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Exemplu: = <code>shootings.in</code> 4 1 1 4 3 2 4 5 7 6 3 10 5 8 1 9 2 2 0 3 9 1 <code>shootings.out</code> 18 9 <syntaxhighlight lang="python" line="1"> def calculate_intersection_length(rect, ray_origin, direction): x1, y1, x2, y2 = rect x0, y0 = ray_origin if direction == "left": # Equation of the line: y = -(x - x0) + y0 (negative slope) intersections = [] # Check intersection with left side of the rectangle y = -(x1 - x0) + y0 if y1 <= y <= y2: intersections.append((x1, y)) # Check intersection with bottom side of the rectangle x = -(y1 - y0) + x0 if x1 <= x <= x2: intersections.append((x, y1)) # Check intersection with top side of the rectangle x = -(y2 - y0) + x0 if x1 <= x <= x2: intersections.append((x, y2)) elif direction == "right": # Equation of the line: y = (x - x0) + y0 (positive slope) intersections = [] # Check intersection with right side of the rectangle y = (x2 - x0) + y0 if y1 <= y <= y2: intersections.append((x2, y)) # Check intersection with bottom side of the rectangle x = (y1 - y0) + x0 if x1 <= x <= x2: intersections.append((x, y1)) # Check intersection with top side of the rectangle x = (y2 - y0) + x0 if x1 <= x <= x2: intersections.append((x, y2)) elif direction == "up": # Vertical line upwards intersections = [] # Check intersection with top side of the rectangle if x1 <= x0 <= x2: intersections.append((x0, y2)) # Check intersection with bottom side of the rectangle if x1 <= x0 <= x2: intersections.append((x0, y1)) # Calculate length of intersection segment if len(intersections) == 2: (xA, yA), (xB, yB) = intersections return ((xB - xA)**2 + (yB - yA)**2)**0.5 else: return 0 def calculate_shooting_cost(rectangles, shots): costs = [] for shot in shots: origin, direction = shot total_cost = 0 for rect in rectangles: total_cost += calculate_intersection_length(rect, origin, direction) costs.append(total_cost) return costs # Exemplu de utilizare rectangles = [ (1, 1, 4, 4), # dreptunghi definit prin (x1, y1, x2, y2) (5, 3, 7, 5) ] shots = [ ((2, 0), "left"), # tragere de la (2, 0) în direcția diagonală stânga ((6, 0), "right"), # tragere de la (6, 0) în direcția diagonală dreapta ((3, 0), "up") # tragere de la (3, 0) în direcția verticală în sus ] costs = calculate_shooting_cost(rectangles, shots) for i, cost in enumerate(costs): print(f"Costul tragerii {i+1}: {cost}") </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width