Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code I was looking at for an assignment but I reached a part of it that I don't understand and I was trying to see if someone could explain it to me. Like what does each line of code do or something like that?


def magic(self, a, triangle):
    trianglex = triangle
    if (self.x > a.x):
        trianglex = 1.0 - triangle
    triangley = triangle
    if (self.y > a.y):
        triangley = 1.0 - triangle
    x = abs(self.x - a.x) * trianglex + min(self.x, a.x)
    y = abs(self.y - a.y) * triangley + min(self.y, a.y)
    return Point(x,y)


What I have tried:

I've tried to get other help on it but I haven't received much. Just the normal go back and look at the code and try to work through it but I'm still not understanding it. Any help would be greatly appreciated.
Posted
Updated 26-Oct-22 18:46pm

1 solution

Quote:
Like what does each line of code do

What each line of code does is obvious: none of them are complicated - simple compares and assignments mostly. The only vague complexity is the last two lines which do the "donkey work" and they are also self explanatory.

If you don't understand the individual lines, then you need to go back to the beginning of your Python course, and learn the basics properly because nobody is going to do a "line by line" explanation of basic code for you": Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line? Would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

A better question would be "what does this function do?" And to be honest, I have no idea whatsoever - mostly because I have no idea what values you are supposed to pass into the function as the triangle parameter. I could guess based on "it's probably 1.0 or -1.0" but even then I'm not sure what the function is supposed to used for - the name magic implies that it's not supposed to be anything particularly sensible.

So go back to wherever you got the code from, and look at it in context: when is it called, what parameters does it get, what is done with the result?
But don't expect line by line translations os basic code to happen or that they would necessarily be of any use if they did happen.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900