Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Shooting Range by tarjeii
from math import acos, degrees, sqrt, radians, sin
def shot(*data):
"""
C
/\
/ \
b/ \a
/ \
A/________\B & F
c
- Find angle of wall, 'A'
- Find fire angle 'F' (and 'F2' which is fire angle compared to C)
- Hit angle on wall is: 180-F-A
- Distance from wall start to hit is then found
"""
# w1 is wall start, w2 is wall end
# f1 is bullet start, f2 is bullet after a specified length of time
w1,w2,f1,f2 = data
# 'a','b','c' are the lengths of the triangle formed by the wall and fire start point.
# 'b' equals the length of the wall
a = sqrt(abs(w2[0]-f1[0])**2 + abs(w2[1]-f1[1])**2)
b = sqrt(abs(w1[0]-w2[0])**2 + abs(w1[1]-w2[1])**2)
c = sqrt(abs(w1[0]-f1[0])**2 + abs(w1[1]-f1[1])**2)
# 'A' is the angle of the wall
# Uses 'The Law of Cosines
A = degrees(acos((b**2+c**2-a**2)/(2*b*c)))
# 'f' is the length of the bullet after a specified length of time
# 'bf' is the length from wall start to the end of 'f'
f = sqrt(abs(f1[0]-f2[0])**2 + abs(f1[1]-f2[1])**2)
bf = sqrt(abs(w1[0]-f2[0])**2 + abs(w1[1]-f2[1])**2)
af = sqrt(abs(w2[0]-f2[0])**2 + abs(w2[1]-f2[1])**2)
# 'B' is the angle at firing point to the top of wall
# Uses 'The Law of Cosines'
B = degrees(acos((a**2+c**2-b**2)/(2*a*c)))
# 'F' is the angle of the bullet.
# Uses 'The Law of Cosines'
F = degrees(acos((f**2+c**2-bf**2)/(2*f*c)))
# if fire angle is above wall
if round(F,4) > round(B,4):
return -1
# 'F2' is the angle of the bullet refered to the end of wall.
# Uses 'The Law of Cosines'
F2 = degrees(acos((f**2+a**2-af**2)/(2*f*a)))
# if fire angle is below wall
if round(F2,4) > round(B,4):
return -1
# 'bh' is where the bullet hits the wall
# Uses 'The Law of Sines'
Hit_angle_on_wall = 180 - A - F
bh = c*sin(radians(F))/sin(radians(Hit_angle_on_wall))
bull_eye = b/2
points = round((bull_eye-abs(bull_eye-bh))/bull_eye*100)
return points
April 26, 2016
Comments: