Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """test"""
- def print_blank():
- """Prints a blank line."""
- print("\n" + "#" * 79 + "\n")
- def length(x_1, y_1, x_2, y_2):
- """Computes the length of [(x_1, y_1), (x_2, y_2)]."""
- return ((x_2 - x_1) ** 2 + (y_2 - y_1) ** 2) ** 0.5
- def to_point(z):
- """Converts x + iy to (x, y)."""
- return [z.real, z.imag]
- def mod(z):
- """Computes the modulus of the specified complex."""
- return (z.real ** 2 + z.imag ** 2) ** 0.5
- def n_inf(mat):
- """Computes N_inf(A)."""
- return max([
- sum([mod(a) for a in l])
- for l in mat
- ])
- def line(mat, i):
- """Computes L_i(mat)."""
- n = len(mat)
- return sum([mod(mat[i][k]) for k in range(n) if k != i])
- def column(mat, j):
- """Computes L_j(mat)."""
- n = len(mat)
- return sum([mod(mat[k][j]) for k in range(n) if k != j])
- class CircleCons(object):
- """Represents a set of circle constraints."""
- def __init__(self, subcons=[]):
- """Init method."""
- # a subc: (x_c, y_c, r)
- self._subcons = subcons
- def render_subc(self, x, y, subc):
- """Returns True iif x & y respect the specified constraint."""
- (x_c, y_c, r) = subc
- return length(x, y, x_c, y_c) <= r
- def render(self, x, y):
- """Returns True iif x & y respect all constraints."""
- for subc in self._subcons:
- if not self.render_subc(x, y, subc):
- return False
- return True
- def __str__(self):
- """Returns a string representation of the object."""
- return "<Circles> = {\n" + "\n".join(["({}, {}, {})".format(*elt) for elt in self._subcons]) + "\n}\n"
- @staticmethod
- def sum_c_cons(c_cons_a, c_cons_b):
- """Returns the sum of both circle constraints."""
- if c_cons_a is None:
- return c_cons_b
- else:
- if c_cons_b is None:
- return c_cons_a
- else:
- return CircleCons(c_cons_a._subcons + c_cons_b._subcons)
- class OvalCons(object):
- """Represents a set of oval constraints."""
- def __init__(self, subcons=[]):
- """Init method."""
- # a subc: (x_a, y_a, x_b, y_b, m)
- self._subcons = subcons
- def render_subc(self, x, y, subc):
- """Returns True iif x & y respect the specified constraint."""
- (x_a, y_a, x_b, y_b, m) = subc
- return length(x, y, x_a, y_a) * length(x, y, x_b, y_b) <= m
- def render(self, x, y):
- """Returns True iif x & y respect all constraints."""
- for subc in self._subcons:
- if not self.render_subc(x, y, subc):
- return False
- return True
- def __str__(self):
- """Returns a string representation of the object."""
- return "<Ovals> = {\n" + "\n".join(["({}, {}, {}, {}, {})".format(*elt) for elt in self._subcons]) + "\n}"
- @staticmethod
- def sum_o_cons(o_cons_a, o_cons_b):
- """Returns the sum of both oval constraints."""
- if o_cons_a is None:
- return o_cons_b
- else:
- if o_cons_b is None:
- return o_cons_a
- else:
- return OvalCons(o_cons_a._subcons + o_cons_b._subcons)
- class Cons(object):
- """Represents a set of constraints."""
- def __init__(self, c_cons=None, o_cons=None):
- """Init method."""
- self._c_cons = c_cons
- self._o_cons = o_cons
- def __str__(self):
- """Returns a representation of the object."""
- return "CONSTRAINTS\n----------\n" + str(self._c_cons) + "\n" + str(self._o_cons) + "\n----------"
- @staticmethod
- def sum_cons(cons_a, cons_b):
- """Returns the sum of both constraints sets."""
- if cons_a is None:
- return cons_b
- else:
- if cons_b is None:
- return cons_a
- else:
- return Cons(
- CircleCons.sum_c_cons(cons_a._c_cons, cons_b._c_cons),
- OvalCons.sum_o_cons(cons_a._o_cons, cons_b._o_cons))
- class RShape(object):
- """Represents a rectangle area with constraints to draw a shape."""
- def __init__(self, x_1, y_1, x_2, y_2, cons=None):
- """Init method."""
- self._x_1 = x_1
- self._y_1 = y_1
- self._x_2 = x_2
- self._y_2 = y_2
- self._cons = cons
- @staticmethod
- def inter_rshape(rshape_a, rshape_b):
- """Returns the intersection of the rectangle shapes."""
- # print("carabuc")
- if rshape_a is None or rshape_b is None:
- return None
- else:
- max_x_1 = max(rshape_a._x_1, rshape_b._x_1)
- min_x_2 = min(rshape_a._x_2, rshape_b._x_2)
- min_y_1 = min(rshape_a._y_1, rshape_b._y_1)
- max_y_2 = max(rshape_a._y_2, rshape_b._y_2)
- if min_x_2 > max_x_1 and min_y_1 > max_y_2:
- return RShape(max_x_1, min_y_1, min_x_2, max_y_2,
- Cons.sum_cons(rshape_a._cons, rshape_b._cons))
- else:
- return None
- def __str__(self):
- """Returns a string representation of the object."""
- return "RSHAPE\n({}, {}) -> ({}, {})\n".format(self._x_1, self._y_1, self._x_2, self._y_2) + str(self._cons)
- class Shape(object):
- """Represents an union of rectangle areas with constraints."""
- def __init__(self, rshapes=[]):
- """Init method."""
- self._rshapes = rshapes
- self._x_1 = None
- self._y_1 = None
- self._x_2 = None
- self._y_2 = None
- for r in self._rshapes:
- # print("{{{COUIC}}}")
- self.resize_with(r)
- def resize_with(self, r):
- # print("passage")
- """Resize the global shape according to the (new) rshape."""
- if self._x_1 is None:
- self._x_1 = r._x_1
- self._y_1 = r._y_1
- self._x_2 = r._x_2
- self._y_2 = r._y_2
- else:
- self._x_1 = min(self._x_1, r._x_1)
- self._y_1 = max(self._y_1, r._y_1)
- self._x_2 = max(self._x_2, r._x_2)
- self._y_2 = min(self._y_2, r._y_2)
- def add_rshape(self, r):
- """Adds the specified RShape to the global shape."""
- self._rshapes.append(r)
- self.resize_with(r)
- @staticmethod
- def inter_shape(shape_a, shape_b):
- """Returns the intersection of the shapes."""
- result = Shape()
- # print("#####APPEL#####\n###############")
- for r_a in shape_a._rshapes:
- for r_b in shape_b._rshapes:
- r_res = RShape.inter_rshape(r_a, r_b)
- if r_res is not None:
- # print("prink")
- result.add_rshape(r_res)
- return result
- def __str__(self):
- """Returns a string representation of the object."""
- if self._x_1 is None:
- c = "[Shape]: <empty>"
- else:
- c = ("[Shape]: ({}, {}) -> ({}, {})"
- .format(self._x_1,
- self._y_1,
- self._x_2,
- self._y_2))
- return c + " [{}]\n".format(len(self._rshapes)) + "\n//////////////\n".join([str(r) for r in self._rshapes])
- def __len__(self):
- """Returns the length of the shape."""
- return len(self._rshapes)
- __repr__ = __str__
- def circle(x_c, y_c, r):
- """Creates the RShape object corresponding to the specified circle params.
- """
- return RShape(x_c - r, y_c + r, x_c + r, y_c - r,
- Cons(c_cons=CircleCons([(x_c, y_c, r)])))
- def oval(x_a, y_a, x_b, y_b, m):
- """Creates the RShape object corresponding to the specified oval params."""
- x_m = (x_a + x_b) / 2
- y_m = (y_a + y_b) / 2
- s = length(x_a, y_a, x_m, y_m)
- r = (s ** 2 + m) ** 0.5
- return RShape(x_m - r, y_m + r, x_m + r, y_m - r,
- Cons(o_cons=OvalCons([(x_a, y_a, x_b, y_b, m)])))
- def process(mat):
- """Constructs the requested drawing."""
- spectrum_shape = Shape([circle(0, 0, n_inf(mat))])
- n = len(mat)
- g_l_shape = Shape(
- [circle(*to_point(mat[i][i]), line(mat, i)) for i in range(n)])
- print("!!!!! G_L_SHAPE !!!!!")
- print(g_l_shape)
- print_blank()
- g_c_shape = Shape(
- [circle(*to_point(mat[j][j]), column(mat, j)) for j in range(n)])
- print("!!!!! G_C_SHAPE !!!!!")
- print(g_c_shape)
- print_blank()
- oval_shape = Shape(
- [oval(*(to_point(mat[i][i]) + to_point(mat[j][j])),
- line(mat, i) * column(mat, j))
- for i in range(n)
- for j in range(n)])
- print("!!!!! OVAL_SHAPE !!!!!")
- print(oval_shape)
- print_blank()
- g_shape = Shape.inter_shape(g_l_shape, g_c_shape)
- k_shape = Shape.inter_shape(oval_shape, spectrum_shape)
- print("!!!!! G_SHAPE !!!!!")
- print(g_shape)
- print_blank()
- print("!!!!! K_SHAPE !!!!!")
- print(k_shape)
- print_blank()
- #result_shape = Shape.inter_shape(
- # g_shape,
- # k_shape)
- #print("!!!!! RESULT_SHAPE !!!!!")
- #print(result_shape)
- #print(result_shape)
- def main():
- """Launcher."""
- mat = [
- [-1, 2],
- [2, 3]
- ]
- process(mat)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement