def draw_tri(L, H, x0, y0): p0 = Line_Point.Point(x0, y0) p1 = Line_Point.Point((x0 - (L / 2)), (y0 + H)) p2 = Line_Point.Point((x0 + (L / 2)), (y0 + H)) print 'line', Line_Point.Line(p0, p1) print 'line', Line_Point.Line(p1, p2) print 'line', Line_Point.Line(p2, p0) return
def load_line_file(file_object): line_objects = [] for line in file_object: # convert text line to a Line object line_object = line.split() point0 = Line_Point.Point(float(line_object[1]), float(line_object[2])) point1 = Line_Point.Point(float(line_object[3]), float(line_object[4])) line_object = Line_Point.Line(point0, point1) line_objects.append(line_object) return line_objects
def process_lines_file(file_object, options): for line in file_object: # convert L to a Line object L = line.split() point0 = Line_Point.Point(float(L[1]), float(L[2])) point1 = Line_Point.Point(float(L[3]), float(L[4])) line = Line_Point.Line(point0, point1) # rotate, scale, translate and write line count times for i in range(options['-n']): line.rotate(options['-a']) line.scale(options['-f']) line.translate(options['-x'], options['-y']) print 'line', line
def load_line_files(file_object): """Convert file_object to line_object.""" lines = [] for line in file_object: lineObject = line.split() point0 = Line_Point.Point(float(lineObject[1]), float(lineObject[2])) point1 = Line_Point.Point(float(lineObject[3]), float(lineObject[4])) try: colour = lineObject[5] except IndexError: colour = DEFCOLOUR lineObject = Line_Point.Line(point0, point1, colour) lines.append(lineObject) return lines
def generate_triangle(size): s = 3 triangle = [] central_angle = 2 * math.pi / s p0 = Line_Point.Point(0.0, size) while s > 0: p1 = Line_Point.Point(p0.x, p0.y) p1.rotate(central_angle) line = Line_Point.Line(p0, p1) triangle.append(line) p0 = p1 s = s - 1 return triangle
def trisect(line): trisected = [] trisected.append(line.point0) xdiff = line.point1.x - line.point0.x ydiff = line.point1.y - line.point0.y pointA = Line_Point.Point(line.point0.x + xdiff / 3, line.point0.y + ydiff / 3) trisected.append(pointA) pointB = Line_Point.Point(line.point0.x + 2 * xdiff / 3, line.point0.y + 2 * ydiff / 3) trisected.append(pointB) trisected.append(line.point1) return trisected
def process_lines_file(file_object, options): for line in file_object: # convert L to a Line object L = line.split() point0 = Line_Point.Point(float(L[1]), float(L[2])) point1 = Line_Point.Point(float(L[3]), float(L[4])) line = Line_Point.Line(point0, point1) colour = L[5] # rotate, scale, translate and write line count times for i in range(options['-n']): line.rotate(options['-a']) line.scale(options['-f']) line.translate(options['-x'], options['-y']) if options['-c'] > 0.0: jump_factor = int(options['-c']) oldc = colour colour = colour_options(jump_factor,oldc) print 'line', line, colour
def loadLineFile(fileObject, colour=DEFCOLOUR): """Convert lines of text from file object into list of Line objects. Returns: Line list - all line objects in file. """ lines = [] for line in fileObject: lineObject = line.split() point0 = Line_Point.Point(float(lineObject[1]), float(lineObject[2])) point1 = Line_Point.Point(float(lineObject[3]), float(lineObject[4])) try: colour = lineObject[5] except IndexError: colour = DEFCOLOUR lineObject = Line_Point.Line(point0, point1, colour) lines.append(lineObject) return lines
def __init__(self, size, lines=None): self.size = size if lines is None: point0 = Line_Point.Point(-size, size) point1 = Line_Point.Point(-size, -size) point2 = Line_Point.Point(size, -size) point3 = Line_Point.Point(size, size) line0 = Line_Point.Line(point0, point1) line1 = Line_Point.Line(point1, point2) line2 = Line_Point.Line(point2, point3) line3 = Line_Point.Line(point3, point0) self.lines = [line0, line1, line2, line3] else: line0 = Line_Point.Line(lines[0].point0, lines[0].point1) line1 = Line_Point.Line(lines[1].point0, lines[1].point1) line2 = Line_Point.Line(lines[2].point0, lines[2].point1) line3 = Line_Point.Line(lines[3].point0, lines[3].point1) self.lines = [line0, line1, line2, line3]
''' purpose for each line L in stdin repeat count times scale L about the origin by factor print L preconditions stdin contains a legal lines file ''' R = sys.argv[1:] try: delta_x = float(R[0]) delta_y = float(R[1]) count = float(R[2]) i = 0 for line in sys.stdin: #print line L = line.split() point0 = Line_Point.Point(float(L[1]), float(L[2])) point1 = Line_Point.Point(float(L[3]), float(L[4])) i = 0 while i < count: point0 = point0.translate(delta_x, delta_y) point1 = point1.translate(delta_x, delta_y) print 'line', point0, point1 i = i + 1 except ValueError: print >> sys.stderr, 'Syntax: translate.py delta_x delta_y count'
import Line_Point ''' purpose write to stdout a regular polygon with s sides and first vertex at (x0,y0) preconditions None ''' try: x0 = sys.argv[1] y0 = sys.argv[2] count = sys.argv[3] if type(x0) != float or type(y0) != float or type(count) != int: raise ValueError elif type(x0) == float and type(y0) == float and type(count) == int: print >> sys.stderr, 'hi' F = [] point0 = Line_Point.Point(x0, y0) F.append(point0) print >> sys.stderr, F for i in range(1, count): point0.rotate(2 * math.pi / count) F.append(point0) B = [] for j in range(0, count - 1): L1 = Line_Point.Line(F[j], F[j + 1]) B.append(L1) print >> sys.stderr, 'line' + ' ' + B[j] except ValueError: print >> sys.stderr, 'Syntax: generate_polygon.py x0 y0 s'
''' purpose write to stdout a star with s points and first center at (x0,y0) preconditions None ''' # process the command line arguments if len(sys.argv) != 4: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' x0 y0 s' sys.exit(1) try: x0 = float(sys.argv[1]) y0 = float(sys.argv[2]) s = int(sys.argv[3]) except ValueError: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' x0 y0 s' sys.exit(2) if s < 1: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' x0 y0 s' # generate s lines, each rotated by the central angle p0 = Line_Point.Point(x0, y0) p1 = Line_Point.Point(p0.x, p0.y + 25) central_angle = 2 * math.pi / s while s > 0: print('line', Line_Point.Line(p0, p1)) p1.rotate(central_angle) s = s - 1
recursive_draw(next_line(root, -0.5 * a + s, f), h - 1, a, f) recursive_draw(next_line(root, -0.75 * a + s, f), h - 1, a, f) recursive_draw(next_line(root, -a + s, f), h - 1, a, f) # ********** process the command line arguments if len(sys.argv) != 4: print >> sys.stderr, 'Syntax: ' + sys.argv[ 0] + ' height branch_number(>2||<9) branch_factor' sys.exit(1) try: height = int(sys.argv[1]) branch_number = int(sys.argv[2]) branch_factor = float(sys.argv[3]) except ValueError: print >> sys.stderr, 'Syntax: ' + sys.argv[ 0] + ' height branch_number(>2|| <9) branch_factor' sys.exit(2) if height < 1 or branch_factor <= 0: print >> sys.stderr, 'Syntax: ' + sys.argv[ 0] + ' height branch_number(>2||<9) branch_factor' sys.exit(3) # root: height 100, at bottom of canvas point0 = Line_Point.Point(0.0, -50.0) point1 = Line_Point.Point(0.0, 0.0) root = Line_Point.Line(point0, point1) recursive_draw(root, height, branch_number, branch_factor)
'MediumVioletRed', 'MidnightBlue', 'MintCream', 'MistyRose', 'Moccasin', 'NavajoWhite', 'Navy', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed', 'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed', 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple', 'RebeccaPurple', 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Salmon', 'SandyBrown', 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue', 'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'Tan', 'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White', 'WhiteSmoke', 'Yellow', 'YellowGreen' ] fun = False if colour == 'Fun': fun = True # root: length line_length, in middle of canvas point0 = Line_Point.Point(0.0, 0.0) point1 = Line_Point.Point(0.0, 0.0 + line_length) root = Line_Point.Line(point0, point1) map = ['R'] new_map = iterative_map(map, iterations) its = len(new_map) cs = len(colours) if fun: iterative_draw_c(root, its, line_length, cs, new_map, rotation_angle) else: iterative_draw(root, its, iterations, iterations, line_length, colour, new_map, rotation_angle)
# print root print 'line', root # continue with recursion recursive_draw( next_line(root, a, f), h-1, a, f ) recursive_draw( next_line(root, -a, f), h-1, a, f ) # ********** process the command line arguments if len(sys.argv) != 4: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' height branch_angle branch_factor' sys.exit(1) try: height = int(sys.argv[1]) branch_angle = float(sys.argv[2]) branch_factor = float(sys.argv[3]) except ValueError: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' height branch_angle branch_factor' sys.exit(2) if height < 1 or branch_factor <= 0: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + ' height branch_angle branch_factor' sys.exit(3) # root: height 100, at bottom of canvas point0 = Line_Point.Point(0.0,-250.0) point1 = Line_Point.Point(0.0,-150.0) root = Line_Point.Line(point0, point1) recursive_draw(root, height, branch_angle, branch_factor)
except ValueError: print >> sys.stderr, 'Syntax: ' + sys.argv[0] + 'Value Error' sys.exit(2) # generate first base triangle #x0 = float(sys.argv[1]) #y0 = float(sys.argv[2]) Ax = float(-250) Ay = float(-250) Cx = float(250) Cy = float(250) Bx = float(0) By = float(((Cx - Ax)*(math.tan((math.pi/3))) p0 = Line_Point.Point(Ax, Ay) p1 = Line_Point.Point(Bx, By) p2 = Line_Point.Point(Cx, Cy) print 'line', Line_Point.Line(p0, p1) print 'line', Line_Point.Line(p1, p2) print 'line', Line_Point.Line(p2, p0) ''' s = 3 x0 = 0 y0 = 250 central_angle = 2 * math.pi / s p0 = Line_Point.Point(x0, y0) while side > 0: p1 = Line_Point.Point(p0.x, p0.y)
def rotate_point(point0, point1): point_new = Line_Point.Point(point1.x - point0.x, point1.y - point0.y) point_new.rotate(-math.pi / 3) return Line_Point.Point(point0.x + point_new.x, point0.y + point_new.y)
''' purpose write to stderr a regular polygon with s sides and first vertex at (x0,y0) preconditions s > 2 x0 and y0 are of type float ''' L = sys.argv[1:] try: x0 = float(L[0]) y0 = float(L[1]) s = int(L[2]) if s <= 2: print >> sys.stderr, 'Syntax: generate_polygon.py x0 y0 s' central_angle = (2 * math.pi) / s vertex0 = Line_Point.Point(x0, y0) i = 0 while i < s: vertex1 = Line_Point.Point(vertex0.x, vertex0.y) vertex1 = vertex1.rotate(central_angle) line = Line_Point.Line(vertex0, vertex1) print 'line', print line vertex0 = vertex1 i = i + 1 except ValueError: print >> sys.stderr, 'Syntax: generate_polygon.py x0 y0 s'
tri_rec(n, L / 2, H / 2, x0, (y0 + (H / 2))) tri_rec(n, L / 2, H / 2, (x0 - (L / 2)), (y0 - (H / 2))) tri_rec(n, L / 2, H / 2, (x0 + (L / 2)), (y0 - (H / 2))) return # THIS IS THE MAIN METHOD # generate first base triangle Ax = float(-250) Ay = float(-250) Cx = float(250) Cy = float(-250) Bx = float(0) By = float(183) p0 = Line_Point.Point(Ax, Ay) p1 = Line_Point.Point(Bx, By) p2 = Line_Point.Point(Cx, Cy) print 'line', Line_Point.Line(p0, p1) print 'line', Line_Point.Line(p1, p2) print 'line', Line_Point.Line(p2, p0) # if n is greater than 1 draws first upside down triangle and then calls the tri_rec function if (n >= 1): Ax0 = ((Ax - Bx) / 2) Ay0 = (By + ((Ay - By) / 2)) Bx0 = ((Cx - Bx) / 2) By0 = (Cy + ((By - Cy) / 2)) Cx0 = ((Ax + Cx) / 2) Cy0 = (Cy + ((Ay - Cy) / 2))