def circulate(p0, r0, p1, r1, p2, r2, p3, r3, depth): pointList = [p0, p1, p2] radiiList = [r0, r1, r2] if depth == 0: return [p3], [r3] else: newPL = [] newRL = [] for indxs in itertools.combinations(range(3), 2): i0, i1 = indxs pI, rI, pO, rO = circit(pointList[i0], radiiList[i0], pointList[i1], radiiList[i1], p3, r3) if radiiList[i0]*radiiList[i1]*r3 > 0: pNew, rNew = pI, rI else: pNew, rNew = pO, rO result = circulate(pointList[i0], radiiList[i0], pointList[i1], radiiList[i1], p3, r3, pNew, rNew, depth-1) newPL += result[0] newRL += result[1] return [p3]+newPL, [r3]+newRL
def circulate(c0, c1, c2, c3, depth): cList = [c0, c1, c2] if depth == 0: return [c3] else: new = [] for indxs in itertools.combinations(range(3), 2): i0, i1 = indxs cI, cO = circit(cList[i0], cList[i1], c3) if cList[i0][0] * cList[i1][0] * c3[0] > 0: cNew = cI else: cNew = cO new += circulate(cList[i0], cList[i1], c3, cNew, depth - 1) return [c3] + new
def circulate(c0, c1, c2, depth): cList = [c0, c1, c2] if depth == 0: return c3 new = [] for circleA, circleB in itertools.combinations(cList, 2): cInner, cOuter = circit(circleA, circleB, c2) if circleA[0] * circleB[0] > 0: cNew = cInner else: cNew = cOuter result = circulate(circleA, circleB, cNew, depth-1) new += result return c3 + new
def circulate(c0, c1, c2, c3, depth): cList = [c0, c1, c2] if depth == 0: return [c3] else: new = [] for indxs in itertools.combinations(range(3), 2): i0, i1 = indxs cI, cO = circit(cList[i0], cList[i1], c3) if cList[i0][0] * cList[i1][0] * c3[0] > 0: cNew = cI else: cNew = cO result = circulate(cList[i0], cList[i1], c3, cNew, depth - 1) new += result return [c3] + new
def make_one(shift=[0., 0.]): """ original """ # startPoints = list(range(3)) # for i in startPoints: # x = 2.5*cos(2.0 * pi * i / 3) # y = 2.5*sin(2.0 * pi * i / 3) # startPoints[i] = [x, y] """ anomalus """ # startPoints = [[-2, 2], [0, 0], [2, 2]] """ earhoops """ # startPoints = [[-1, -2], [0, 1], [1, -2]] """ random """ # startPoints = (np.random.rand(3, 2) * 4. - 2.).tolist() """ random angles """ # startPoints = [[np.cos(2. * np.pi * t), np.sin(2. * np.pi * t)] for t in np.random.rand(3)] # all circles are defined as c = [r, px, py] with # radius = c[0] and x, y coordinates of the center # c[1] and c[2] (latter adding c[3] to make a 3d # gasket with spheres) def rand_init(): points = np.random.rand(3, 2) - .5 return points.tolist() def circulate(c0, c1, c2, c3, depth): cList = [c0, c1, c2] if depth == 0: return [c3] else: new = [] for indxs in itertools.combinations(range(3), 2): i0, i1 = indxs cI, cO = circit(cList[i0], cList[i1], c3) if cList[i0][0] * cList[i1][0] * c3[0] > 0: cNew = cI else: cNew = cO new += circulate(cList[i0], cList[i1], c3, cNew, depth - 1) return [c3] + new def sign(p1, p2, p3): return (p1[1] - p3[1]) * (p2[2] - p3[2]) - (p2[1] - p3[1]) * (p1[2] - p3[2]) def PointInTriangle(pt, v1, v2, v3): b1 = sign(pt, v1, v2) < 0. b2 = sign(pt, v2, v3) < 0. b3 = sign(pt, v3, v1) < 0. return ((b1 == b2) and (b2 == b3)) loops = 4 good3 = False while not good3: startPoints = rand_init() [c0, c1, c2] = radii(*startPoints) c3, c4 = circit(c0, c1, c2) if c4[0] > 0: continue if not PointInTriangle(c4, c1, c2, c3): continue good3 = True clot = [c0, c1, c2] + circulate(c0, c1, c2, c4, loops) + circulate( c0, c1, c2, c3, loops) biggestR = max(abs(np.array(clot)[:, 0])) scale = 1.4 / biggestR annuli = [] for c in clot: r, x, y = c if abs(r) < 0.05: continue annuli.append( makeShapelyCircle(scale * (x - c4[1]) + (shift[0] - 1) * 3, scale * (y - c4[2]) + (shift[1] - 1) * 3, scale * abs(r), thickness=0.05)) testUnion = cascaded_union(annuli) return testUnion
if radiiList[i0]*radiiList[i1]*r3 > 0: pNew, rNew = pI, rI else: pNew, rNew = pO, rO result = circulate(pointList[i0], radiiList[i0], pointList[i1], radiiList[i1], p3, r3, pNew, rNew, depth-1) newPL += result[0] newRL += result[1] return [p3]+newPL, [r3]+newRL loops = 9 p0, p1, p2 = startPointList r0, r1, r2 = startRadiiList p3, r3, p4, r4 = circit(p0, r0, p1, r1, p2, r2) plot = [p0, p1, p2] + circulate(p0, r0, p1, r1, p2, r2, p4, r4, loops)[0] + circulate(p0, r0, p1, r1, p2, r2, p3, r3, loops)[0] rlot = [r0, r1, r2] + circulate(p0, r0, p1, r1, p2, r2, p4, r4, loops)[1] + circulate(p0, r0, p1, r1, p2, r2, p3, r3, loops)[1] # Make a pdf surface surf = cairo.PDFSurface(open("test.pdf", "w"), 512, 512) # Make a svg surface # surf = cairo.SVGSurface(open("test.svg", "w"), 512, 512) # Get a context object ctx = cairo.Context(surf) width = 0.005 ctx.set_line_width(width)
cI, cO = circit(cList[i0], cList[i1], c3) if cList[i0][0] * cList[i1][0] * c3[0] > 0: cNew = cI else: cNew = cO result = circulate(cList[i0], cList[i1], c3, cNew, depth - 1) new += result return [c3] + new loops = 8 [c0, c1, c2] = radii(*startPoints) c3, c4 = circit(c0, c1, c2) clot = [c0, c1, c2] + circulate(c0, c1, c2, c4, loops) + circulate(c0, c1, c2, c3, loops) w = 512 h = 512 # Make a pdf surface surf = cairo.PDFSurface(open("test.pdf", "w"), w, h) # Make a svg surface # surf = cairo.SVGSurface(open("test.svg", "w"), w, h) # Get a context object ctx = cairo.Context(surf) lineWidth = 0.001