Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 19

Tell us what’s happening:

i don’t know how i’d repeat this four times… i try to make comparisons between this and the previous code as i realise all we are doing here is shorting it and making the code more organised

Your code so far

import math

GRAVITATIONAL_ACCELERATION = 9.81
PROJECTILE = "∙"
x_axis_tick = "T"
y_axis_tick = "⊣"

class Projectile:
    __slots__ = ('__speed', '__height', '__angle')

    def __init__(self, speed, height, angle):
        self.__speed = speed
        self.__height = height
        self.__angle = math.radians(angle)
        
    def __str__(self):
        return f'''
Projectile details:
speed: {self.speed} m/s
height: {self.height} m
angle: {self.angle}°
displacement: {round(self.__calculate_displacement(), 1)} m
'''

    def __calculate_displacement(self):
        horizontal_component = self.__speed * math.cos(self.__angle)
        vertical_component = self.__speed * math.sin(self.__angle)
        squared_component = vertical_component**2
        gh_component = 2 * GRAVITATIONAL_ACCELERATION * self.__height
        sqrt_component = math.sqrt(squared_component + gh_component)
        
        return horizontal_component * (vertical_component + sqrt_component) / GRAVITATIONAL_ACCELERATION
        
    def __calculate_y_coordinate(self, x):
        height_component = self.__height
        angle_component = math.tan(self.__angle) * x
        acceleration_component = GRAVITATIONAL_ACCELERATION * x ** 2 / (
                2 * self.__speed ** 2 * math.cos(self.__angle) ** 2)
        y_coordinate = height_component + angle_component - acceleration_component

        return y_coordinate
    
    def calculate_all_coordinates(self):
        return [
            (x, self.__calculate_y_coordinate(x))
            for x in range(math.ceil(self.__calculate_displacement()))
        ]

    @property
    def height(self):
        return self.__height

    @property
    def angle(self):
        return round(math.degrees(self.__angle))

    @property
    def speed(self):
        return self.__speed

    @height.setter
    def height(self, n):
        self.__height = n

    @angle.setter
    def angle(self, n):
        self.__angle = math.radians(n)

    @speed.setter
    def speed(self, s):
       self.__speed = s
    
    def __repr__(self):
        return f'{self.__class__}({self.speed}, {self.height}, {self.angle})'

class Graph:
    __slots__ = ('__coordinates')

    def __init__(self, coord):
        self.__coordinates = coord

    def __repr__(self):
        return f"Graph({self.__coordinates})"

    def create_coordinates_table(self):
        table = '\n  x      y\n'
        for x, y in self.__coordinates:
            table += f'{x:>3}{y:>7.2f}\n'

        return table


# User Editable Region

    def create_trajectory(self):
        
        rounded_coords = [(round(x), round(y)) for x,y in self.__coordinates]
        
        x_max = max(rounded_coords, key=lambda i: i[0])[0]
        y_max = max(rounded_coords, key=lambda j: j[1])[1]
        
        matrix_list = [[' ' for _ in rounded_coords]]

        return matrix_list
        

# User Editable Region


ball = Projectile(10, 3, 45)
print(ball)
coordinates = ball.calculate_all_coordinates()
graph = Graph(coordinates)
for row in graph.create_trajectory():
    print(row) 
   

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 19

no, here you are adding the dots that make the trajectory in the right places

what is it that you are repeating four times?

okay this is my understanding of this instruction we are meant to mark a maximum height i suppose via the projectile mark, i don’t know how to do this though, i have no idea how to implement it into my code like how the example shows, and also i don’t really know what i can do with the -Remember that a coordinates graph has the(0, 0) in the bottom left corner. instruction i suppose this suggests the projectile starts at a rest.

the projectile starts with the height, angle and speed determined when instantiating the class

the reminder of having (0,0) in the bottom left corner is because the 0,0 for the list of list would be the first element in the first list, so top left corner

you already have the x_max and y_max, you calculated them a few steps ago

you need to use the coordinates in rounded_coords

this list is what should determine where to put the dots in the list of lists.
For example, you have the list of lists, how would you add a dot in (3,3) and (4,4)?

btw, you deleted a part of matrix_list, you may want to reset the step to restore it.

Add your new code below that, before the return statement

i don’t understand what it means by the elements tho, is that referring to a specific part of my code?

elements or items is a way to call the things that are held in a list

okay that helps i don’t know what to use here to make it match the example.

matrix_list = [[ PROJECTILE for _ in rounded_coords] for _ in  rounded_coords]

what is your plan? don’t throw code on the page guessing randomly

my plan is to change the elements in matrix_list at the coordinates, i’ve done that, now im trying to change them to the symbol of the PROJECTILE, i need to match the coordinates at the example, i don’t really know what they represent

matrix_list = [[ (" ",PROJECTILE) for _ in rounded_coords] for _ in  rounded_coords]
i've changed it a bit now while reading over what i said but its really messy

what have you done?

the list of coordinates is the dots that make the trajectory of the projectile, you need to add them to the matrix_list as a step in drawing the trajectory

do not change the line where matrix_list is created, add new code below that instead that add for each coordinate a dot in the matrix_list

but it tells me to change the lines in matrix_list?

you need to update matrix_list, yes, but you can do that also after it being created. It can be done in different ways but I am not sure trying to change the list comprehension is one that is easily approachable.

My suggestion would be, iterate over the coordinates, and for each coordinate change the right item in matrix_list

so if you are talking about updating variables there is only one way of doing so and that is creating a new variable under the same name, whats the point then of keeping the original list?

it’s not a variable like a = 3 where you can onyl change it doing a = 4, it’s a list

my_list = [3, 4, 5, "monkey", 7]

replace the element so that it counts correctly, do you know how to do that?

My list of not understanding
-i’m confused on how to update the list you said you don’t update it like how you would with a variable.

  • You then said your suggestion would be to iterate over coordinates, i am now going to ask you a question which will solve most of these issues,
  • do i update the list via the iteration or something else becuase you said replace the element so that it counts correctly, do you know how to do that? and i don’t know what you mean replace the element so that it counts correctly

the list has the numbers from 3 to 7 but instead of 6 it has “monkey”, make it have a 6 instead.

okay ill just type 6 instead or make monkey = 6 above it

you can’t, you need to update the list in a new line

you can’t assign a value to a string


it looks like you are missing a lot on how to work with lists, I suggest you go back to https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-lambda-functions-by-building-an-expense-tracker/step-1 and you complete the first 8 steps