def drawNpolygon(name, n, length): turtle = IV122Graphics.Turtle(name, length * n, length * n, length * n / 2, length * (n - 1)) for i in range(n): turtle.forward(length) turtle.left(360.0 / n) turtle.close()
def generateWeirdStrips(name, size): bitmap = IV122Graphics.BitMap("output/effects/" + name + ".jpg", size, size) stripSize = 20 radiusSize = 50 for i_x in range(-size / 2, size / 2): for i_y in range(-size / 2, size / 2): distanceFromMiddle = ((i_x**2 + i_y**2)**0.5) isInsideSquare = abs(i_x) < squareSize / 2 and abs( i_y) < squareSize / 2 if (isInsideSquare): colElement = 127 + Commons.cos( (360 / radiusSize) * (distanceFromMiddle % radiusSize)) * 127 else: colElement = 127 - Commons.cos( (360 / radiusSize) * (distanceFromMiddle % radiusSize)) * 127 color = (colElement, colElement, colElement) bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color) print "neco" bitmap.close()
def kochSnowflakeExecute(): snowFlake = IV122Graphics.Turtle("output/fractal/koch.svg", 1000, 1000, 400, 500) for i in range(3): kochSnowFlake(snowFlake, 400, 5) snowFlake.left(120) snowFlake.close()
def generateWeirdCircles(name, size): bitmap = IV122Graphics.BitMap("output/effects/" + name + ".jpg", size, size) squareSize = 100 black = (0, 0, 0) #initially black, but swaps during execution white = (0xFF, 0xFF, 0xFF) radiusSize = 50 for i_x in range(-size / 2, size / 2): for i_y in range(-size / 2, size / 2): distanceFromMiddle = ((i_x**2 + i_y**2)**0.5) isInsideSquare = abs(i_x) < squareSize / 2 and abs( i_y) < squareSize / 2 if (isInsideSquare): colElement = 127 + Commons.cos( (360 / radiusSize) * (distanceFromMiddle % radiusSize)) * 127 else: colElement = 127 - Commons.cos( (360 / radiusSize) * (distanceFromMiddle % radiusSize)) * 127 color = (colElement, colElement, colElement) bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color) print "neco" bitmap.close()
def juliusSet(): imgSize = 400 depth = 20 simpleHeuristic = 2 bitmap = IV122Graphics.BitMap("output/julii.jpg", imgSize, imgSize) for x in range(-imgSize / 2, imgSize / 2): for y in range(-imgSize / 2, imgSize / 2): z = complex(x / 100.0, y / 100.0) c = complex(-0.13, 0.75) currDepth = 0 while abs(z) < simpleHeuristic and currDepth < depth: z = nextMandelBrot(z, c) currDepth += 1 if abs(z) < simpleHeuristic: distane = abs(z) color = ((255.0 * distane) / simpleHeuristic) bitmap.putPixel(x + imgSize / 2, y + imgSize / 2, (color, 0, 0)) else: color = ((255.0 * currDepth) / depth) bitmap.putPixel(x + imgSize / 2, y + imgSize / 2, (0, color, 0)) bitmap.close()
def squaredRainbow(): ff = 255 bitmap = IV122Graphics.BitMap("output/squaredRainbow.jpg", ff, ff) for i in range(ff): for j in range(ff): bitmap.putPixel(i, j, (i, j, ff)) bitmap.close()
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 visualizeNsds(): size = 400 bitmapForSubstract = IV122Graphics.BitMap("output/substract.bmp", size, size) bitmapForModulo = IV122Graphics.BitMap("output/modulo.bmp", size, size) for i in range(1, size): for j in range(1, size): stepCountSubstract = nsdSubstract(i, j, True) stepCountMod = nsdMod(i, j, True) print((i, j, stepCountSubstract, stepCountMod)) #colorSubstract = (255- stepCountSubstract%255,255- (stepCountSubstract/255)%255,255- ((stepCountSubstract/255)/255)%255) #colorMod = (255- stepCountMod%255,255- (stepCountMod/255)%255,255- ((stepCountMod/255)/255)%255) colorSubstract = (255 - stepCountSubstract, 0, 0) colorMod = (255 - stepCountMod, 0, 0) bitmapForSubstract.putPixel(i, size - j, colorSubstract) bitmapForModulo.putPixel(i, size - j, colorMod) bitmapForSubstract.close() bitmapForModulo.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 spiral(name, x, y, r, full=0): size = 500 bitmap = IV122Graphics.BitMap("output/shapes/" + name + ".jpg", size, size) #todo zmenit 20 na parametrizovatleny rozestup for t in range(360 * 5): i_x = (t / 20) * math.sin(Commons.degToRad((t))) + x i_y = (t / 20) * math.cos(Commons.degToRad((t))) + y bitmap.putPixel(i_x, i_y, (100, 100, 100)) bitmap.close()
def drawFilledPolygon(points, name, size): bitmap = IV122Graphics.BitMap("output/" + name + ".jpg", size, size) resultLineSegments = [] for i in range(len(points)): resultLineSegments.append((points[i - 1], points[i])) print resultLineSegments for i_x in range(size): for i_y in range(size): intersetCount = countIntersect((i_x, i_y), resultLineSegments) if (intersetCount % 2 == 1): bitmap.putPixel(i_x, i_y, (100, 100, 100)) bitmap.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 chaosgame(path, ngon, ratio): imgSize = 800 jpeg = IV122Graphics.BitMap(path, imgSize, imgSize) outerPoints = generateNGonPoint(ngon, imgSize / ngon) stepCount = 10000 point = getRandomPoint(imgSize) for i in range(stepCount): chosenOrigin = random.randint(0, ngon - 1) point = computeMidPoint(outerPoints[chosenOrigin], point, ratio) if (i > 100): jpeg.putPixel(point[0], point[1], (255, 0, 0)) jpeg.close()
def pentagramRelative(): turtle = IV122Graphics.Turtle("output/pentaInPenta.svg", 400, 400, 50, 200) n = 5 length = 100 for i in range(n): turtle.forward(length) turtle.left(360.0 / n) turtle.left(360.0 / n + 180 + (180 - 360.0 / (2 * n))) a = length * math.sin(Commons.degToRad(360.0 / n)) b = length * math.cos(Commons.degToRad(360.0 / n)) length = math.sqrt((100 + b)**2 + a**2) for i in range(n): turtle.forward(length) turtle.left(180 - (360.0 / n) / 2) turtle.close()
def squareInception(size=200.0, percent=20.0, nesting=10.0): turtle = IV122Graphics.Turtle("output/squareInception.svg", 400, 400, 50, 300) curSize = size for level in range(nesting): for i in range(4): turtle.forward(curSize) turtle.left(90) turtle.forward(curSize * percent / 100.0) a = (curSize * percent / 100.0) b = (curSize * (1 - percent / 100.0)) angle = Commons.radToDeg(math.atan(a / b)) turtle.left(angle) curSize = math.sqrt(a**2 + b**2) turtle.close()
def mandelBrot(imgSize, cutX, cutY, cutSize, name="mandelbrot"): #imgsize resaclovat na ctverec -2, 2 #cutX = -1 #cutY = -1 #cutSize = imgSize/4.0 #multiply something by cutSize, add cutX to something, add cutY to something scaleFactor = imgSize / (4.0) scaleFactor2 = imgSize / cutSize #scaleFactorX = imgSize / (4.0 * (-2.0/cutX)) # #scaleFactorY = imgSize / (4.0 * (-2.0/cutY)) # bullshit # scaledX cutX # -2 = 0 # -1 = +1 # 0 +2 # 1 +3 # 2 +4 depth = 20 simpleHeuristic = 2 bitmap = IV122Graphics.BitMap("output/" + name + ".jpg", imgSize, imgSize) for x in range(-imgSize / 2, imgSize / 2): for y in range(-imgSize / 2, imgSize / 2): z = complex(0, 0) scaledX = x / scaleFactor scaledY = y / scaleFactor c = complex((scaledX / scaleFactor2) + cutX - 2, (scaledY / scaleFactor2) + cutY - 2) currDepth = 0 while abs(z) < simpleHeuristic and currDepth < depth: z = nextMandelBrot(z, c) currDepth += 1 if abs(z) < simpleHeuristic: distane = abs(z) color = ((255.0 * distane) / simpleHeuristic) bitmap.putPixel(x + imgSize / 2, y + imgSize / 2, (color, 0, 0)) else: color = ((255.0 * currDepth) / depth) bitmap.putPixel(x + imgSize / 2, y + imgSize / 2, (0, color, 0)) bitmap.close()
def circle(name, x, y, r, full=0): size = 500 bitmap = IV122Graphics.BitMap("output/shapes/" + name + ".jpg", size, size) thickness = 300.0 for i_x in range(size): for i_y in range(size): if (full == 0): if abs((float(i_x) - x)**2.0 + (i_y - y)**2 - r**2) <= thickness: bitmap.putPixel(i_x, i_y, (100, 100, 100)) else: if abs((float(i_x) - x)**2.0 + (i_y - y)**2) <= thickness + r * r: bitmap.putPixel(i_x, i_y, (100, 100, 100)) bitmap.close()
def triangleInception(size=200, d=10): turtle = IV122Graphics.Turtle("output/triangleInception.svg", 400, 400, 50, 300) curSize = size for i in range(size / (d * 2)): for i in range(3): turtle.forward(curSize) turtle.left(120) turtle.penUp() turtle.forward(d) turtle.left(90) turtle.forward(d * (2.0 / 3) * (math.sqrt(3.0) / 2.0)) #deserves comment turtle.right(90) turtle.penDown() curSize = curSize - 2 * d turtle.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 colorEquilateralTriangle(name, size=500): bitmap = IV122Graphics.BitMap("output/shapes/" + name + ".jpg", size, size) bot = 10 right = size - 10 left = 10 for i_x in range(size): for i_y in range(size): halfplane1 = i_y >= 0 #halfplane2 = i_y <= -math.sqrt(3.0)/2 * i_x + math.sqrt(3.0)*size/2 halfplane2 = i_y >= math.sqrt(3.0) * i_x halfplane3 = i_y >= -math.sqrt(3.0) * i_x + size #halfplane3 = i_y <= math.sqrt(3.0)/2 * i_x + -math.sqrt(3.0)*size/2 if (halfplane1 and halfplane2 and halfplane3): bitmap.putPixel(i_x, i_y, ((i_x / 2) % 255, (i_y / 3) % 255, 100)) bitmap.close()
def drawStar(name, n, length): #some n work better then other n turtle = IV122Graphics.Turtle(name, 400, 400, 50, 200) if (n % 2 == 0 and (n / 2) % 2 == 1): #should be upperLim = n * 2 x = 1 div = n elif (n % 4 == 0): #commented what upperLim = 2 * n x = 2 div = n else: #each branch does upperLim = n x = 2 div = n for i in range(upperLim): turtle.forward(length) turtle.left(180 - (360.0 / n) / x) turtle.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 generateWeirdChessboard(name, size): bitmap = IV122Graphics.BitMap("output/effects/" + name + ".jpg", size, size) squareSize = 20 black = (0, 0, 0) #initially black, but swaps during execution white = (0xFF, 0xFF, 0xFF) radiusSize = 50 for i_x in range(-size / 2, size / 2): for i_y in range(-size / 2, size / 2): xParity = (i_x / 20) % 2 yParity = (i_y / 20) % 2 levelOfCircle = ( (i_x**2 + i_y**2)** 0.5) / radiusSize #v kolikatem krouzku od prostred jsem #print int(levelOfCircle) if (int(levelOfCircle) % 2 == 0): color1, color2 = black, white else: color1, color2 = white, black if (xParity == 1): if (yParity == 1): print "a", color1, color2 bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color2) else: print "b", color1, color2 bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color1) else: if (yParity == 1): print "c", color1, color2 bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color1) else: print "d", color1, color2 bitmap.putPixel(size / 2 + i_x, size / 2 + i_y, color2) print "neco" bitmap.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 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 feingebaum(x_min, x_max, steps, name, size=100): jpeg = IV122Graphics.BitMap("output/" + name + ".jpg", size, size) dif = x_max - x_min step = (float(dif) / steps) myRange = [] for x in range( steps ): #containes "steps" number of values from 0 to 1, scaled according to specified zoom myRange.append(x * step * (1 / dif)) print myRange for i in myRange: xt = 0.5 for j in range(200): xt = getNextFeingenbaum( xt, i * dif + x_min) #adding to i to scale according to zoom if (j > 100): jpeg.putPixel( i * (size), size - (size * xt) - 1, (0, 0, 0)) #odecitam od size, aby to nebylo obracen jpeg.close()
def ellipses(name, size=500): bitmap = IV122Graphics.BitMap("output/shapes/" + name + ".jpg", size, size) bot = 10 right = size - 10 left = 10 for i_x in range(-size / 2, size / 2): for i_y in range(-size / 2, size / 2): a = 100 b = 200 term1 = ( (i_x * Commons.cos(30) + i_y * Commons.sin(30))**2) / (a * a) term2 = ( (i_x * Commons.sin(30) - i_y * Commons.cos(30))**2) / (b * b) distance = (i_x * i_x + i_y * i_y)**0.5 if (term1 + term2 <= 1): bitmap.putPixel( size / 2 + i_x, size / 2 + i_y, (255 - distance, 255 - distance, 255 - distance)) bitmap.close()
def turtleCreativityExecute(): triangleFractal = IV122Graphics.Turtle( "output/fractal/creativeTriangle.svg", 10000, 10000, 5000, 5000) turtleCreativity(triangleFractal, 800, 8) triangleFractal.close()
def creativity2Execute(): somethign = IV122Graphics.Turtle("output/fractal/creative2.svg", 400, 400, 200, 200) creativity2(somethign, 800, 4) somethign.close()