Esempio n. 1
0
def drawLeaf(leaf, edges, ctx, outerRadius=mm(20), axisRadius=mm(4), R=mm(50)):
    neighbour = [otherCorner(edge, leaf) for edge in edges if leaf in edge][0]
    offsetCairo = -0.5 * math.pi
    angle = (leaf - neighbour).polar()[1]

    # 15 degrees offset between outer intersection points
    offsetAngle = 2 * math.pi * (15 / 360)
    # Get points where circle of leaf begins and ends
    V = Vec(0, -1).rotate(-angle - offsetAngle)
    W = Vec(0, -1).rotate(-angle + offsetAngle)

    # Generate weird puzzle piece thing for leaf
    points = generateThoseWeirdPuzzlePieces(leaf + V * R, leaf + W * R, 1)

    ctx.set_source_rgb(1, 0, 0)
    ctx.set_line_width(0.05)

    ctx.new_sub_path()
    # Move to starting point
    ctx.move_to((leaf + V * R)[0], (leaf + V * R)[1])

    # Draw weird puzzle piece thing
    for i in range(len(points) - 2):
        p2, p3 = points[i + 1], points[i + 2]
        pp = (p2 + p3) / 2  # Middle of next path
        # Go to end of path if at last curve
        if i == len(points) - 3:
            pp = p3
        ctx.curve_to(p2[0], p2[1], p2[0], p2[1], pp[0], pp[1])

    # 15 degrees offset between inner intersection points
    offsetAngle = 2 * math.pi * (80 / 360)
    # V = Vec(0, -1).rotate(-angle - offsetAngle)
    W = Vec(0, -1).rotate(-angle + offsetAngle)

    # Line from end of puzzle piece to beginning of circle
    ctx.line_to((leaf + W * outerRadius)[0], (leaf + W * mm(20))[1])
    # Circle
    ctx.arc(leaf[0], leaf[1], outerRadius, offsetCairo - angle + offsetAngle,
            offsetCairo - angle - offsetAngle)
    # Line back from end of circle to beginning of puzzle piece
    ctx.line_to((leaf + V * R)[0], (leaf + V * R)[1])

    ### Draw hole for shaft / axis, 3.9mm
    V = Vec(0, axisRadius).rotate(offsetCairo) + leaf
    ctx.move_to(V[0], V[1])
    ctx.arc(leaf[0], leaf[1], axisRadius, 0, 2 * math.pi)

    ctx.stroke()
    ctx.close_path()

    ## Draw id and connecting id
    ctx.set_source_rgb(0, 0, 0)
    ctx.set_line_width(1)
    ctx.set_font_size(4)

    Ptext = leaf + Vec(0, 10)
    ctx.move_to(Ptext[0], Ptext[1])
    ctx.show_text(makeVectorId(leaf))

    Ptext = leaf + (neighbour - leaf).normalize() * 15
    ctx.move_to(Ptext[0], Ptext[1])
    ctx.show_text(makeVectorId(neighbour))

    ctx.stroke()
Esempio n. 2
0
def sampleFromNormal2D(x=0, y=0, sigma=25):
    px = sigma * sampleFromNormal() + x
    py = sigma * sampleFromNormal() + y
    return Vec(px, py)
