geom-demo/geom/demos/reflection.py
2020-11-16 13:06:11 +04:00

53 lines
1.4 KiB
Python

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):
self.s_angle = -pi/4
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
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)