Refactor some code, and draw a grid of points.
This commit is contained in:
86
main.py
86
main.py
@@ -10,6 +10,9 @@ CENTER_X, CENTER_Y = 400, 300
|
||||
alg = Algebra(2,1,0)
|
||||
locals().update(alg.blades)
|
||||
|
||||
points = []
|
||||
lines = []
|
||||
|
||||
def point(x,y):
|
||||
#Construct a point.
|
||||
#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_1 = -point.e23
|
||||
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_1 = x_1/norm
|
||||
x_2 = x_2/norm
|
||||
@@ -37,11 +43,12 @@ def draw_line(line):
|
||||
delta = line.e3
|
||||
if abs(delta) < ERROR:
|
||||
# The geodesic is a line through the origin.
|
||||
if abs(line.e2) < ERROR:
|
||||
pygame.draw.line(screen, BLUE, (CENTER_X, CENTER_Y+DISK_RADIUS), (CENTER_X, CENTER_Y-DISK_RADIUS), 1)
|
||||
else:
|
||||
slope = line.e1/line.e2
|
||||
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)
|
||||
theta = math.atan2(line.e1, line.e2)
|
||||
start_x = int(CENTER_X-DISK_RADIUS*math.cos(theta))
|
||||
start_y = int(CENTER_Y-DISK_RADIUS*math.sin(theta))
|
||||
end_x = int(CENTER_X+DISK_RADIUS*math.cos(theta))
|
||||
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:
|
||||
#The geodesic is a circle
|
||||
a = line.e1
|
||||
@@ -55,7 +62,33 @@ def draw_line(line):
|
||||
c_y = int(CENTER_Y + y_c*DISK_RADIUS)
|
||||
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
|
||||
@@ -83,49 +116,28 @@ BLUE = (0, 0, 255)
|
||||
running = True
|
||||
t = 0
|
||||
|
||||
lines.append(x_axis)
|
||||
lines.append(y_axis)
|
||||
while running:
|
||||
t += 0.0002
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# Clear the screen
|
||||
screen.fill(WHITE)
|
||||
|
||||
#Poincare Disk Boundary
|
||||
pygame.draw.circle(screen, BLACK, (CENTER_X, CENTER_Y), DISK_RADIUS, 1)
|
||||
|
||||
#Generate a transformation for demonstration
|
||||
transform = math.sin(t)*tx_generator + math.sin(math.sqrt(2)*t)*ty_generator + math.sin(math.sqrt(3)*t)*origin
|
||||
transform = transform
|
||||
motor = transform.exp()
|
||||
simulation(t)
|
||||
for line in lines:
|
||||
draw_line(line)
|
||||
for point in points:
|
||||
draw_point(point)
|
||||
|
||||
#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)
|
||||
|
||||
tx = tx_generator.exp()
|
||||
ty = ty_generator.exp()
|
||||
point_a = tx*ty*origin*ty.reverse()*tx.reverse()
|
||||
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)
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
# Update the display
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
|
||||
# Quit Pygame
|
||||
pygame.quit()
|
||||
|
||||
Reference in New Issue
Block a user