import pygame
from pygame.locals import *
from math import *
from sys import exit
from random import *
def xydist(p1,p2):
return sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )
def findb(px,py,ny,nx):
b = py - (float(ny)/float(nx))*(px)
return b
def findi(ny,nx, tx, ty, b1):
t=[]
t.append( (10, (ny/nx)*(10) + b1 ))
t.append( (tx, (ny/nx)*(tx) + b1))
t.append( ((10-b1)*nx/ny , 10) )
t.append( ((ty-b1)*nx/ny , ty) )
return t
def showi(p):
temp=[]
for data in p:
if data[0]>0 and data[0]<1020 and data[1]>0 and data[1]<520:
temp.append (data)
return temp
###
def main():
pygame.init()
my_font = pygame.font.SysFont("arial", 16)
pygame.display.set_caption("First Reflection")
screen = pygame.display.set_mode((1200,800),0,32)
state = 0
xup=1010
yup=510
xup2=250
yup2=90
ox,oy=300,150
while (state==0):
colors=[(255,255,255),(55,55,200)]
blue= (55,55,200)
white=(255,255,255)
color1=(10,100,200)
color2=(10,10,200)
color3=(10,10,20)
colors.append
screen.fill((0,0,0))
for i in range(4):
pygame.draw.line(screen,(255,255,0),(10,10+125*i),(xup,10+125*i))
for i in range(8):
pygame.draw.line(screen,(255,255,0),(10+125*i,10),(10+125*i,yup))
pygame.draw.line(screen,colors[0],(10,10),(xup,10))
pygame.draw.line(screen,colors[0],(10,yup),(xup,yup))
pygame.draw.line(screen,colors[0],(10,10),(10,yup))
pygame.draw.line(screen,colors[0],(xup,10),(xup,yup))
pygame.draw.circle(screen,blue,(ox,oy),20,1)
pygame.draw.circle(screen,blue,(xup2,yup2),20,1)
pygame.draw.line(screen,colors[0],(ox,oy),(xup2,yup2))
dy=float(oy-yup2)
dx=float(ox-xup2)
if dx!=0 and dy!=0:
b=findb(ox,oy,dy,dx)
r = findi(dy,dx,xup,yup, b)
rp = showi(r)
for data in rp:
pygame.draw.line(screen,(255,255,0),(ox,oy),data)
pygame.draw.line(screen,(255,255,0),(xup2,yup2),data)
pygame.draw.circle(screen,(255,255,255),data,15,1)
b2=findb(rp[1][0],rp[1][1],-dy,dx)
r2 = findi(-dy,dx,xup,yup, b2)
rp2 = showi(r2)
for data in rp2:
#pygame.draw.line(screen,(255,255,0),(rp[1][0],rp[1][1]),data)
pygame.draw.circle(screen,(255,255,255),data,15,1)
pygame.draw.circle(screen,(255,255,255),rp2[0],15,1)
pygame.draw.circle(screen,(255,255,255),rp2[1],15,1)
pygame.draw.line(screen,(255,255,0),rp2[0],rp2[1],15)
b3=findb(rp[0][0],rp[0][1],-dy,dx)
r3 = findi(-dy,dx,xup,yup, b3)
rp3 = showi(r3)
for data in rp2:
pygame.draw.line(screen,(255,255,0),(rp[1][0],rp[1][1]),data)
pygame.draw.circle(screen,(255,255,255),data,15,1)
if len(rp3)>=1:
pygame.draw.circle(screen,(255,255,255),rp3[0],20,1)
pygame.draw.circle(screen,(5,5,255),rp3[1],20,1)
else:
pygame.draw.circle(screen,(255,255,255),rp3[0],20,1)
#REFLECTIONO2
pygame.draw.line(screen,(255,255,0),rp3[0],rp3[1],15)
b4=findb(rp3[0][0],rp3[0][1],dy,dx)
r4 = findi(dy,dx,xup,yup, b4)
rp4 = showi(r4)
for data in rp4:
pygame.draw.line(screen,(255,255,0),rp3[0],data)
pygame.draw.circle(screen,(255,255,255),data,20,1)
if dy==0:
pygame.draw.line(screen,(255,255,0),(xup,oy),(10,oy))
elif dx==0:
pygame.draw.line(screen,(255,255,0),(ox,10),(ox,yup))
#####
else:
r1=(xup2,yup2)
r2=(ox,oy)
pygame.draw.line(screen,white,r1,r2)
for event in pygame.event.get():
if event.type == QUIT:
state = 1
if event.type == KEYDOWN:
if event.key == K_a:
ox-=10
elif event.key == K_s:
oy+=10
elif event.key == K_d:
ox+=10
elif event.key == K_w:
oy-=10
elif event.key == K_g:
xup2-=10
elif event.key == K_h:
yup2+=10
elif event.key == K_j:
xup2+=10
elif event.key == K_y:
yup2-=10
elif event.key == K_z:
ox,oy=pygame.mouse.get_pos()
elif event.key == K_x:
xup2,yup2=pygame.mouse.get_pos()
pygame.display.update()
main()
pygame.quit()