Skip to content

Instantly share code, notes, and snippets.

@DarioSucic
Created May 14, 2021 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarioSucic/92d1a75bfea6f1c11c19f9a47d511094 to your computer and use it in GitHub Desktop.
Save DarioSucic/92d1a75bfea6f1c11c19f9a47d511094 to your computer and use it in GitHub Desktop.
Signed Angle Delta Visualization
from math import *
import pyglet as pgl
w, h = 540, 540
cx, cy = w//2, h//2
radius = 3*cx // 5
φ = pi / 4
config = pgl.gl.Config(sample_buffers=1, samples=16)
window = pgl.window.Window(w, h, config=config)
batch = pgl.graphics.Batch()
RED, GREEN, BLUE, GRAY = (224, 96, 96), (96, 224, 96), (96, 96, 224), (64, 64, 64)
p = 0.1
grid_x = pgl.shapes.Line(p*w, cy, (1-p)*w, cy, width=2, color=GRAY, batch=batch)
grix_y = pgl.shapes.Line(cx, p*h, cx, (1-p)*h, width=2, color=GRAY, batch=batch)
circle = pgl.shapes.Arc(cx, cy, radius, batch=batch)
point_opts = dict(x=-5, y=-5, radius=7, batch=batch)
point_mouse = pgl.shapes.Circle(color=RED, **point_opts)
point_fixed = pgl.shapes.Circle(color=GREEN, **point_opts)
point_fixed.position = cx+radius*cos(φ), cy+radius*sin(φ)
text_opts = dict(font_name="Consolas", anchor_x="left", anchor_y="top", batch=batch)
text_1 = pgl.text.Label("", x=24, y=h-1*24, color=RED + (255,), **text_opts)
text_2 = pgl.text.Label("", x=24, y=h-2*24, color=GREEN + (255,), **text_opts)
text_3 = pgl.text.Label("", x=24, y=h-3*24, color=BLUE + (255,), **text_opts)
text_4 = pgl.text.Label("", x=24, y=h-4*24, color=GRAY + (255,), **text_opts)
def angle_delta(θ, φ):
abs_diff = abs(θ - φ)
return min(abs_diff, 2*pi - abs_diff)
def is_further_clockwise(θ, θφ):
"""True if θ is "further" clockwise than θ"""
return (θ > φ) ^ (abs(θ - φ) < pi)
def signed_angle_delta(θ, φ):
"""Basically `angle_delta` and `is_further_clockwise` combined"""
abs_diff = abs(θ - φ)
abs_angle_delta = min(abs_diff, 2*pi - abs_diff)
sign = 1 if (abs_diff < pi) ^ (θ > φ) else -1
return abs_angle_delta * sign
@window.event
def on_mouse_motion(x, y, dx, dy):
θ = atan2(y-cy, x-cx)
# Round to radian values that correspond to integer degrees
θ = radians(int(degrees(θ)))
point_mouse.position = cx+radius*cos(θ), cy+radius*sin(θ)
text_1.text = f"θ = {degrees(θ):4.0f}°"
text_2.text = f"φ = {degrees(φ):4.0f}°"
text_3.text = f"Δ = {degrees(signed_angle_delta(θ, φ)):4.0f}°"
text_4.text = "RIGHT" if is_further_clockwise(θ, φ) else "LEFT"
@window.event
def on_draw():
window.clear()
batch.draw()
pgl.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment