-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimization_Code_Pygame.py
More file actions
117 lines (94 loc) · 3.42 KB
/
Copy pathOptimization_Code_Pygame.py
File metadata and controls
117 lines (94 loc) · 3.42 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
import pygame
from shapely.geometry import Point, Polygon
import numpy as np
import math
import time
pygame.init()
def create_screen(width, height):
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Modules on Land")
return screen
class Land:
def __init__(self, screen, exterior_coords):
self.exterior_coords = exterior_coords
self.polygon = Polygon(exterior_coords)
self.modules = []
def draw_land(self):
pygame.draw.polygon(screen, (0, 0, 255), self.exterior_coords, 1)
def add_module(self, Module):
self.modules.append(Module)
def draw_modules(self):
for module in self.modules:
if module.get_module().within(self.polygon):
module.draw_module()
else:
pygame.draw.circle(screen, (255, 0, 0), module.get_coord(), 5)
def expand_sound_modules(self):
for module in self.modules:
if module.get_module().within(self.polygon):
module.expand_sound_module()
for module in self.modules:
if module.get_module().within(self.polygon):
module.draw_module()
def calculate_fitness(self):
bounding_box = self.polygon.bounds
coverage = 0
for x in range(int(bounding_box[0])+1, int(bounding_box[2])):
for y in range(int(bounding_box[1])+1, int(bounding_box[3])):
if self.polygon.contains(Point(x,y)):
color_at_pixel = screen.get_at((x, y))
coverage += 255-color_at_pixel[0]
#overlap = self.check_overlapping_areas()
#coverage -= overlap
print(coverage)
"""def check_overlapping_areas(self):
module_list = self.modules
for module in self.modules:
for other_module in module_list:
if module.point.buffer(166):
pass
other_module.pop(0)"""
class Module:
def __init__(self, screen, coord):
self.coord = coord
self.point = Point(coord)
self.module = self.point.buffer(5)
def get_module(self):
return self.module
def get_coord(self):
return self.coord
def draw_module(self):
pygame.draw.circle(screen, (0, 255, 0), self.coord, 5)
def expand_sound_module(self):
min_radius = 6
current_radius = 21
for i in range(6, 22):
#sound intensity = 255/d^2
#scaling distance by x10
#reversed the colors by subtracting from 255 so it goes from dark to light
x = (i-5)**2
current_color = 255-255/x
pygame.draw.circle(screen, (current_color, current_color, 0), self.coord, (i-5)*10+6, 10)
#self.draw_module()
screen = create_screen(1200, 800)
exterior_coords = [[200, 200], [200, 600], [1000, 600], [1000, 200]]
land = Land(screen, exterior_coords)
coord = [300, 400]
module = Module(screen, coord)
land.add_module(module)
second_coord = [800, 400]
second_module = Module(screen, second_coord)
land.add_module(second_module)
outside_coord = [100,100]
outside_module = Module(screen, outside_coord)
land.add_module(outside_module)
screen.fill((255,255,255))
land.draw_land()
module.draw_module()
module.expand_sound_module()
second_module.draw_module()
second_module.expand_sound_module()
land.calculate_fitness()
pygame.display.update()
x= input()
pygame.quit()