Created
July 5, 2024 03:15
-
-
Save tigercoding56/ebe367f0450bd1cbbba4061cb27398d3 to your computer and use it in GitHub Desktop.
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 cv2 | |
import numpy as np | |
import math | |
import pygame | |
zs=200 | |
#sample = light() | |
def cp(p): | |
#p=list(p) | |
#p[1]=400-p[1] | |
return p | |
class light: | |
class sector: | |
def __init__(self,a1,a2,d1,d2): | |
self.angles = [a1,a2] | |
self.angles.sort() | |
self.distances=[d1,d2] | |
def reduce(self,sector2): | |
if self.angles[1] < sector2.angles[0] or self.angles[1] >sector2.angles[1]: | |
return [self,sector2] | |
else: | |
if (self.angles[0]>sector2.angles[0] and self.angles[1]<sector2.angles[1]): | |
return [sector2] | |
if (self.angles[0]<sector2.angles[0] and self.angles[1]>sector2.angles[1]): | |
return [self] | |
if (self.angles[0]==sector2.angles[0]) or (self.angles[1]== sector2.angles[1]): | |
self.distances = [min(self.distances[i],sector2.distances[i]) for i in [0,1]] | |
return [self] | |
if (self.angles[0]< sector2.angles[1]): | |
if (self.distances[0]<sector2.distances[1]): | |
sector2.angles[1] = self.angles[0] | |
else: | |
self.angles[0] =sector2.angles[1] | |
else: | |
if (self.distances[0]<sector2.distances[1]): | |
sector2.angles[0] = self.angles[1] | |
else: | |
self.angles[1] = sector2.angles[0] | |
return [self,sector2] | |
def __init__(self,lines,pos,ml): | |
self.lines = lines | |
self.pos=pos | |
self.sectors=[] | |
self.relsectors=[] | |
self.maxlenght=ml | |
def abw(self, p2): | |
x1,y1=self.pos | |
x2,y2=p2 | |
dx = x2 - x1 | |
dy = y2 - y1 | |
angle = math.atan2(dy, dx) | |
angle = 360-math.degrees(angle)+90 | |
if angle < 0: | |
angle += 360 | |
return angle | |
def gom(self,ang,d=1,o=(0,0)): | |
u=np.deg2rad(ang) | |
return list(((np.sin(u)*d)+o[0],(np.cos(u)*d)+o[1])) | |
def dbw(self,p2): | |
return np.linalg.norm((p2[0]-self.pos[0],p2[1]-self.pos[1])) | |
def l(self,screen): | |
self.sectors=[] | |
for i in self.lines: | |
_1=(int(self.abw(i[0])))%360 | |
_2=(int(self.abw(i[1])))%360 | |
mp = ((i[0][0]+i[1][0])*0.5,(i[0][1]+i[1][1])*0.5) | |
_3=int(self.abw(mp))%360 | |
_D1=self.dbw(i[0]) | |
_D2 =self.dbw(i[1]) | |
_D3 =self.dbw(mp) | |
if _D3 < self.maxlenght: | |
self.sectors.append(self.sector(_1,_2,_D1,_D2)) | |
for i in range(0,len(self.sectors)): | |
idx=i | |
if (idx<len(self.sectors)): | |
for w in range(0,len(self.sectors)): | |
if (w<len(self.sectors)) and i!=w: | |
sctr=self.sector.reduce(self.sectors[idx],self.sectors[w]) | |
if len(sctr)==1: | |
self.sectors[idx]=sctr[0] | |
if (w<idx): | |
idx-=1 | |
del(self.sectors[w]) | |
else: | |
self.sectors[idx],self.sectors[w] = sctr | |
for i in self.sectors: | |
_1,_2=i.angles | |
_D1,D2=i.distances | |
_1P=list(self.gom(_1,min(self.maxlenght,_D1),self.pos)) | |
_2P=list(self.gom(_2,min(self.maxlenght,_D2),self.pos)) | |
pygame.draw.polygon(screen, (255,0,0), (_1P,self.pos,_2P)) | |
def getlines(pygame_image): | |
pygame_array = pygame.surfarray.array3d(pygame_image) | |
opencv_array = np.transpose(pygame_array, (1, 0, 2)) | |
image = cv2.cvtColor(opencv_array, cv2.COLOR_RGB2BGR) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
_garbage, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) | |
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
simplified_contours = [] | |
for cnt in contours: | |
epsilon = 0.005 * cv2.arcLength(cnt, True) | |
approx = cv2.approxPolyDP(cnt, epsilon, True) | |
simplified_contours.append(approx) | |
all_lines=[] | |
for cnt in simplified_contours: | |
s=tuple(cnt[0][0]) | |
lines=[] | |
for i in range(1,len(cnt)): | |
lines.append([s,list(cnt[i][0])]) | |
s=tuple(cnt[i][0]) | |
lines.append([s,tuple(cnt[0][0])]) | |
all_lines+=lines | |
return all_lines | |
pimage=pygame.image.load('image.png') | |
lines=getlines(pimage) | |
print(len(lines)) | |
samplelight=light(lines,(200,200),110) | |
print(lines) | |
ind=-1 | |
width, height = 800, 600 | |
screen = pygame.display.set_mode((width, height)) | |
pygame.display.set_caption("My Pygame App") | |
pygame.init() | |
# Game loop | |
running = True | |
while running: | |
# Handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
# Clear the screen | |
screen.fill((0, 0, 0)) | |
txt= pygame.key.get_pressed() | |
if txt[pygame.K_UP]==1: | |
zs+=20 | |
print("correction needed") | |
print(zs) | |
print("distance") | |
print(samplelight.pos[0]-337) | |
if txt[pygame.K_DOWN]==1: | |
zs-=20 | |
print(zs) | |
print(samplelight.pos) | |
# Draw something | |
screen.blit(pimage,(0,0)) | |
pygame.draw.circle(screen,(0,255,0),samplelight.pos,5) | |
samplelight.l(screen) | |
samplelight.pos=pygame.mouse.get_pos() | |
# Update the display | |
pygame.display.flip() | |
# Quit Pygame | |
pygame.quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment