-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek5.py
More file actions
121 lines (95 loc) · 3.4 KB
/
Copy pathweek5.py
File metadata and controls
121 lines (95 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from random import randint
from PIL import Image
from Geometry import *
size = 1000
image = Image.new("RGB", (size, size), color="white")
draw = ImageDraw.Draw(image)
middle = size // 2
def generateLines(length, count):
lines = []
for i in range(count):
x1 = randint(length, size - length)
y1 = randint(length, size - length)
angle = math.radians(randint(0, 360))
x2 = x1 + math.sin(angle) * length
y2 = y1 + math.cos(angle) * length
lines.append(Line(Point(x1, y1), Point(x2, y2)))
return lines
def drawLines(lines):
for line in lines:
draw.line((line.p1.x, line.p1.y, line.p2.x, line.p2.y), fill=(0, 0, 0))
def draw_intersects(lines):
for first in lines:
for second in lines:
intersect = first.intersect(second)
r = 5
if intersect is not None:
x = intersect.x
y = intersect.y
draw.ellipse((x - r, y - r, x + r, y + r), fill=(0, 0, 0))
def linesWithIntersects(length, count):
lines = generateLines(length, count)
drawLines(lines)
draw_intersects(lines)
image.save("images5\\linesWithIntersects.png")
def generateAndDrawPoints(numberOfPoints):
points = []
r = 5
for i in range(numberOfPoints):
x = randint(0, size)
y = randint(0, size)
points.append(Point(x, y))
draw.ellipse((x - r, y - r, x + r, y + r), fill=(0, 0, 0))
return points
def triangulation(density, sort):
points = generateAndDrawPoints(density)
chosen = []
lines = []
for point1 in points:
for point2 in points:
if point1 != point2:
lines.append(Line(point1, point2))
if sort:
lines.sort(key=lambda x: x.length())
for line in lines:
intersected = False
for selectedLine in chosen:
if line.intersect(selectedLine) is not None:
intersected = True
break
if not intersected:
chosen.append(line)
drawLines(chosen)
image.save("images5\\triangulationSortedUniform.png")
def findLeftMostPoint(points):
result = points[0]
for point in points:
if point.x < result.x and point.y < result.y:
result = point
return result
def isLeftOfLine(point, line):
return ((line.p2.x - line.p1.x) * (point.y - line.p1.y) - (line.p2.y - line.p1.y) * (point.x - line.p1.x)) > 0
def convexHull(points):
pointOnHull = findLeftMostPoint(points)
pointsOnHull = []
endPoint = points[0]
while len(pointsOnHull) == 0 or pointsOnHull[0] != endPoint: # we finish when we close convex hull
pointsOnHull.append(pointOnHull)
for point in points:
if (endPoint == pointOnHull) or (
isLeftOfLine(point, Line(pointOnHull, endPoint))): # we search for leftMost point
endPoint = point
draw.line((pointOnHull.x, pointOnHull.y, endPoint.x, endPoint.y), fill=10)
pointOnHull = endPoint
return [x for x in points if x not in pointsOnHull] # returns points which are not in convex hull
def layeredConvexHull(density):
points = generateAndDrawPoints(density)
while len(points) > 1:
points = convexHull(points)
image.save("images5\\convexHull.png")
# linesWithIntersects(200, 50)
# triangulationBasic(30)
# triangulation(30, False)
# triangulation(30, True)
layeredConvexHull(50)
image.show()