Last active
August 6, 2016 14:04
-
-
Save adusak/1a5b8771996cab5edb06 to your computer and use it in GitHub Desktop.
Triangluation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from python.common import line as ln | |
| from python.common.svg import Svg | |
| from python.less5.generate_points import generate_points | |
| def triangulate(number_of_points, canvas_size, sort=True, name="triangulation", gauss=False): | |
| svg = Svg() | |
| points = generate_points(number_of_points, canvas_size, gauss) | |
| points = [(round(x[0]), round(x[1])) for x in points] | |
| all_lines = [] | |
| for p1 in points: | |
| for p2 in points: | |
| if p1 != p2: | |
| all_lines.append(ln.Line(p1[0], p1[1], p2[0], p2[1])) | |
| if sort: | |
| all_lines = sorted(all_lines, key=lambda line: line.len()) | |
| else: | |
| random.shuffle(all_lines) | |
| painted_lines = [] | |
| for l1 in all_lines: | |
| intersect = None | |
| for l2 in painted_lines: | |
| intersect = ln.line_intersect(l1, l2) | |
| if intersect is not None: | |
| if not (ln.endpoint_on_line(intersect, l1)): | |
| break | |
| else: | |
| intersect = None | |
| if intersect is None: | |
| svg.add_line(l1) | |
| painted_lines.append(l1) | |
| svg.save(name) | |
| # triangulate(150, (1920, 1070), False, name="triangulation_gauss", gauss=True) # Obr. 1 | |
| # triangulate(150, (1920, 1070), False, name="triangulation") # Obr. 2 | |
| # triangulate(150, (1920, 1070), True, name="triangulation_sorted_gauss", gauss=True) # Obr. 3 | |
| # triangulate(150, (1920, 1070), True, name="triangulation_sorted") # Obr. 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment