1

I'm trying to implement my first class, partly as a way to break up the modeling and solving of a math problem I'm having trouble with. I don't think my problem relates to the class, but...?

The error keeps telling me that: "NameError: global name 'corner2' is not defined"

I tried moving the function call, but it still doesn't recognize it, so I put it back into the list declaration in my init function.

Here is my code:

class RotatedRectangle(object):

def corner1(a,b):
    a/=2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner2(a,b):
    a/=-2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner3(a,b):
    a/=-2
    b/=-2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner4(a,b):
    a/=2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def __init__(self, a, b,):
        """Return a Rotated rectangle object whose name is a function of a and b."""
        self.name = str(a) + "," + str(b) + "-rectangle"
        self.corners = [corner1(a,b), corner2(a,b), corner3(a,b), corner4(a,b)]



"""A rectangle with sides equal to even integers a and b is drawn on the          Cartesian plane.Its center (the intersection point of its diagonals) coincides with the point (0, 0),but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.

How many points with integer coordinates are located inside the given   rectangle (including on its sides)? """
Carl
  • 41
  • 9

3 Answers3

2

When defining methods for a class in Python, the first argument is usually set to "self". Then, when calling that method, prefix it with self.

Here is the working code:

import math

class RotatedRectangle(object):

    def corner1(self,a,b):
        a/=2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner2(self,a,b):
        a/=-2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner3(self,a,b):
        a/=-2
        b/=-2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner4(self,a,b):
        a/=2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def __init__(self, a, b,):
            """Return a Rotated rectangle object whose name is a function of a and b."""
            self.name = str(a) + "," + str(b) + "-rectangle"
            self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]
1

You have a few identation errors, and you forget to add the self parameter, it should be:

import math

class RotatedRectangle(object):
  def __init__(self, a, b,):
        """Return a Rotated rectangle object whose name is a function of a and b."""
        self.name = str(a) + "," + str(b) + "-rectangle"
        self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]

  def corner1(self,a,b):
      a/=2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner2(self,a,b):
      a/=-2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner3(self,a,b):
      a/=-2
      b/=-2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner4(self,a,b):
      a/=2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

I strongly recommend to you to read this question and all the answers What is the purpose of self?

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • Yeah, one thing I'm not loving about python. Copying and pasting seems to consistently cause indentation errors. Thank you for your response though. I will take on your suggested reading, it would seem there are parts of this class stuff I still didn't understand or wasn't familiar enough with. – Carl Jul 05 '17 at 12:45
  • @Carl, you have to do it step-by-step. Don't forget mark the question as accepted and voting if was useful :) – developer_hatch Jul 05 '17 at 12:50
1
  • You have to use self as the first parameter of your class methods
  • When calling class methods inside your class you have to pass self as their first parameter(so that the class knows that you are talking about the current object)
  • You have to indent when defining a method inside your class

    import math
    class RotatedRectangle(object):
        def corner1(self,a,b):
            a/=2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner2(self,a,b):
            a/=-2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner3(self,a,b):
            a/=-2
            b/=-2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner4(self,a,b):
            a/=2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def __init__(self, a, b,):
            """Return a Rotated rectangle object whose name is a function of a and b."""
            self.name = str(a) + "," + str(b) + "-rectangle"
            self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]
    
Farzad Vertigo
  • 2,458
  • 1
  • 29
  • 32