diff --git a/geom/demos/reflection.py b/geom/demos/reflection.py new file mode 100644 index 0000000..864aaca --- /dev/null +++ b/geom/demos/reflection.py @@ -0,0 +1,52 @@ +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) diff --git a/geom/screen.py b/geom/screen.py index d3bc3f6..d5adf23 100644 --- a/geom/screen.py +++ b/geom/screen.py @@ -1,5 +1,5 @@ import pygame -from .demos import aabb, vec_proj, sat +from .demos import aabb, vec_proj, sat, reflection from . import HEIGHT from .colors import * from .draw import text_screen @@ -9,6 +9,7 @@ class Screen: def __init__(self): self.demos = [ vec_proj.VecProj(), + reflection.Reflection(), aabb.AABBDistance(), aabb.PointAABBDistance(), sat.SeparatingAxisTheorem(),