• API
• FAQ
• Tools
• Archive
A Pastebin account makes a great Christmas gift
SHARE
TWEET

# Nouvelle version

a guest Nov 17th, 2017 58 Never
ENDING IN00days00hours00mins00secs

1. #!/usr/bin/env python3
2. # -*- coding: utf-8 -*-
3.
4. """test"""
5.
6. def print_blank():
7.     """Prints a blank line."""
8.     print("\n" + "#" * 79 + "\n")
9.
10. def length(x_1, y_1, x_2, y_2):
11.     """Computes the length of [(x_1, y_1), (x_2, y_2)]."""
12.     return ((x_2 - x_1) ** 2 + (y_2 - y_1) ** 2) ** 0.5
13.
14. def to_point(z):
15.     """Converts x + iy to (x, y)."""
16.     return [z.real, z.imag]
17.
18. def mod(z):
19.     """Computes the modulus of the specified complex."""
20.     return (z.real ** 2 + z.imag ** 2) ** 0.5
21.
22. def n_inf(mat):
23.     """Computes N_inf(A)."""
24.     return max([
25.         sum([mod(a) for a in l])
26.         for l in mat
27.     ])
28.
29. def line(mat, i):
30.     """Computes L_i(mat)."""
31.     n = len(mat)
32.     return sum([mod(mat[i][k]) for k in range(n) if k != i])
33.
34. def column(mat, j):
35.     """Computes L_j(mat)."""
36.     n = len(mat)
37.     return sum([mod(mat[k][j]) for k in range(n) if k != j])
38.
39. class CircleCons(object):
40.     """Represents a set of circle constraints."""
41.
42.     def __init__(self, subcons=[]):
43.         """Init method."""
44.         # a subc: (x_c, y_c, r)
45.         self._subcons = subcons
46.
47.     def render_subc(self, x, y, subc):
48.         """Returns True iif x & y respect the specified constraint."""
49.         (x_c, y_c, r) = subc
50.         return length(x, y, x_c, y_c) <= r
51.
52.     def render(self, x, y):
53.         """Returns True iif x & y respect all constraints."""
54.         for subc in self._subcons:
55.             if not self.render_subc(x, y, subc):
56.                 return False
57.         return True
58.
59.     def __str__(self):
60.         """Returns a string representation of the object."""
61.         return "<Circles> = {\n" + "\n".join(["({}, {}, {})".format(*elt) for elt in self._subcons]) + "\n}\n"
62.
63.     @staticmethod
64.     def sum_c_cons(c_cons_a, c_cons_b):
65.         """Returns the sum of both circle constraints."""
66.         if c_cons_a is None:
67.             return c_cons_b
68.         else:
69.             if c_cons_b is None:
70.                 return c_cons_a
71.             else:
72.                 return CircleCons(c_cons_a._subcons + c_cons_b._subcons)
73.
74. class OvalCons(object):
75.     """Represents a set of oval constraints."""
76.
77.     def __init__(self, subcons=[]):
78.         """Init method."""
79.         # a subc: (x_a, y_a, x_b, y_b, m)
80.         self._subcons = subcons
81.
82.     def render_subc(self, x, y, subc):
83.         """Returns True iif x & y respect the specified constraint."""
84.         (x_a, y_a, x_b, y_b, m) = subc
85.         return length(x, y, x_a, y_a) * length(x, y, x_b, y_b) <= m
86.
87.     def render(self, x, y):
88.         """Returns True iif x & y respect all constraints."""
89.         for subc in self._subcons:
90.             if not self.render_subc(x, y, subc):
91.                 return False
92.         return True
93.
94.     def __str__(self):
95.         """Returns a string representation of the object."""
96.         return "<Ovals> = {\n" + "\n".join(["({}, {}, {}, {}, {})".format(*elt) for elt in self._subcons]) + "\n}"
97.
98.     @staticmethod
99.     def sum_o_cons(o_cons_a, o_cons_b):
100.         """Returns the sum of both oval constraints."""
101.         if o_cons_a is None:
102.             return o_cons_b
103.         else:
104.             if o_cons_b is None:
105.                 return o_cons_a
106.             else:
107.                 return OvalCons(o_cons_a._subcons + o_cons_b._subcons)
108.
109. class Cons(object):
110.     """Represents a set of constraints."""
111.
112.     def __init__(self, c_cons=None, o_cons=None):
113.         """Init method."""
114.         self._c_cons = c_cons
115.         self._o_cons = o_cons
116.
117.     def __str__(self):
118.         """Returns a representation of the object."""
119.         return "CONSTRAINTS\n----------\n" + str(self._c_cons) + "\n" + str(self._o_cons) + "\n----------"
120.
121.     @staticmethod
122.     def sum_cons(cons_a, cons_b):
123.         """Returns the sum of both constraints sets."""
124.         if cons_a is None:
125.             return cons_b
126.         else:
127.             if cons_b is None:
128.                 return cons_a
129.             else:
130.                 return Cons(
131.                     CircleCons.sum_c_cons(cons_a._c_cons, cons_b._c_cons),
132.                     OvalCons.sum_o_cons(cons_a._o_cons, cons_b._o_cons))
133.
134. class RShape(object):
135.     """Represents a rectangle area with constraints to draw a shape."""
136.
137.     def __init__(self, x_1, y_1, x_2, y_2, cons=None):
138.         """Init method."""
139.         self._x_1 = x_1
140.         self._y_1 = y_1
141.         self._x_2 = x_2
142.         self._y_2 = y_2
143.         self._cons = cons
144.
145.     @staticmethod
146.     def inter_rshape(rshape_a, rshape_b):
147.         """Returns the intersection of the rectangle shapes."""
148.         # print("carabuc")
149.         if rshape_a is None or rshape_b is None:
150.             return None
151.         else:
152.             max_x_1 = max(rshape_a._x_1, rshape_b._x_1)
153.             min_x_2 = min(rshape_a._x_2, rshape_b._x_2)
154.             min_y_1 = min(rshape_a._y_1, rshape_b._y_1)
155.             max_y_2 = max(rshape_a._y_2, rshape_b._y_2)
156.             if min_x_2 > max_x_1 and min_y_1 > max_y_2:
157.                 return RShape(max_x_1, min_y_1, min_x_2, max_y_2,
158.                               Cons.sum_cons(rshape_a._cons, rshape_b._cons))
159.             else:
160.                 return None
161.
162.     def __str__(self):
163.         """Returns a string representation of the object."""
164.         return "RSHAPE\n({}, {}) -> ({}, {})\n".format(self._x_1, self._y_1, self._x_2, self._y_2) + str(self._cons)
165.
166. class Shape(object):
167.     """Represents an union of rectangle areas with constraints."""
168.
169.     def __init__(self, rshapes=[]):
170.         """Init method."""
171.         self._rshapes = rshapes
172.         self._x_1 = None
173.         self._y_1 = None
174.         self._x_2 = None
175.         self._y_2 = None
176.         for r in self._rshapes:
177.             # print("{{{COUIC}}}")
178.             self.resize_with(r)
179.
180.     def resize_with(self, r):
181.         # print("passage")
182.         """Resize the global shape according to the (new) rshape."""
183.         if self._x_1 is None:
184.             self._x_1 = r._x_1
185.             self._y_1 = r._y_1
186.             self._x_2 = r._x_2
187.             self._y_2 = r._y_2
188.         else:
189.             self._x_1 = min(self._x_1, r._x_1)
190.             self._y_1 = max(self._y_1, r._y_1)
191.             self._x_2 = max(self._x_2, r._x_2)
192.             self._y_2 = min(self._y_2, r._y_2)
193.
195.         """Adds the specified RShape to the global shape."""
196.         self._rshapes.append(r)
197.         self.resize_with(r)
198.
199.     @staticmethod
200.     def inter_shape(shape_a, shape_b):
201.         """Returns the intersection of the shapes."""
202.         result = Shape()
203.         # print("#####APPEL#####\n###############")
204.         for r_a in shape_a._rshapes:
205.             for r_b in shape_b._rshapes:
206.                 r_res = RShape.inter_rshape(r_a, r_b)
207.                 if r_res is not None:
208.                     # print("prink")
210.         return result
211.
212.     def __str__(self):
213.         """Returns a string representation of the object."""
214.         if self._x_1 is None:
215.             c = "[Shape]: <empty>"
216.         else:
217.             c = ("[Shape]: ({}, {}) -> ({}, {})"
218.                     .format(self._x_1,
219.                             self._y_1,
220.                             self._x_2,
221.                             self._y_2))
222.         return c + " [{}]\n".format(len(self._rshapes)) + "\n//////////////\n".join([str(r) for r in self._rshapes])
223.
224.     def __len__(self):
225.         """Returns the length of the shape."""
226.         return len(self._rshapes)
227.
228.     __repr__ = __str__
229.
230. def circle(x_c, y_c, r):
231.     """Creates the RShape object corresponding to the specified circle params.
232.    """
233.     return RShape(x_c - r, y_c + r, x_c + r, y_c - r,
234.                   Cons(c_cons=CircleCons([(x_c, y_c, r)])))
235.
236. def oval(x_a, y_a, x_b, y_b, m):
237.     """Creates the RShape object corresponding to the specified oval params."""
238.     x_m = (x_a + x_b) / 2
239.     y_m = (y_a + y_b) / 2
240.     s = length(x_a, y_a, x_m, y_m)
241.     r = (s ** 2 + m) ** 0.5
242.     return RShape(x_m - r, y_m + r, x_m + r, y_m - r,
243.                   Cons(o_cons=OvalCons([(x_a, y_a, x_b, y_b, m)])))
244.
245. def process(mat):
246.     """Constructs the requested drawing."""
247.     spectrum_shape = Shape([circle(0, 0, n_inf(mat))])
248.     n = len(mat)
249.     g_l_shape = Shape(
250.         [circle(*to_point(mat[i][i]), line(mat, i)) for i in range(n)])
251.     print("!!!!! G_L_SHAPE !!!!!")
252.     print(g_l_shape)
253.     print_blank()
254.     g_c_shape = Shape(
255.         [circle(*to_point(mat[j][j]), column(mat, j)) for j in range(n)])
256.     print("!!!!! G_C_SHAPE !!!!!")
257.     print(g_c_shape)
258.     print_blank()
259.     oval_shape = Shape(
260.         [oval(*(to_point(mat[i][i]) + to_point(mat[j][j])),
261.               line(mat, i) * column(mat, j))
262.          for i in range(n)
263.          for j in range(n)])
264.     print("!!!!! OVAL_SHAPE !!!!!")
265.     print(oval_shape)
266.     print_blank()
267.     g_shape = Shape.inter_shape(g_l_shape, g_c_shape)
268.     k_shape = Shape.inter_shape(oval_shape, spectrum_shape)
269.     print("!!!!! G_SHAPE !!!!!")
270.     print(g_shape)
271.     print_blank()
272.     print("!!!!! K_SHAPE !!!!!")
273.     print(k_shape)
274.     print_blank()
275.     #result_shape = Shape.inter_shape(
276.     #    g_shape,
277.     #    k_shape)
278.     #print("!!!!! RESULT_SHAPE !!!!!")
279.     #print(result_shape)
280.     #print(result_shape)
281.
282. def main():
283.     """Launcher."""
284.     mat = [
285.         [-1, 2],
286.         [2, 3]
287.     ]
288.     process(mat)
289.
290. if __name__ == "__main__":
291.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top