def drawImage(lineSegments, name): dim = 1600 svg = IV122Graphics.SVG("output/" + name + ".svg", dim, dim) #print lineSegments for line in lineSegments: svg.line(line[0][0][0] + dim / 2, line[0][1][0] + dim / 2, line[1][0][0] + dim / 2, line[1][1][0] + dim / 2) svg.close()
def gridInCircle(radius=200, delta=10): svg = IV122Graphics.SVG("output/gridInCircle.svg", radius * 2, radius * 2) x = radius y = radius for curY in range(-radius, radius, delta): distFromMiddle = math.sqrt((radius * 1.0)**2.0 - (curY * 1.0)**2) svg.line(x - distFromMiddle, y + curY, x + distFromMiddle, y + curY) svg.line(x + curY, y - distFromMiddle, x + curY, y + distFromMiddle) svg.close()
def closure(n): size = 400 img = IV122Graphics.SVG("output/closure.svg", size, size) points = generatePoints(n, size) img.setFill() for p in points: img.circle(5, p[0], p[1]) while (len(points) > 0): startPoint = leftMostPoint(points) img.setFill("Blue") img.circle(10, startPoint[0], startPoint[1]) img.setFill() #doprav adolu cond = lambda startPoint, point: (point[0] > startPoint[0] and point[1] > startPoint[1]) computation = lambda startPoint, point: float(point[0] - startPoint[ 0]) / float((point[1] - startPoint[1])) startPoint = partialConvexClosure(startPoint, points, cond, computation, img) #doprava nahoru cond = lambda startPoint, point: (point[0] > startPoint[0] and point[1] < startPoint[1]) computation = lambda startPoint, point: float(startPoint[1] - point[ 1]) / float(point[0] - startPoint[0]) startPoint = partialConvexClosure(startPoint, points, cond, computation, img) #doleva nahoru cond = lambda startPoint, point: (point[0] < startPoint[0] and point[1] < startPoint[1]) computation = lambda startPoint, point: float(startPoint[0] - point[ 0]) / float(startPoint[1] - point[1]) startPoint = partialConvexClosure(startPoint, points, cond, computation, img) #doleva dolu cond = lambda startPoint, point: (point[0] < startPoint[0] and point[1] > startPoint[1]) computation = lambda startPoint, point: float(point[1] - startPoint[ 1]) / float(startPoint[0] - point[0]) startPoint = partialConvexClosure(startPoint, points, cond, computation, img) img.close()
def linearRegression(inFile, outFile): inputData = open(inFile) points = [(float(line.split(" ")[0]), float(line.split(" ")[1])) for line in inputData.readlines()] size = 200 svg = IV122Graphics.SVG("output/" + outFile + ".svg", size, size) svg.resizeCoordinates(16, 16) svg.flipByX() svg.setMidInCenter() drawAxes(size, svg) analyticalLinRegression(points, svg) gridSearchLinRegression(points, svg) svg.close()
def pentagramAbsolute(): length = 100 points = [(50, 200)] n = 5 orientation = 90 svg = IV122Graphics.SVG("output/absolute.svg", 400, 400) for i in range(n - 1): #from turtle newX = length * math.sin(Commons.degToRad(orientation)) + points[-1][0] newY = length * math.cos(Commons.degToRad(orientation)) + points[-1][1] orientation += 360.0 / n points.append((newX, newY)) for target in points: svg.circle(20, target[0], target[1]) for i in range(0, len(points)): for target in points[i + 1:]: svg.line(points[i][0], points[i][1], target[0], target[1]) svg.close()
def drawClusters(name, minX, maxX, minY, maxY, points, centers): size = 200 svg = IV122Graphics.SVG("output/" + name + ".svg", size, size) imgMaxCoord = 2 * max([ minX, maxX, minY, maxY ]) #vsechny body se musi vlezt do obrazku, pulka je uprostred, takze *2 svg.resizeCoordinates(imgMaxCoord, imgMaxCoord) svg.flipByX() svg.setMidInCenter() drawAxes(size, svg) colors = [ "black", "red", "blue", "green", "yellow", "orange", "purple", "grey" ] #obviously, limited to 9 clusters for point in points: cluster = findNearestCluster(point, centers) print cluster, colors[cluster] svg.setFill(colors[cluster]) svg.setStroke(colors[cluster]) svg.circle(1, point[0], point[1]) svg.close()
def weirdGrid(): dim = 400 stepSize = 20 svg = IV122Graphics.SVG("output/curvyGrid.svg", dim, dim) coords = ((0, 1, 1), (0, 1, -1), (1, -1, 1), (1, -1, -1)) for quadrant in range(4): for i in range(11): svg.line((dim * coords[quadrant][0]) + coords[quadrant][1] * stepSize * i, dim / 2, dim / 2, dim / 2 + coords[quadrant][2] * stepSize * i) #naive #for i in range(11): #svg.line(stepSize*i ,dim/2,dim/2 , dim/2 - stepSize*i) #for i in range(11): #svg.line(dim - stepSize*i ,dim/2,dim/2 , dim/2 + stepSize*i) #for i in range(11): #svg.line(dim - stepSize*i ,dim/2,dim/2 , dim/2 - stepSize*i) #for i in range(11): #svg.line(dim/2 ,stepSize*i,dim/2 - stepSize*i , dim/2) svg.close()
def intersections(n, name, normal=False): size = 400 img = IV122Graphics.SVG("output/" + name + ".svg", size, size) lines = generateLineSegments(n, size, normal) img.setFill() for line in lines: img.line(line[0][0], line[0][1], line[1][0], line[1][1]) for line1 in lines: for line2 in lines: if (line1 != line2): inter = Commons.computeLineIntersection( line1[0], line1[1], line2[0], line2[1]) if (inter == -1): continue img.setFill("Red") img.circle(3, inter[0], inter[1]) img.setFill() img.close()
def drawMaze(n, maze, name="graph"): rowCount = n / 2 + 1 sizeLen = 20.0 svg = IV122Graphics.SVG("output/" + name + ".svg", n * sizeLen, n * sizeLen) heightOfLevel = (sizeLen / 2.0) * 3.0**0.5 index = 0 for row in range(rowCount): for column in range(n - 2 * row): vertex = maze[index] vertexNeighbours = vertex.getNeighbours() left = None != findVertexByData(vertexNeighbours, (column - 1, row)) right = None != findVertexByData(vertexNeighbours, (column + 1, row)) verticalUp = None != findVertexByData(vertexNeighbours, (column + 1, row - 1)) verticalDown = None != findVertexByData(vertexNeighbours, (column - 1, row + 1)) xoffset = row * (heightOfLevel / 2) ycoord = heightOfLevel * row xcoord = xoffset + sizeLen * ((column + 1) // 2) print vertex, vertexNeighbours, left, right, verticalUp, verticalDown, xcoord, ycoord if (column % 2 == 0): #spicka dole if (verticalUp): svg.line(xcoord, ycoord, xcoord + sizeLen, ycoord) if (right): svg.line(xcoord + sizeLen, ycoord, xcoord + (sizeLen / 2), ycoord + heightOfLevel) if (left): svg.line(xcoord + (sizeLen / 2), ycoord + heightOfLevel, xcoord, ycoord) else: if (right): svg.line(xcoord, ycoord, xcoord + sizeLen / 2, ycoord + heightOfLevel) if (verticalDown): svg.line(xcoord + sizeLen / 2, ycoord + heightOfLevel, xcoord - sizeLen / 2, ycoord + heightOfLevel) if (left): svg.line(xcoord - sizeLen / 2, ycoord + heightOfLevel, xcoord, ycoord) index = index + 1 print #vnejsi strany svg.line(0, 0, sizeLen * rowCount, 0) svg.line(sizeLen * rowCount, 0, sizeLen * rowCount / 2.0, rowCount * heightOfLevel) svg.line(sizeLen * rowCount / 2.0, rowCount * heightOfLevel, 0, 0) svg.line svg.close()