Exemplo n.º 1
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
'''
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'
Exemplo n.º 13
0
	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)
Exemplo n.º 14
0
'''
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
Exemplo n.º 15
0
        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)
Exemplo n.º 16
0
    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]
Exemplo n.º 17
0
    '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)
Exemplo n.º 18
0
	
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)
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'
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
# read lines from files and perform rotate, scale, translate on them
if len(files_list) > 0:
    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)
Exemplo n.º 22
0
'''
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'