Exemple #1
0
def count_monsters(tile: ImageTile):
    size = len(tile.pixels)

    total = sum(sum(row) for row in tile.pixels)
    subtract: Set[lib.Point] = set()

    count = 0
    for dx in range(size):
        for dy in range(size):
            monster = True

            for p in MONSTER_PATTERN:
                x, y = p.displace(dx, dy)

                if x >= size or y >= size or not tile.pixels[y][x]:
                    monster = False

            if monster:
                for p in MONSTER_PATTERN:
                    x, y = p.displace(dx, dy)
                    subtract.add(lib.Point(x, y))

            count += monster

    return total - len(subtract)
    def __init__(self, root):
        self.root = root

        self.ui_list = ['lab6', 'graphic', 'save-load', 'screw', 'camera']

        self.polyhedron = lib.Polyhedron.Cube(lib.Point(0, 0, 0), 100)
        self.camera = lib.Camera.ortho()
        self.transform = lib.Transform.identity()

        self.current = 0

        self.menu_var = tk.StringVar()
        self.menu_var.set(self.ui_list[self.current])
        self.menu = tk.OptionMenu(root, self.menu_var, *self.ui_list)
        self.menu.grid(row=0, column=0)

        self.frames = [
            Lab6(root),
            Graphic(root),
            SaveLoad(root),
            Screw(root),
            CameraFrame(root)
        ]
        self.frames[self.current].grid(row=1, column=0)

        self.rebind(-1, 0)

        self.menu_var.trace("w", self.change_menu)
Exemple #3
0
def monster_points(m: str):
    points = []
    for y, row in enumerate(m.strip("\n").splitlines()):
        for x, c in enumerate(row):
            if c == "#":
                points.append(lib.Point(x, y))

    return points
Exemple #4
0
 def read_position(self, *args):
     try:
         self.position_x = float(self.position_x_var.get())
         self.position_y = float(self.position_y_var.get())
         self.position_z = float(self.position_z_var.get())
     except:
         self.position_x = 0
         self.position_y = 0
         self.position_z = 0
     self.position = lib.Point(self.position_x, self.position_y,
                               self.position_z)
Exemple #5
0
    def screw(self):
        if self.angle == 0:
            return
        points = [
            lib.Point(x - self.width / 2, 0, self.height / 2 - z)
            for x, z in self.points
        ]
        transform = lib.Transform.rotate(self.axis_var.get(), self.angle)
        polyhedron = lib.Polyhedron(points, [])
        sides = []
        # print(self.angle)
        for _ in range(int(2 * pi / self.angle)):
            polyhedron2 = polyhedron.apply_transform(transform)
            ind_now = len(points)
            ind_prev = ind_now - len(self.points)
            points += [lib.Point(x, y, z) for x, y, z in polyhedron2.points.T]

            for i in range(len(self.points) - 1):
                p1 = ind_prev + i
                p2 = ind_prev + i + 1
                q1 = ind_now + i
                q2 = ind_now + i + 1

                sides.append([p1, p2, q1])
                sides.append([p2, q2, q1])

            polyhedron = polyhedron2

        for i in range(len(self.points) - 1):
            p1 = len(points) - i - 1
            p2 = len(points) - i - 2
            q1 = len(self.points) - i - 1
            q2 = len(self.points) - i - 2

            sides.append([q1, p2, p1])
            sides.append([q1, q2, p2])

        self.polyhedron = lib.Polyhedron(points, sides)
        self.draw()
Exemple #6
0
def parse_input(raw: str, *,
                num_dimensions) -> typing.DefaultDict[lib.PointNd, bool]:
    points = {}

    for y, line in enumerate(raw.splitlines()):
        for x, char in enumerate(line):
            active = char == "#"
            points[lib.Point(x, y)] = active

    dimensional_points = collections.defaultdict(lambda: False)

    for point, active in points.items():
        n_dimensions = [*point, *[0] * (num_dimensions - 2)]
        dimensional_points[lib.PointNd(n_dimensions)] = active

    return dimensional_points
Exemple #7
0
    def __init__(self, root):
        super().__init__(root)

        self.size = (600, 800)

        self.transform = lib.Transform.identity()

        self.polyhedron = lib.Polyhedron.Cube(lib.Point(0, 0, 0), 0)

        self.render = tk.Label(self)
        self.render.grid(row=0, column=0, rowspan=4)

        self.pos = [0, 0, 0]
        self.angles = [0, 0, 0]
        self.camera = lib.Camera.persp(0.01, self.pos, self.angles)

        self.draw()
