pátek 27. června 2014

Object Oriented Programming of a Projectile

About month ago I started learning Python as my first computer programming language. Python was designed in 1991 by a computer scientist Guido van Rossum and developed into an open source project, that was evolving since then. It's beautiful to work with, easy to write and really efficient. Operations with lists, tuples, dictionaries, strings or any other types have amazing capabilities and Python can be used for many purposes as its multi paradigm language. 

Few days ago I firstly got in touch with object oriented programming. I tried to create a program, mapping the position (x, y) of a point (e.g. bullet) that was shot into the plane with initial angle and velocity and position, if we're not beginning in the origin (0, 0). It's a projectile and the results were x, y positions at certain times. I think it's a great program to practice object oriented programming.
source: http://www.techmania.cz/edutorium/art_exponaty.php?xkat=fyzika&xser=506f687962h&key=107

Important to mention that I am not calculating with air resistance and my gravitational acceleration is the Earth's g = 9,806 ms^-2.

The resulting program looked like this.

As I figured out few hours later, there is a simple technique of animating the point. It is using the turtle module of Python, which was created (in 1966) for kids to able to draw with programming language. It uses an arrow (a turtle) that has a certain position (x, y) and is automatically creating a path behind itself. So instead of printing the x and y position I set it to the turtle's position and therefore it is drawing for us. It's just few line added so everything written in red signifies the step of animation and usage of the Turtle module.

-------------------------------------

import math
import turtle

#A function calculation the y position at certain time through physics formula
def y_position(x, y, alpha, vel, time):
    alpha = math.sin(math.radians(alpha))
    g = 9.806
    new_y_position = y + vel * time * alpha - (1/2) * g * math.pow(time, 2)
    return new_y_position


#A function calculating the x position at a certain time with initial attributes
def x_position(x, y, alpha, vel, time):
    alpha = math.cos(math.radians(alpha))
    return x + vel * time * alpha


class Bullet:

    #Initializing object with its attributes: position (x,y), angle alpha, initial velocity
    def __init__(self, x, y, alpha, vel, time):
        self.x = x
        self.y = y
        self.alpha = alpha
        self.vel = vel
        self.time = time

    #Function returning x position of an object at a certain time
    def x_position_print(self):
        return x_position(self.x, self.y, self.alpha, self.vel, self.time)

    #Function returning y position of an object at a certain time
    def y_position_print(self):
        return y_position(self.x, self.y, self.alpha, self.vel, self.time)

    #Function mapping the trajectory above the ground of an object
    def shot(self):
        time = 0
        
        while y_position(self.x, self.y, self.alpha, self.vel, time) >= 0:

             now_position = [x_position(self.x, self.y, self.alpha, self.vel, time),
                                        y_position(self.x, self.y, self.alpha, self.vel, time),]

 print("x position: {0:.5}, y position: {1:.5}, time:                                                                               {2}".format(now_position[0],now_position[1],time))


             turtle.setposition(now_position[0], now_position[1])

             time += 1 #Setting the frequency of the mapping, in this case every second
        turtle.exitonclick()
        return 0

#Trying the program out.
point = Bullet(0, 0, 30, 100, 5) #An object, the initial time value 5 isn't important in the shot() func.

print(point.shot()) #Printing the shot function

print(point.x_position_print()) #Printing the x position function at time = 5 seconds

-------------------------------------------------------

The result of this certain program (print version, not the Turtle Graphics one) with the end experiment looked like this.

-------------------------------------------------------

x position: 0.0, y position: 0.0, time: 0
x position: 86.603, y position: 45.097, time: 1
x position: 173.21, y position: 80.388, time: 2
x position: 259.81, y position: 105.87, time: 3
x position: 346.41, y position: 121.55, time: 4
x position: 433.01, y position: 127.42, time: 5
x position: 519.62, y position: 123.49, time: 6
x position: 606.22, y position: 109.75, time: 7
x position: 692.82, y position: 86.208, time: 8
x position: 779.42, y position: 52.857, time: 9
x position: 866.03, y position: 9.7, time: 10
0
433.01270189221935 #The x position of the point at time = 5 seconds

-------------------------------------------------------


I set the program that it ends at the second before it lands. So this certain projectile lands somewhere between 10th and 11th seconds.

The end result of the turtle animation would look something like this (I was using different initial attributes of the shot/point)


I know it's not something special but it was definitely fun doing it. There are certainly more efficient ways of doing solving this problem, but this is the best I could think of.

I described the each steps and hopefully it is readable. 

Lukas Cerny, 17. 6. 2014

Žádné komentáře:

Okomentovat