From 239d07c654bf1fde79dd25c4c01300359327bba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Wed, 3 Apr 2024 16:20:42 +0500 Subject: [PATCH] Initial commit. --- .gitignore | 1 + LICENSE | 5 +++ gyro.py | 69 ++++++++++++++++++++++++++++++++++ lines.py | 14 +++++++ main.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 gyro.py create mode 100644 lines.py create mode 100755 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f22b800 --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +Copyright (C) 2024 by bedohswe bedohswe@noreply.localhost + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/gyro.py b/gyro.py new file mode 100644 index 0000000..6977336 --- /dev/null +++ b/gyro.py @@ -0,0 +1,69 @@ +def ZeroCheck(cN): + if cN == complex(0,0): + return complex(1,0) + else: + return cN + +def c_tr(cN): + return cN.real, cN.imag + +def cNorm(cN): + return ZeroCheck(cN)/abs(ZeroCheck(cN)) + +def cDot(cA, cB): + return (cA * cB.conjugate()).real + +def cDist(cA, cB): + return abs(cA-cB) + +def MobiusAdd(cA, cB): + return (cA + cB) / (1 + cA.conjugate() * cB) + +def MobiusGyr(cA, cB): + return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB) + +def MobiusAddGyr(cA, cB): + return MobiusAdd(cA, cB), MobiusGyr(cA, cB) + +def Poincare2Klein(cN): + return 2*cN / (1 + cDot(cN,cN)) + +class GyroVector: + def __init__(self, cPos, cRot): + self.cPos = complex(cPos) + self.cRot = complex(cRot) + self.normalize() + + def __add__(gA, gB): + cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos / ZeroCheck(gA.cRot)) + return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr) + + def __neg__(self): + return GyroVector(-(self.cRot * self.cPos), 1/self.cRot) + + def __sub__(gA, gB): + return gA + (-gB) + + def copy(self): + return GyroVector(self.cPos,self.cRot) + + def normalize(self): + if abs(self.cPos) > 1: + self.cPos = cNorm(self.cPos) + self.cRot = cNorm(self.cRot) + + def rotate(self,cRot): + self.cRot *= cRot + + def transform(self, cA): + cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot)) + self.cPos = cAdd + self.cRot = self.cRot * cGyr + + def transformed(self, cA): + #cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot)) + #return GyroVector(cAdd, self.cRot * cGyr) + return gA.copy().transform(cA) + + def nrtransformed(self, cA): + return MobiusAdd(self.cPos, cA / ZeroCheck(self.cRot)) diff --git a/lines.py b/lines.py new file mode 100644 index 0000000..76b2825 --- /dev/null +++ b/lines.py @@ -0,0 +1,14 @@ +def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4): + rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) + rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) + return (rX, rY) + +def cLineIntersection(cA0, cA1, cB0, cB1): + rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag) + return complex(rX, rY) + +def Between(rA,rB,rN): + return (rA < rN and rN < rB) or (rB < rN and rN < rA) + +def cBetween(cA,cB,cN): + return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag) diff --git a/main.py b/main.py new file mode 100755 index 0000000..c771e9b --- /dev/null +++ b/main.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 + +from time import time_ns, sleep +from cmath import exp +import pygame +import pygame.freetype +from gyro import * +from lines import * + + +WHITE = (255,255,255) +BLACK = (0,0,0) +OFFSET = .00781250 +I = complex(0,1) +PI = 3.14159265358979323846264338327 +ROT = cNorm(0.99939 + 0.0003490*I) +IROT = 1/ROT +F = 5 * 10**7 +F_ = 10**9 + + +class Segment: + def __init__(self,bA,cA,cB,trColor): + self.isFinite = bA + self.pointA = cA + self.pointB = cB + self.color = trColor + +class DrawnSegment: + def __init__(self,height,color): + self.height = height + self.color = color + +level = [ +# Segment(False,cNorm(-1),cNorm(I),(255,255,20)), + Segment(False,cNorm(-1),cNorm(I),BLACK) + ] + +def draw(level,gPlayer,fov,res): + drawn = list() + irot = exp(fov/res*I) + for i in range(res): + m = DrawnSegment(0,BLACK) + rot = irot * gPlayer.cRot**(i-(res/2)) + for j in level: + tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos)) + tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos)) + cInt = cLineIntersection(tA,tB,complex(0),rot) + rDist = cDist(0,cInt) + if cDot(cInt,rot) > 0: + continue + if (1 - m.height) > rDist: + m = DrawnSegment(1-rDist,j.color) + drawn.append(m) + return drawn + +def cap(rN): + if rN < 0: + return 0 + return rN + +def mainLoop(): + gPlayer = GyroVector(0,1) + display = pygame.display.set_mode((1280,720)) + font = pygame.freetype.Font(None,24) + while True: + framestart = time_ns() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return True + keys = pygame.key.get_pressed() + if keys[pygame.K_d]: + gPlayer.rotate(IROT) + if keys[pygame.K_a]: + gPlayer.rotate(ROT) + if keys[pygame.K_w]: + gPlayer += GyroVector(OFFSET, 1) + if keys[pygame.K_s]: + gPlayer -= GyroVector(OFFSET, 1) + display.fill(WHITE) + #pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0) + drawn = draw(level,gPlayer,PI/2,40) + n = 0 + for i in drawn: + pygame.draw.rect(display,i.color, (n,500 - (cap(i.height) * 500),10,cap(i.height) * 1000)) + n += 10 + font.render_to(display, (20, 150), str(gPlayer.cPos), (0, 0, 0)) + font.render_to(display, (20, 250), str(gPlayer.cRot), (0, 0, 0)) + pygame.display.update() + + frameend = time_ns() + sleep((frameend-framestart) / F_) + return True + +def main(): + pygame.init() + pygame.display.set_caption('P3HE') + if not pygame.get_init(): + return False + retstatus = mainLoop() + return retstatus + +if __name__ == "__main__": + if not main(): + print("An error occured")