Learn Encapsulation by Building a Projectile Trajectory Calculator - Step 4

Tell us what’s happening:

just a general question, is your code meant to appear this messy? obviously when i put the right values in the code will look neater but im just interested if there is anyway i can write it better

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)
        

# User Editable Region

    def __calculate_displacement(self): 
        d = math.cos() * (self.__speed*math.sin()*__self.angle+math.sqrt(self.__speed**2 * math.sin()+2*GRAVITATIONAL_ACCELERATION*self.__height/GRAVITATIONAL_ACCELERATION))   


# User Editable Region

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 4

You can definitely break this up to make it easier to read:

d = math.cos() * 
    (self.__speed * math.sin() *  
    __self.angle +  math.sqrt(self.__speed**2 *
    math.sin()+2*GRAVITATIONAL_ACCELERATION*
    self.__height/GRAVITATIONAL_ACCELERATION))

Or break it up in a way that organizes it for you.

I’d suggest checking PEP 8, a guide for Python style. Here’s a good tip:

https://pep8.org/#break-before-or-after-binary-operator

Following the tradition from mathematics usually results in more readable code:

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

That is better. So more like:

d = math.cos() 
    * (self.__speed 
    * math.sin() 
    * __self.angle 
    +  math.sqrt(self.__speed**2 
    * math.sin()
    + 2
    * GRAVITATIONAL_ACCELERATION
    * self.__height/GRAVITATIONAL_ACCELERATION))

Good question, I learned something here :+1:

This is a pretty tough one to break up well. I might keep the brackets on one line but that’s a tough one.

d = math.cos() * 
    (self.__speed * math.sin() *  __self.angle 
    + math.sqrt(self.__speed**2 *
    math.sin() + 2*GRAVITATIONAL_ACCELERATION*
    self.__height/GRAVITATIONAL_ACCELERATION))

Maybe they show you something further in this project.

1 Like

you can also break it down in smaller operations, check the seed code in the next step

1 Like

yeah you are right they do it like this

        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`

ops sorry didn’t realise lol

I’ve blurred this solution. Please don’t post solutions into the forum. I’m just blurring this one instead of removing it since it’s relevant to this convo.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

In this case, I’m not sure anyone would guess to do it this way.

Did they break up formulas like this in earlier projects?

You can also use blur spoiler in certain situations where you are discussing the solution like this

Screenshot 2024-09-15 093451

1 Like

not that i know of. In the instructions it doesn’t mention doing it like that, it just says do that and put it into a variable.

As long as you get the formula correct it should pass the test. I guess as a reward you get to see this tidy structure later.

To understand that v⋅cosθ is the horizontal component and v⋅sinθ is the vertical component and divide it up this way would take a pretty good understanding of this math.

This gif is actually pretty good: https://en.wikipedia.org/wiki/Sine_and_cosine#Graph_of_a_function_and_its_elementary_properties

You can see how the max and min of the cosine correlates to the width of the circle.