0

I am working on a python code that has 2 options. Make calculations for distance and area from user input or make the calculations from default point values. For default point values, I got the code working. Nevertheless, I cannot get it to work for when user inputs the points. I currently read the inputs fine, but I cannot get to use them on the 2nd and 3rd class (Polyline and Polygon)

import math

#Point class
class Point():
    def __init__(self):
        self.x = 0
        self.y = 0
    def setXY(self,x,y):
        self.x = x
        self.y = y

#Class instantiation
p1 = Point()
p2 = Point()
p3 = Point()

val = 0

print("\n----Please select from the following options:----")
print("Enter 1 to set your point coordinates")
print("Enter 2 to use default point coordinates\n")

val = int(input("Enter your value: "))
if val == 1:
    #Set point values for P1
    p1_x = int(input("Enter x value for P1: "))
    p1_y = int(input("Enter y value for P1: "))
    #Set point values for P2
    p2_x = int(input("Enter x value for P2: "))
    p2_y = int(input("Enter y value for P2: "))
    #Set point values for P3
    p3_x = int(input("Enter x value for P3: "))
    p3_y = int(input("Enter y value for P3: "))
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Print points values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    print("Location of point P2 is: (",p2.x,",",p2.y,")")
    print("Location of point P3 is: (",p3.x,",",p3.y,")")
    print("\n")

    #Polyline class
    class Polyline():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    p1 = Polyline()
    p2 = Polyline()
    p3 = Polyline()
    #Set point values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    p1.setXY(p1.x,p1.y)
    p2.setXY(p2.x,p2.y)
    p3.setXY(p3.x,p3.y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = p1.getLength(p2)
    resultPoint2_3 = p2.getLength(p3)
    resultPoint1_3 = p1.getLength(p3)
    #Print distance values
    print("Length of the perimeter from P1 to P2 is: ", resultPoint1_2)
    print("Length of the perimeter from P2 to P3 is: ", resultPoint2_3)
    print("Length of the perimeter from P1 to P3 is: ", resultPoint1_3)
    print("\n")

    #Polygon class
    class Polygon():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    p1 = Polygon()
    p2 = Polygon()
    p3 = Polygon()
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = p1.getLength(p2)
    resultPoint2_3 = p2.getLength(p3)
    resultPoint1_3 = p1.getLength(p3)
    #Creating perimeter variable and setting its value according to the formula
    #Perimeter formula for 3 points polygon: length1 + length2 + length3
    perimeter = resultPoint1_2 + resultPoint2_3 + resultPoint1_3
    #Print perimeter value
    print("Perimeter of the polygon is: ", perimeter)
    print("\n")

Current output when input 3 for x and y values on all points:

Location of point P1 is: ( 3 , 3 )
Location of point P2 is: ( 3 , 3 )
Location of point P3 is: ( 3 , 3 )


Location of point P1 is: ( 0 , 0 )
Length of the perimeter from P1 to P2 is:  0.0
Length of the perimeter from P2 to P3 is:  0.0
Length of the perimeter from P1 to P3 is:  0.0


Perimeter of the polygon is:  0.0

As you can see, the 2nd and 3er class are not utilizing the values set on the first class.

5
  • You're using the same variables p1, p2, p3 for the Point and Polyline instances. So when you write p1.setXY(p1.x,p1.y), p1.x refers to the Polyline, not the Point. Commented Sep 24, 2021 at 22:41
  • Did you really intend to define the Polyline and Polygon classes inside the if statement? Usually functions and classes are defined at top-level. Commented Sep 24, 2021 at 22:42
  • @Barmar yes, I did. I understand your point, but for this project I found it necessary. Commented Sep 24, 2021 at 22:43
  • @Barmar, would you please share in code the solution suggested? Commented Sep 24, 2021 at 22:44
  • Hard to imagine there ever being a good reason for defining functions and classes in a branched statement... Commented Sep 24, 2021 at 23:01

1 Answer 1

1

You need to use different variables for the points, polylines, and polygons. Otherwise, when you write something like

p1.setXY(p1.x, p1.y)

this is getting p1.x and p1.y from the Polygon that you're trying to update, not the Point that you created earlier.

In the code below, p1, p2, p3 are Points, pl1, pl2, pl3 are Polylines, and pg1, pg2, pg3 are Polygons.

import math

#Point class
class Point():
    def __init__(self):
        self.x = 0
        self.y = 0
    def setXY(self,x,y):
        self.x = x
        self.y = y

#Class instantiation
p1 = Point()
p2 = Point()
p3 = Point()

val = 0

print("\n----Please select from the following options:----")
print("Enter 1 to set your point coordinates")
print("Enter 2 to use default point coordinates\n")

val = int(input("Enter your value: "))
if val == 1:
    #Set point values for P1
    p1_x = int(input("Enter x value for P1: "))
    p1_y = int(input("Enter y value for P1: "))
    #Set point values for P2
    p2_x = int(input("Enter x value for P2: "))
    p2_y = int(input("Enter y value for P2: "))
    #Set point values for P3
    p3_x = int(input("Enter x value for P3: "))
    p3_y = int(input("Enter y value for P3: "))
    #Set point values
    p1.setXY(p1_x,p1_y)
    p2.setXY(p2_x,p2_y)
    p3.setXY(p3_x,p3_y)
    #Print points values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    print("Location of point P2 is: (",p2.x,",",p2.y,")")
    print("Location of point P3 is: (",p3.x,",",p3.y,")")
    print("\n")

    #Polyline class
    class Polyline():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    pl1 = Polyline()
    pl2 = Polyline()
    pl3 = Polyline()
    #Set point values
    print("Location of point P1 is: (",p1.x,",",p1.y,")")
    pl1.setXY(p1.x,p1.y)
    pl2.setXY(p2.x,p2.y)
    pl3.setXY(p3.x,p3.y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = pl1.getLength(p2)
    resultPoint2_3 = pl2.getLength(p3)
    resultPoint1_3 = pl1.getLength(p3)
    #Print distance values
    print("Length of the perimeter from P1 to P2 is: ", resultPoint1_2)
    print("Length of the perimeter from P2 to P3 is: ", resultPoint2_3)
    print("Length of the perimeter from P1 to P3 is: ", resultPoint1_3)
    print("\n")

    #Polygon class
    class Polygon():
        def __init__(self):
            self.x = 0
            self.y = 0
        def setXY(self,x,y):
            self.x = x
            self.y = y

    #getLength() method for distance calculation
    #Distance formula: root square of[(x2-x1)^2 + (y2-y1)^2]
        def getLength(self, p):
            return math.sqrt((self.x-p.x)**2+(self.y-p.y)**2)

    #Class instantiation
    pg1 = Polygon()
    pg2 = Polygon()
    pg3 = Polygon()
    #Set point values
    pg1.setXY(p1_x,p1_y)
    pg2.setXY(p2_x,p2_y)
    pg3.setXY(p3_x,p3_y)
    #Creating variables and setting their value to their respective distance
    resultPoint1_2 = pg1.getLength(p2)
    resultPoint2_3 = pg2.getLength(p3)
    resultPoint1_3 = pg1.getLength(p3)
    #Creating perimeter variable and setting its value according to the formula
    #Perimeter formula for 3 points polygon: length1 + length2 + length3
    perimeter = resultPoint1_2 + resultPoint2_3 + resultPoint1_3
    #Print perimeter value
    print("Perimeter of the polygon is: ", perimeter)
    print("\n")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.