Esempio n. 3
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 800))
    pygame.display.set_caption('SPH Forces')

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    clock = pygame.time.Clock()

    max_num = 2**12  #4096
    #max_num = 2**10 #1024
    #max_num = 2**8 #256
    #max_num = 2**7 #128

    dmin = Vec([0, 0, 0])
    dmax = Vec([50, 50, 50])
    domain = Domain(dmin, dmax)  #, screen)
    system = sph.SPH(max_num, domain)
    #system = magnet.Magnet(max_num, domain)

    #particles = sph.init_particles(5, system, domain, screen)
    particles = []
    p1 = Vec([25, 25]) * system.sim_scale
    np = Vec([.8, .8])
    particles += [Particle(p1, np, system, [0, 128, 128], screen)]
    particles[0].lock = True

    p = Vec([25 + 8, 25]) * system.sim_scale
    particles += [Particle(p, np, system, [128, 128, 0], screen)]
    #"""
    p = Vec([25, 25 + 8]) * system.sim_scale
    particles += [Particle(p, np, system, [128, 128, 0], screen)]
    p = Vec([25 - 8, 25]) * system.sim_scale
    particles += [Particle(p, np, system, [128, 128, 0], screen)]
    p = Vec([25, 25 - 8]) * system.sim_scale
    particles += [Particle(p, np, system, [128, 128, 0], screen)]
    p = Vec([25 + 8, 25 + 8]) * system.sim_scale
    particles += [Particle(p, np, system, [128, 128, 0], screen)]
    #"""

    print "p0.pos:", particles[0].pos

    mouse_down = False
    pause = True
    pi = 1
    tpi = pi
    while 1:
        tpi = pi
        tcol = particles[pi].col[:]
        particles[pi].col = [0, 200, 0]
        clock.tick(60)
        key = pygame.key.get_pressed()
        for event in pygame.event.get():
            if event.type == QUIT or key[K_ESCAPE] or key[K_q]:
                print "quit!"
                return
            elif key[K_t]:
                print timings

            elif key[K_0]:
                pi = 0
            elif key[K_1]:
                pi = 1
            elif key[K_2]:
                pi = 2
            elif key[K_3]:
                pi = 3
            elif key[K_4]:
                pi = 4
            #elif key[K_5]:
            #    pi = 5

            elif key[K_p]:
                pause = not pause

            elif event.type == MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[2]:
                    v = Vec([event.pos[0], event.pos[1]])
                    v = fromscreen(v, screen)
                    p = Particle([0, 0], [0, 0], system, [128, 128, 0], screen)
                    p.move(v)
                    particles += [p]
                    break
                tcol = particles[pi].col[:]
                mouse_down = True
            elif event.type == MOUSEMOTION:
                if (mouse_down):
                    v = Vec([event.pos[0], event.pos[1]])
                    v = fromscreen(v, screen)
                    print v
                    particles[pi].move(v)
                    particles[pi].vel = Vec([0., 0.])
                    particles[pi].force = Vec([0., 0.])
                    particles[pi].angular = Vec([0., 0.])
            elif event.type == MOUSEBUTTONUP:
                particles[pi].vel = Vec([0., 0.])
                particles[pi].force = Vec([0., 0.])
                particles[pi].angular = Vec([0., 0.])
                mouse_down = False

        screen.blit(background, (0, 0))

        density_update(system, particles)
        if not pause:
            for i in range(10):
                #magnet_update(system, particles)
                #density_update(system, particles)
                force_update(system, particles)

                #contact_update(system, particles)
                collision_wall(system, domain, particles)
                #euler_update(system, particles, dt)
                leapfrog_update(system, particles)

        draw_particles(particles)
        pygame.display.flip()

        particles[tpi].col = tcol[:]
Esempio n. 4
0
def generatePoint(radius=200, mu=0, sigma=25):
    r = np.random.rand() * (2 * math.pi * 2 / 4) - (
        2 * math.pi * 1 / 4)  # Rotation somewhere between -.5pi and .5pi
    p = Vec(0, -radius).rotate(r) + sampleFromNormal2D(
        sigma=sigma
    )  # Rotate vector, add random offset sampled from normal distribution
    return p


def inPlane(P, r, w, h):
    return 0 < P[0] - r and 0 < P[1] - r and P[0] + r < w and P[1] + r < h


O = np.array([WIDTH // 2, HEIGHT - HEIGHT // 5])
rootNode = Vec(WIDTH // 2, HEIGHT - 2 * R)


def getPointsFromEdge(edge, dist=10):
    V, W = edge
    center = (V + W) * 0.5
    p1 = (dist * (V - W) * (1 / (V - W).norm())).rotate(0.5 * math.pi) + center
    p2 = (dist * (W - V) * (1 / (W - V).norm())).rotate(0.5 * math.pi) + center
    return p1, p2


def getConnectingPairsFromNode(edges, N):
    # Retrieve all neighbouring points
    neighbours = [otherCorner(edge, N) for edge in edges if N in edge]
    # Convert all neighbours from cartesian to polar
    polar = [(n - N).polar() for n in neighbours]
Esempio n. 5
0
 def transform(self, vec):
     """
     Return a rotated vec around local origin.
     i.e. apply self.angle
     """
     return vec.rotate_origin(self.angle(), origin=Vec(self.radius, self.radius))
Esempio n. 6
0
def toscreen(p, surface, screen_scale):
    translate = Vec([0, 0])
    p.x = translate.x + p.x * screen_scale
    p.y = surface.get_height() - (translate.y + p.y * screen_scale)
    return p
Esempio n. 7
0
    def __init__(self, pos, velocity, radius):
        super().__init__(pos, velocity, radius)
        self.axis = Vec(0, -1)
        self.direction = Vec(0, -1)
        self._thrust = False
        self.rotate_speed = 0.0
        self.score = 0
        self.invincible = 200

        wingtip1 = self.vec_from_center(135)
        wingtip2 = self.vec_from_center(-135)
        nose = Vec(self.radius, 0)
        backtip1 = wingtip1 * 0.95
        backtip2 = wingtip2 * 0.95

        # For some reason the back is off by 1 pixel on the x-axis
        backtip1[0] += 1.0
        backtip2[0] += 1.0

        width = 2
        color = C.white
        self.standby_lines = [
            Line(color, Vec(self.radius, 0), Vec(self.radius,self.radius), width ),
            Line(
                color=color,
                start_pos=nose,
                end_pos=wingtip1,
                width=width,
            ),
            Line(
                color=color,
                start_pos=nose,
                end_pos=wingtip2,
                width=width,
            ),
            Line(
                color=color,
                start_pos=backtip1,
                end_pos=backtip2,
                width=width,
            ),
        ]

        thrust_start = Vec(self.radius, backtip1.y)
        thrust_end = Vec(self.radius, 2*self.radius)
        thrust_mid = Vec(self.radius, backtip1.y + (2*self.radius - backtip1.y) * 0.5)

        self.thrust_lines = [
            Line(
                color=C.red,
                start_pos=thrust_start,
                end_pos=thrust_mid,
                width=radius//5,
            ),
             Line(
                color=C.orange,
                start_pos=thrust_mid,
                end_pos=thrust_end,
                width=radius//10,
            ),
        ]
Esempio n. 8
0
 def cannon(self):
     return self.position + self.transform(Vec(self.radius, 0))
Esempio n. 9
0
 def position(self, value):
     # Using only rect.x and rect.y causes weird behaviour
     self._pos = Vec(*value)
     self.rect.x, self.rect.y = value
Esempio n. 10
0
 def origin(self, value):
     value = Vec(*value)
     self.position = value.x - self.radius, value.y - self.radius
Esempio n. 11
0
 def origin(self):
     return Vec(self.x + self.radius, self.y + self.radius)
Esempio n. 12
0
    def _getRandPos():

        randPos = Vec(random.randrange(0, BoidEcosystem.habitatWidth),
                      random.randrange(0, BoidEcosystem.habitatHeight))
        return randPos
Esempio n. 13
0
import networkx as nx
from computer import Computer
from vector import Vec

WALL, PATH = 0, 1
UP, DOWN, LEFT, RIGHT = 1, 2, 3, 4
START = Vec((0, 0))

directions = {
    Vec((-1, 0)): UP,
    Vec((1, 0)): DOWN,
    Vec((0, -1)): LEFT,
    Vec((0, 1)): RIGHT
}


def reduce_path(path):
    for start, end in zip(path, path[1:]):
        yield end - start


class Robot:
    def __init__(self):
        self.G = nx.Graph()
        self.stack = []
        with open('input15', 'r') as data:
            self.brain = Computer(
                int_code=list(map(int,
                                  data.read().split(','))))
        self.computation = self.brain.compute_iter()