rtx3.py

Created by chick-chicky

Created on May 01, 2024

2.14 KB


from math import*;from kandinsky import*
def k(c):return max(0,min(255,floor(c)))
class V:
 def __init__(s,x,y,z):s.x,s.y,s.z=x,y,z
 @staticmethod
 def n(v):return V(v,v,v)
 def ad(s,o):return V(s.x+o.x,s.y+o.y,s.z+o.z)
 def su(s,o):return V(s.x-o.x,s.y-o.y,s.z-o.z)
 def mu(s,o):return V(s.x*o.x,s.y*o.y,s.z*o.z)
 def di(s,o):return V(s.x/o.x,s.y/o.y,s.z/o.z)
 def m(s):return sqrt(s.x**2+s.y**2+s.z**2)
 def u(s):m=sqrt(s.x**2+s.y**2+s.z**2);return V(s.x/m,s.y/m,s.z/m)
 def c(s):return V(s.x,s.y,s.z)
class C:
 def __init__(s,r,g,b,a):s.r,s.g,s.b,s.a=r,g,b,a
 def c(s):return C(s.r,s.g,s.b,s.a)
 @staticmethod
 def r(r,g,b):return C(r,g,b,1)
 def ad(s,o):return C(k(s.r+o.r*o.a)),k(s.g+o.g*o.a),k(s.b+o.b*o.a),max(0,min(s.a+o.a,1))
 def su(s,o):return C(k(s.r-o.r*o.a)),k(s.g-o.g*o.a),k(s.b-o.b*o.a),max(0,min(s.a+o.a,1))
 def mu(s,o):return C(k(s.r*(o.r*o.a))),k(s.g*(o.g*o.a)),k(s.b*(o.b*o.a)),max(0,min(s.a+o.a,1))
 def di(s,o):return C(k(s.r/(o.r*o.a)),k(s.g/(o.g*o.a)),k(s.b/(o.b*o.a)),max(0,min(s.a+o.a,1)))
class S:
 def __init__(s,p:V,r:float,c:C):s.c=c.c();s.pos=p.c();s.r=r
 def i(s,p):return(s.pos.su(p)).m()<s.r
camerapos=V(0,0,0)
camerasize=5
cameralength=2
resolution=100,100
precision=50
maxrange=10
shapes=[S(V(0,0,2),1,C(255,0,0,1))]
enable_shadows=True
enable_reflections=False
max_reflections=1
lightpos=V(0,5,0)
voidColor=C(0,0,60,1)
p=V.n(precision)
def h(src,dr,bl):
 ray=src.c();i=0
 while i<precision*maxrange:
  ray=ray.ad(dr.di(p))
  for obj in shapes:
   if obj.i(ray):
    hit=False;color=obj.c
    if enable_shadows:
     d=lightpos.su(ray);n=ceil(d.m()*precision);s=d.u().di(V.n(precision));r=ray;dd=False;r=r.ad(s)
     for _ in range(n-1):
      r=r.ad(s)
      for objj in shapes:
       if objj.i(r):hit=dd=True
       if dd:break
      if dd:break
    if hit:color=color.di(C(4,4,4,1))
    return color.c()
  i+=1
 return voidColor
s=floor(240/resolution[1])
for px in range(0,resolution[0]):
 for py in range(0,resolution[1]):
  direction=V(camerasize/2-px/resolution[0]*camerasize,camerasize/2-py/resolution[1]*camerasize,cameralength);ray=camerapos.c();c=h(ray,direction,max_reflections);fill_rect(px*s,py*s,s,s,(c.r,c.g,c.b))
input()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.