Nov-22-2019, 02:04 AM
(This post was last modified: Nov-22-2019, 02:06 AM by crosshairpvp.)
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:
Test calls:
My Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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()) |