Python Forum
Building a class for polygons and triangles in 2-space
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a class for polygons and triangles in 2-space
#1
Im trying to build a class for polygons and triangle in 2-space. I have test calls which have the parameters set in them. Im writing code to grab any info I put in those test calls to work so if I were to change any of those parameters, then I should get the correct answer every time. Right now, I have written code that can grab all the information from the test calls and print out the perimeter of the polygon and the average edge length. I need help doing the same for finding the angle for the polygon. Also any help on the class try (pol): in getting the color, seeing if its an equilateral, it's signed area, and if its CCW. I've been having trouble figuring this out on my own and any help with any of these problems would be very much appreciated.

My Code:

import math

class Pol (object):

    """Pol is a closed polygon in 2-space."""

    def __init__ (self, vtx):

        """Initialize a closed polygon from a list of vertices.
        Params: vtx (list of float 2-tuples) vertices, ordered around the bdry
        """
        self.vtx = vtx

    def __str__ (self):

        """str method
        Returns: (str) a string representation of the polygon
        """
        return str(self.vtx)

    def perimeter (self):

        """Sum of the lengths of the sides of the polygon.
        Returns: (float) perimeter
        """
        p = 0
        for i in range(len(self.vtx)):
            x1 = self.vtx[i-1][0]
            y1 = self.vtx[i-1][1]
            x2 = self.vtx[i][0]
            y2 = self.vtx[i][1]
            p += math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
        return p

    def avgEdgeLength (self):

        """
        Returns: (float) average edge length
        """
        return self.perimeter()/len(self.vtx)   #Code returns attribute error (AttributeError: 'Tri' object has no attribute 'vtx')

    def angle (self, i):

        """
        Params: i (int): vertex index (0 = first vertex)
        Returns: (float) angle, in degrees, at vertex i
        """

        pass

class Tri (Pol):

    """Tri is a triangle class."""

    def __init__ (self, A, B, C, rgbA, rgbB, rgbC):

        """
        Params:
            A,B,C (float 2-tuples): vertices of the triangle ABC
            rgbA, rgbB, rgbC (int 3-tuples): RGB colours of the vertices
                     colour range is [0,255]; e.g., 0 <= rgbA[i] <= 255
        """



        pass

    def __str__ (self):

        """
        Returns: (str) a string representation of the triangle
        """
        return str(self.A, self.B, self.C)  # will complain about 'pass' here: needs a string, not None

    def getColour (self, i):

        """
        Params: i (int): vertex index, 0<=i<=2
        Returns: (int 3-tuple) colour of ith vertex
        """
        return self.color[i]

    def isEquilateral (self, eps):

        """Test for equilateral triangle.
        Params: eps (float): allowable deviation in length
        Returns: (bool) is the triangle equilateral within eps?
                        (difference between min edge and max edge < eps?)
        """
        pass

    def signedArea (self):

        """
        Returns: (float) signed area of the triangle, +ve iff counterclockwise
        """
        pass

    def isCCW (self):

        """Counterclockwise orientation.
        ccw iff all (both) turns are left
        Returns: (bool) is the triangle oriented counterclockwise?
        """
        pass
Test calls:

import poly as poly

# this is only a start at testing the class: add more calls to debug

sq = poly.Pol ([(0.,0.), (1.,0.), (1.,1.), (0.,1.)])
print ('polygon:', sq)
print ('perimeter:', sq.perimeter())

t = poly.Tri ((0.,0.),
         (1.,0.),
         (.5, 1.),
         (179, 27, 27),
         (30, 107, 82),
         (244, 195,0))

print ('signed area:', t.signedArea())
print ('orientation:', 'ccw' if t.isCCW() else 'cw')
print ('angles:', t.angle(0), t.angle(1), t.angle(2))
print ('average edge length:', t.avgEdgeLength())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  building class zarize 4 2,324 Mar-10-2020, 08:11 AM
Last Post: zarize
  Building a class for sqlite3 interaction Nuzvee 5 3,244 Jan-29-2019, 06:47 AM
Last Post: Nuzvee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020