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 koch(order, size): lines = generate_triangle(size) order = order - 1 while (order >= 0): new_lines = [] for i in range(len(lines)): trisected = trisect(lines[i]) line0 = Line_Point.Line(trisected[0], trisected[1]) rotated = rotate_point(trisected[1], trisected[2]) line1 = Line_Point.Line(trisected[1], rotated) line2 = Line_Point.Line(rotated, trisected[2]) line3 = Line_Point.Line(trisected[2], trisected[3]) new_lines = new_lines + [line0, line1, line2, line3] order = order - 1 lines = new_lines for x in lines: print "line ", x
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 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 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 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 next_line(root, a, f): # copy root new_line = Line_Point.Line(root.point0, root.point1) # translate to origin new_line.translate(-root.point0.x, -root.point0.y) # rotate and scale new_line.rotate(a) new_line.scale(f) # translate back new_line.translate(root.point0.x, root.point0.y) # translate tail to head of root new_line.translate(root.point1.x - root.point0.x, root.point1.y - root.point0.y) return new_line
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]
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 next_line(root, i, turn): # copy root new_line = Line_Point.Line(root.point0, root.point1) # translate to origin new_line.translate(-root.point0.x, -root.point0.y) # rotate and scale if turn[i-1] == 'R': new_line.rotate(-1.5708) else: new_line.rotate(1.5708) # translate back new_line.translate(root.point0.x, root.point0.y) # translate tail to head of root new_line.translate(root.point1.x - root.point0.x, root.point1.y - root.point0.y) return new_line
scale, rotate, and print lines preconditions ''' def Draw_Shape(L, n): if (n == 0): return for x in L: print 'line', x Draw_Shape(next_Shape(L), n - 1) # generate s lines, each rotated by the central angle sides = 2 * math.pi / s p0 = Line_Point.Point(x0, y0) L = [] for i in range(s): p1 = Line_Point.Point(p0.x, p0.y) p1.rotate(sides) x = Line_Point.Line(p0, p1) L.append(x) p0 = p1 Draw_Shape(L, 2)
''' 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)
# 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) p1.rotate(central_angle) print 'line', Line_Point.Line(p0, p1) p0 = p1 s = s - 1
for file in files_list: try: f = open(file, 'r') except IOError: print >> sys.stderr, 'Cannot open file:', file sys.exit() for x in f: x = x.replace('\n', '') # remove line terminator: Linux x = x.replace('\r\n', '') # remove line terminator: Windows with open(file) as open_file: file_data = open_file.readlines() for file_line in file_data: L = file_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) for i in range(count): line.rotate(angle) line.scale(factor) line.translate(delta_x, delta_y) print('line', line) # read lines from stdin and perform rotate, scale, translate on them else: for line in sys.stdin: 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) for i in range(count): line.rotate(angle)
''' 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'
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'