Exemple #8
0
    def __init__(self, root):
        super().__init__(root)

        self.width = 400
        self.height = 400

        self.polyhedron = lib.Polyhedron.Cube(lib.Point(0, 0, 0), 0)
        self.camera = lib.Camera.iso()

        self.canvas = tk.Canvas(self,
                                width=self.width,
                                height=self.height,
                                bg='white')
        self.canvas.grid(row=0, column=0, rowspan=4)

        self.canvas.bind("<Button-1>", self.make_point)
        self.points = []

        self.axis_var = tk.StringVar()
        self.axis = tk.OptionMenu(self, self.axis_var, 'x', 'y', 'z')
        self.axis.grid(row=2, column=1)
        self.axis_var.set("x")
        self.axis_var.trace('w', self.read_params)

        self.angle = 0.
        self.separation_var = tk.StringVar()
        self.separation = tk.Entry(self, textvar=self.separation_var)
        self.separation.grid(row=1, column=1)
        self.separation_var.trace('w', self.read_params)

        self.clear_button = tk.Button(self, text="clear", command=self.clear)
        self.clear_button.grid(row=3, column=1)

        self.render = tk.Label(self)
        self.render.grid(row=0, column=2, rowspan=4)

        self.canvas_draw()
Exemple #9
0
import lib
import sys

assert (len(sys.argv) == 2)

private, public = lib.openssl_secp112r1.genPair()
with open("read.public_key", "r") as publicKeyFile:
    readerPublicStr = publicKeyFile.read()
    readerPublicSections = readerPublicStr.split(",")
    readerPublic = lib.Point(lib.openssl_secp112r1,
                             int(readerPublicSections[0]),
                             int(readerPublicSections[1]))

with open("send.public_key", "w") as senderPublicFile:
    senderPublicFile.write(str(public.x) + "," + str(public.y))

with open("mssg.cyphertext", "wb") as mssgFile:
    mssgFile.write((private * readerPublic).twoFish(sys.argv[1]))
Exemple #10
0
    def __init__(self, root):
        tk.Frame.__init__(self, root)

        self.size = (400, 400)

        self.polyhedron = lib.Polyhedron.Cube(lib.Point(0, 0, 0), 100)
        self.camera = lib.Camera.ortho(pos=[0, -100, 0], angles=[0, 1, 0])
        self.transform = lib.Transform.identity()
        self.matrix_transform = lib.Transform.identity()
        self.choice_transform = lib.Transform.identity()
        self.keyboard_rotate_transform = lib.Transform.identity()

        # self.key_bindid = root.bind('<Key>', self.keyboard_rotate)

        self.camera_var = tk.IntVar()
        self.persp_k_var = tk.StringVar()
        self.iso_a_var = tk.StringVar()
        self.iso_b_var = tk.StringVar()
        self.object_var = tk.IntVar()
        self.radius_var = tk.StringVar()
        self.position_x_var = tk.StringVar()
        self.position_y_var = tk.StringVar()
        self.position_z_var = tk.StringVar()
        self.transform_mode_var = tk.IntVar()
        self.transform_choice_var = tk.StringVar()

        self.transform_choice_list = [
            'identity', 'translate', 'rotate', 'scale', 'rotate_around_line',
            'reflect'
        ]

        self.view = tk.Label(self, width=self.size[0], height=self.size[1])
        self.view.grid(row=0, column=0, rowspan=18)

        # Camera
        self.camera_var.trace('w', self.set_camera)
        tk.Label(self, text='Camera:').grid(row=0, column=1)
        tk.Radiobutton(self, text='ortho', variable=self.camera_var,
                       value=0).grid(row=1, column=2)

        tk.Radiobutton(self, text='persp', variable=self.camera_var,
                       value=1).grid(row=2, column=2)
        tk.Label(self, text='k:').grid(row=2, column=3)

        self.persp_k_var.trace('w', self.read_persp_k)
        tk.Entry(self, textvar=self.persp_k_var).grid(row=2, column=4)

        tk.Radiobutton(self, text='iso', variable=self.camera_var,
                       value=2).grid(row=3, column=2)
        tk.Label(self, text='a:').grid(row=3, column=3)

        self.iso_a_var.trace('w', self.read_iso_a_b)
        tk.Entry(self, textvar=self.iso_a_var).grid(row=3, column=4)
        tk.Label(self, text='b:').grid(row=3, column=5)

        self.iso_b_var.trace('w', self.read_iso_a_b)
        tk.Entry(self, textvar=self.iso_b_var).grid(row=3, column=6)

        # Object

        self.object_var.trace('w', self.set_object)
        tk.Label(self, text='Object:').grid(row=4, column=1)
        tk.Radiobutton(self, text='Another', variable=self.object_var,
                       value=0).grid(row=4, column=2)
        tk.Radiobutton(self, text='Cube', variable=self.object_var,
                       value=1).grid(row=5, column=2)
        tk.Radiobutton(self,
                       text='Tetrahedron',
                       variable=self.object_var,
                       value=2).grid(row=6, column=2)
        tk.Radiobutton(self,
                       text='Octahedron',
                       variable=self.object_var,
                       value=3).grid(row=7, column=2)

        self.radius_var.set(100)
        self.radius_var.trace('w', self.set_object)
        tk.Label(self, text='radius:').grid(row=4, column=3)
        tk.Entry(self, textvariable=self.radius_var).grid(row=4, column=4)

        # Position

        self.position_x_var.trace('w', self.set_object)
        self.position_y_var.trace('w', self.set_object)
        self.position_z_var.trace('w', self.set_object)
        self.position_x_var.set(0)
        self.position_y_var.set(20)
        self.position_z_var.set(0)
        tk.Label(self, text='Position:').grid(row=8, column=1)
        tk.Label(self, text='x:').grid(row=9, column=2)
        tk.Entry(self, textvariable=self.position_x_var).grid(row=9, column=3)
        tk.Label(self, text='y:').grid(row=9, column=4)
        tk.Entry(self, textvariable=self.position_y_var).grid(row=9, column=5)
        tk.Label(self, text='z:').grid(row=9, column=6)
        tk.Entry(self, textvariable=self.position_z_var).grid(row=9, column=7)

        # Transform
        tk.Label(self, text='Transform:').grid(row=10, column=1)

        self.transform_mode_var.trace('w', self.draw)
        tk.Radiobutton(self,
                       text='Global',
                       variable=self.transform_mode_var,
                       value=0).grid(row=10, column=2)
        tk.Radiobutton(self,
                       text='Local',
                       variable=self.transform_mode_var,
                       value=1).grid(row=10, column=3)
        # # Matrix
        tk.Label(self, text='Matrix:').grid(row=11, column=2)
        self.matrix_vars = []
        self.matrix_values = np.zeros((4, 4))
        for i in range(4):
            self.matrix_vars.append([])
            for j in range(4):
                self.matrix_vars[i].append(tk.StringVar())
                self.matrix_values[i][j] = (1 if i == j else 0)
                tk.Entry(self,
                         textvar=self.matrix_vars[i][j]).grid(row=12 + i,
                                                              column=3 + j)
                self.matrix_vars[i][j].set(1 if i == j else 0)
                self.matrix_vars[i][j].trace('w', self.read_matrix)

        # # Choose
        self.transform_choice_var.set(self.transform_choice_list[0])
        self.transform_choice_current = 0
        tk.Label(self, text='Choose:').grid(row=16, column=2)
        tk.OptionMenu(self, self.transform_choice_var, *self.transform_choice_list) \
            .grid(row=16, column=3)

        # # # Choice frames
        self.transform_choice_frames = []
        self.transform_choice_var.trace('w', self.change_transform_choice)
        # # # # identity
        self.transform_choice_frames.append((tk.Frame(self), (1, 1)))
        tmp_fr = self.transform_choice_frames[-1][0]
        tk.Label(tmp_fr, text='No params here').grid(row=0, column=0)
        # # # # translate
        self.transform_choice_frames.append((tk.Frame(self), (1, 6)))
        tmp_fr = self.transform_choice_frames[-1][0]
        self.transform_choice_translate_vars = []
        self.transform_choice_translate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='x:').grid(row=0, column=0)
        tk.Entry(tmp_fr, textvar=self.transform_choice_translate_vars[-1]) \
            .grid(row=0, column=1)
        self.transform_choice_translate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='y:').grid(row=0, column=2)
        tk.Entry(tmp_fr, textvar=self.transform_choice_translate_vars[-1]) \
            .grid(row=0, column=3)
        self.transform_choice_translate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='z:').grid(row=0, column=4)
        tk.Entry(tmp_fr, textvar=self.transform_choice_translate_vars[-1]) \
            .grid(row=0, column=5)
        for v in self.transform_choice_translate_vars:
            v.trace('w', self.read_transform_choice_translate)
        # # # # rotate
        self.transform_choice_frames.append((tk.Frame(self), (1, 4)))
        tmp_fr = self.transform_choice_frames[-1][0]
        self.transform_choice_rotate_vars = [tk.StringVar(), tk.StringVar()]
        tk.Label(tmp_fr, text='axis:').grid(row=0, column=0)
        tk.OptionMenu(tmp_fr, self.transform_choice_rotate_vars[0], 'x', 'y', 'z') \
            .grid(row=0, column=1)
        self.transform_choice_rotate_vars[0].set('x')
        tk.Label(tmp_fr, text="angle (radians):").grid(row=0, column=2)
        tk.Entry(tmp_fr, textvariable=self.transform_choice_rotate_vars[1]) \
            .grid(row=0, column=3)
        for v in self.transform_choice_rotate_vars:
            v.trace('w', self.read_transform_choice_rotate)
        # # # # scale
        self.transform_choice_frames.append((tk.Frame(self), (1, 6)))
        tmp_fr = self.transform_choice_frames[-1][0]
        self.transform_choice_scale_vars = []
        self.transform_choice_scale_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='sx:').grid(row=0, column=0)
        tk.Entry(tmp_fr, textvar=self.transform_choice_scale_vars[-1]) \
            .grid(row=0, column=1)
        self.transform_choice_scale_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='sy:').grid(row=0, column=2)
        tk.Entry(tmp_fr, textvar=self.transform_choice_scale_vars[-1]) \
            .grid(row=0, column=3)
        self.transform_choice_scale_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='sz:').grid(row=0, column=4)
        tk.Entry(tmp_fr, textvar=self.transform_choice_scale_vars[-1]) \
            .grid(row=0, column=5)
        for v in self.transform_choice_scale_vars:
            v.trace('w', self.read_transform_choice_scale)

        # # # # rotate_around_line
        self.transform_choice_frames.append((tk.Frame(self), (3, 6)))
        tmp_fr = self.transform_choice_frames[-1][0]

        self.transform_choice_line_rotate_vars = []
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='x1:').grid(row=0, column=0)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=0, column=1)
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='y1:').grid(row=0, column=2)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=0, column=3)
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='z1:').grid(row=0, column=4)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=0, column=5)

        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='x2:').grid(row=1, column=0)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=1, column=1)
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='y2:').grid(row=1, column=2)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=1, column=3)
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='z2:').grid(row=1, column=4)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=1, column=5)

        # angle
        self.transform_choice_line_rotate_vars.append(tk.StringVar())
        tk.Label(tmp_fr, text='angle:').grid(row=2, column=0)
        tk.Entry(tmp_fr, textvar=self.transform_choice_line_rotate_vars[-1]) \
            .grid(row=2, column=1)

        for c in self.transform_choice_line_rotate_vars:
            c.trace("w", self.read_transform_choice_line_rotate)

        # # # # reflect
        self.transform_choice_frames.append((tk.Frame(self), (1, 2)))
        tmp_fr = self.transform_choice_frames[-1][0]
        tk.Label(tmp_fr, text='plane:').grid(row=0, column=0)
        self.transform_choice_reflect_var = tk.StringVar()
        tk.OptionMenu(tmp_fr, self.transform_choice_reflect_var, 'xy', 'xz', 'yz') \
            .grid(row=0, column=1)
        self.transform_choice_reflect_var.set('xy')
        self.transform_choice_reflect_var.trace(
            'w', self.read_transform_choice_reflect)

        self.change_transform_choice()

        self.draw()
# import dependencies
import lib
import matplotlib.pyplot as plt

# Create FiniteFeild of integers mod 41 and use that to create the eliptic curve
# y**2 = x**3 - 3x + 15 mod 41
F = lib.FiniteFeild(41)
C = lib.Curve(F, -3, 15, 6, 7, 33)

# Generate all possible integer points in form (0 <= x < 41, 0 <= y < 41)
# Proceed to filter for points that lie on the curve
points = [lib.Point(C, i // F.size, i % F.size) for i in range(F.size**2)]
points = list(filter(C.isOnCurve, points))

# print the points and the order of the curve to the screen.
print(list(map(str, points)))
print(len(points))

# Display the points of the eliptic curve as a scatter plot.
plt.plot([point.x for point in points], [point.y for point in points], "bo")
plt.axis([0, F.size, 0, F.size])
plt.show()
Exemple #12
0
    column_mapping = [int(x) for x in args.columns.split(',')]

## Get data
if args.input:
    ## CSV data from file
    data = []
    with open(args.input) as file:
        reader = csv.reader(file)
        for row in reader:
            if len(row) <= 0:
                continue
            if column_mapping:
                point = tuple([float(row[i]) for i in column_mapping])
            else:
                point = tuple([float(x) for x in row])
            data.append(lib.Point(point, row))
else:
    ## Random data
    data = []
    for _ in range(80):
        data.append(tuple([random.random() * 5 + 0, random.random() * 5 + 0]))
        data.append(tuple([random.random() * 5 + 15,
                           random.random() * 5 + 10]))
        data.append(tuple([random.random() * 6 + 2, random.random() * 6 + 10]))
        data.append(tuple([random.random() * 16 + 2,
                           random.random() * 16 + 2]))
        data.append(tuple([random.random() * 16 + 2,
                           random.random() * 16 + 2]))

## Do calculations
if args.kmeans: