@LevMckinney/

Projectile Distance Calculator

Python

Takes in some Values it will calculate the distance a projectle will go.

fork
loading
Files
  • main.py
  • graph.png
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import math as m
import matplotlib.pyplot as plt

#basic varibles I need to make calculations
Vi = float(input('launch velocity (m/s) ='))
Wi = (float(input('luanch angle (deg) ='))/180) * m.pi
A = float(input('frontal area of projectle (m^2) ='))
Cd = float(input('drag cofficent ='))
M = float(input('projectle mass (kg) ='))
if input('Is on Earth Near Sea Level y/n :') == 'n':
    d = float(input('air density (kg/m^3) ='))
    g = float(input('gravity (m/s^2) ='))
g = -9.807
d = 1.225
Pyi = float(input('hight off the groud (m) ='))
Dt = float(input('time step (recomended 0.01 to 1) (sec) ='))

#Getting wether not to plot
isGoingToPlt = input('shold it print a plot (y/n):') == 'y'

#list position to plot x, y
PPltx = []
PPlty = []
VPlt = []

#setting up the things we need to make a plot
fig = plt.figure()
graph = fig.add_subplot(111)

#function for decomponetizing a vector
def getMagnitude(x,y) :
    return m.sqrt(m.pow(x,2) + m.pow(y,2));

def getAngle(x, y) :
    return m.atan(y/x);

#function for componetizing a vector
def getXcomp(S, W) :
    return S * m.cos(W);

def getYcomp(S, W) :
    return S * m.sin(W);

#Intigrates between two points using somthing alot like a a trapizodal amproximation
def Tn(a, b) :
    return ((a + b)/2) * Dt;

#This are the to difernchal equation that difine the acseleration in x and y
#We get these equations by Solving the Formula for the Force of Drage for acseleration 
def Ax(V, W) :
    return ((Cd*d*A)/(2*M))*m.pow(V,2)* (-m.cos(W));

def Ay(V, W) :
    return ((Cd * d * A) / (2 * M)) * m.pow(V, 2) * (-m.sin(W)) + g;

#In this function I use an Eler amproximation + trapizodal amproximation to get position
#Then when the position gose below the "groud" i return the Distance or in other words the position in x
def Distance(V, W, Py) :
    
    #Setting up initail values for initail Velocity in X and Y
    Vx1 = getXcomp(V, W)
    Vy1 = getYcomp(V, W)
    
    Px = 0
    
    while Py >= 0:
        
        #Adding x and y positions to plot
        if isGoingToPlt:
            PPltx.append(Px)
            PPlty.append(Py)
            VPlt.append(V)
            
        #Setting up our X and Y of Velocity samples to use for integration later 
        Vx0 = Vx1
        Vy0 = Vy1
    
        #Gettign our X and Y Velocity from the acseleration equation
        #This is based on an Euler amproximation to get the next point
        Vx1 = Ax(V, W) * Dt + Vx0
        Vy1 = Ay(V, W) * Dt + Vy0
        
        #Summing positions to get next point
        Px += Tn(Vx0, Vx1)
        Py += Tn(Vy0, Vy1)
        
        #Decomponitizing Velosity back to and angle and a scaler
        V = getMagnitude(Vx1, Vy1)
        W = getAngle(Vx1, Vy1)
    
    #this just plots the projectles path
    if isGoingToPlt :
        #this part is just for making the nice gradent for the speed
        Vmax = max(VPlt)
        Vmin = min(VPlt)
        c = []
        for Vn in VPlt : 
            c.append([1 - (Vn - Vmin)/(Vmax - Vmin),(Vn - Vmin)/(Vmax - Vmin),0])
        #now we make the scatter plot with our Py and Px values and we use our speed color
        graph.scatter(PPltx, PPlty, color= c)
        #this makes it so that the axis of the plot are the same scale
        fig.gca().set_aspect('equal', adjustable='box')
        #save the plot to a file
        fig.savefig('graph.png', dpi = 1300)
        
    return Px;
#Call distance function give it its inital values and print its output
print('\n', 'Distance:', Distance(Vi,Wi,Pyi), 'm')