sajjads programming blog

publish my programming stuffs on web

Wednesday, August 09, 2006

refractive_new.py





import pygame
import math
import pygame.font
from pygame.locals import *
color=(255,255,255)
u=0
sin_i=0
sin_r=0
r_index=1.33
emitter_y=20
value=200

deviation=0
screen=pygame.display.set_mode((1000,700))

river=pygame.image.load("scaled_river.jpeg")
def colorchooser():
pygame.draw.rect(screen,(255,255,255),(20,20,20,20),0)
pygame.draw.rect(screen,(255,0,0),(20,60,20,20),0)
pygame.draw.rect(screen,(0,255,0),(20,100,20,20),0)
pygame.draw.rect(screen,(0,0,255),(20,140,20,20),0)
def prntrindex():
string='n2='+str(r_index)
text_d1 = font.render(string,1, (255,255,255))
textpos_d1 = ((900,80,48,24))
screen.blit(text_d1,textpos_d1)
def pgbar():
pygame.draw.line(screen,(255,255,0),(950,200),(950,300),5)
pygame.draw.polygon(screen,(255,255,0),((940,175),(960,175),(950,190)) ,0)
pygame.draw.polygon(screen,(255,255,0),((940,325),(960,325),(950,310)) ,0)

i=200
while i<=300: if value==i: pygame.draw.line(screen,(255,255,0),(940,i),(960,i),5) else: pygame.draw.line(screen,(255,255,0),(940,i),(960,i),2) i+=20 def inc_angle(): opposite_side=400 dx=400 dy=400-emitter_y #hypt=pow((pow(dx,2)+pow(dy,2)),.5) hypt=math.sqrt(dx*dx +dy*dy) sin_i=opposite_side/hypt degr=math.degrees(math.asin(sin_i)) # opposite side=(100,emitter_y)-(100,400) #print incident angle string='i='+str(int(degr)) text_d1 = font.render(string,1, (255,255,255)) textpos_d1 = ((900,20,48,24)) screen.blit(text_d1,textpos_d1) return sin_i def emitter(): inci=inc_angle() sin_r=inci/r_index refr=math.degrees(math.asin(sin_r)) # print refracted angle string2='r='+str(int(refr)) text_r = font.render(string2,1, (255,255,255)) textpos_r = ((900,50,48,24)) screen.blit(text_r,textpos_r) # evaluating x for drawing cos_r=math.cos(math.asin(sin_r)) refracted_x= math.sqrt(pow(300/cos_r,2)-(300*300))+500 if color==(255,0,0): deviation=40 elif color==(0,255,0): deviation=20 elif color==(0,0,255): deviation=0 if color==(255,255,255): pygame.draw.line(screen,(255,0,0),(500,400),(refracted_x+40,700),1) pygame.draw.line(screen,(0,255,0),(500,400),(refracted_x+20,700),1) pygame.draw.line(screen,(0,0,255),(500,400),(refracted_x,700),1) else: pygame.draw.line(screen,color,(500,400),(refracted_x+deviation,700),1) def refra(): screen.fill((0,0,0)) screen.blit(river,(0,400)) #emitter position selecter pygame.draw.rect(screen,(255,255,0),(100,20,3,285),0) #emitter line pygame.draw.line(screen,color,(100,emitter_y),(500,400),2) #normal pygame.draw.line(screen,(255,255,255),(500,0),(500,700),1) #refracted line emitter() pgbar() pygame.init() font=pygame.font.Font(None,25) while 1: event=pygame.event.wait() if event.type is MOUSEBUTTONDOWN: x,y=pygame.mouse.get_pos() #print 'x=', x, 'y=',y if x> 95 and x<105>20 and y<305: emitter_y="y">20 and x<40:>20 and y<40: color="(255,255,255)">60 and y<80: color="(255,0,0)">100 and y<120: color="(0,255,0)">140 and y<160: color="(0,0,255)">940 and x<960>175 and y<190:>2:
r_index=2
if value>300:
value=300
elif x>940 and x<960>310 and y<325:
value-=20
r_index-=.2
if r_index<1:
r_index=1
if value<200:
value=200


refra()
prntrindex()
colorchooser()
pygame.display.update()

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home