geom-demo/geom/demos/reflection.py

54 lines
1.5 KiB
Python
Raw Normal View History

2020-11-16 02:06:11 -07:00
import pygame
from ..geom import Polygon, generate_polygon
from .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs
from ..colors import *
from ..math import *
from ..draw import *
from math import pi, sin, cos
class Reflection:
title = "Reflection"
def __init__(self):
2020-11-20 01:39:16 -07:00
self.s_angle = -pi/8
2020-11-16 02:06:11 -07:00
def handle_key_down(self, key):
pass
def handle_input(self):
inputs = get_inputs()
if inputs[pygame.K_q]:
self.s_angle -= (1/60) * pi/3
if inputs[pygame.K_e]:
self.s_angle += (1/60) * pi/3
def render(self, surface):
self.handle_input()
s_end = scale(cos(self.s_angle),
sin(self.s_angle),
WIDTH)
s_start = reverse(*s_end)
# the reflecting surface
s = sub(*s_end, *s_start)
# the incoming ray, towards reflection surface
2020-11-20 01:39:16 -07:00
# it's important that it is pointing towards the reflecting surface
2020-11-16 02:06:11 -07:00
incoming = sub(0, 0, *get_cursor_pos())
# surface normal
normal = normalize(*lnormal(*s))
# projection of incoming ray onto normal, multiplied by 2
projection_2 = dot(*incoming, *normal) * 2
# outgoing ray, away from reflection surface
outgoing = sub(*incoming, *scale(*normal, projection_2))
line(surface, WHITE, s_start, s_end)
line(surface, CYAN, (0, 0), scale(*normal, 1000))
line(surface, YELLOW, get_cursor_pos(), (0, 0))
line(surface, RED, (0, 0), outgoing)