Refactor some code, and draw a grid of points.

This commit is contained in:
2025-08-16 11:35:39 -05:00
parent e797470103
commit 554082d0e2

84
main.py
View File

@@ -10,6 +10,9 @@ CENTER_X, CENTER_Y = 400, 300
alg = Algebra(2,1,0) alg = Algebra(2,1,0)
locals().update(alg.blades) locals().update(alg.blades)
points = []
lines = []
def point(x,y): def point(x,y):
#Construct a point. #Construct a point.
#Hyperbolic space is modeled as a hyperboloid embedded in 3D Minkowski space. #Hyperbolic space is modeled as a hyperboloid embedded in 3D Minkowski space.
@@ -23,7 +26,10 @@ def draw_point(point):
x_0 = point.e12 x_0 = point.e12
x_1 = -point.e23 x_1 = -point.e23
x_2 = point.e13 x_2 = point.e13
norm = math.sqrt(x_0*x_0 - x_1*x_1 - x_2*x_2) sqr = x_0*x_0 - x_1*x_1 - x_2*x_2
if sqr < 0:
return
norm = math.sqrt(sqr)
x_0 = x_0/norm x_0 = x_0/norm
x_1 = x_1/norm x_1 = x_1/norm
x_2 = x_2/norm x_2 = x_2/norm
@@ -37,11 +43,12 @@ def draw_line(line):
delta = line.e3 delta = line.e3
if abs(delta) < ERROR: if abs(delta) < ERROR:
# The geodesic is a line through the origin. # The geodesic is a line through the origin.
if abs(line.e2) < ERROR: theta = math.atan2(line.e1, line.e2)
pygame.draw.line(screen, BLUE, (CENTER_X, CENTER_Y+DISK_RADIUS), (CENTER_X, CENTER_Y-DISK_RADIUS), 1) start_x = int(CENTER_X-DISK_RADIUS*math.cos(theta))
else: start_y = int(CENTER_Y-DISK_RADIUS*math.sin(theta))
slope = line.e1/line.e2 end_x = int(CENTER_X+DISK_RADIUS*math.cos(theta))
pygame.draw.line(screen, BLUE, (CENTER_X-DISK_RADIUS, int(CENTER_Y-slope*DISK_RADIUS)), (CENTER_X+DISK_RADIUS, int(CENTER_Y+slope*DISK_RADIUS)), 1) end_y = int(CENTER_Y+DISK_RADIUS*math.sin(theta))
pygame.draw.line(screen, BLUE, (start_x, start_y), (end_x, end_y), 1)
else: else:
#The geodesic is a circle #The geodesic is a circle
a = line.e1 a = line.e1
@@ -55,7 +62,33 @@ def draw_line(line):
c_y = int(CENTER_Y + y_c*DISK_RADIUS) c_y = int(CENTER_Y + y_c*DISK_RADIUS)
pygame.draw.circle(screen, BLUE, (c_x, c_y), int(r*DISK_RADIUS), 1) pygame.draw.circle(screen, BLUE, (c_x, c_y), int(r*DISK_RADIUS), 1)
def simulation(t):
lines.clear()
points.clear()
lines.append(y_axis)
lines.append(x_axis)
points.append(origin)
x_motor = tx_generator.exp()
y_motor = ty_generator.exp()
generator = math.sin(t)*tx_generator + math.sin(math.sqrt(2)*t)*ty_generator + math.sin(math.sqrt(3)*t)*origin
m = generator.exp()
y_tick = m*y_axis*m.reverse()
x_tick = m*x_axis*m.reverse()
lines.append(y_tick)
lines.append(x_tick)
points.append(y_tick^x_tick)
for i in range(13):
for j in range(13):
motor_x = ((i-6)*tx_generator/10).exp()
motor_y = ((j-6)*ty_generator/10).exp()
y_tick = m * motor_x * y_axis * motor_x.reverse() * m.reverse()
x_tick = m * motor_y * x_axis * motor_y.reverse() * m.reverse()
lines.append(y_tick)
lines.append(x_tick)
points.append(y_tick^x_tick)
y_axis = e1 #x = 0 line y_axis = e1 #x = 0 line
@@ -83,49 +116,28 @@ BLUE = (0, 0, 255)
running = True running = True
t = 0 t = 0
lines.append(x_axis)
lines.append(y_axis)
while running: while running:
t += 0.0002 t += 0.0002
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen # Clear the screen
screen.fill(WHITE) screen.fill(WHITE)
#Poincare Disk Boundary #Poincare Disk Boundary
pygame.draw.circle(screen, BLACK, (CENTER_X, CENTER_Y), DISK_RADIUS, 1) pygame.draw.circle(screen, BLACK, (CENTER_X, CENTER_Y), DISK_RADIUS, 1)
#Generate a transformation for demonstration simulation(t)
transform = math.sin(t)*tx_generator + math.sin(math.sqrt(2)*t)*ty_generator + math.sin(math.sqrt(3)*t)*origin for line in lines:
transform = transform draw_line(line)
motor = transform.exp() for point in points:
#Apply the transformation
y_tick = motor*y_axis*motor.reverse()
x_tick = motor*x_axis*motor.reverse()
#Construct a point as the intersection of the two lines
point = y_tick^x_tick
#Draw everything
draw_line(y_tick)
draw_line(x_tick)
draw_point(point) draw_point(point)
tx = tx_generator.exp() for event in pygame.event.get():
ty = ty_generator.exp() if event.type == pygame.QUIT:
point_a = tx*ty*origin*ty.reverse()*tx.reverse() running = False
point_b = ty*tx*origin*tx.reverse()*ty.reverse()
draw_line(x_axis)
draw_line(y_axis)
draw_point(origin)
draw_point(point_a)
draw_point(point_b)
# Update the display # Update the display
pygame.display.flip() pygame.display.flip()
# Quit Pygame # Quit Pygame
pygame.quit() pygame.quit()