From 2bae3740c4c8f0e0c57a675a48d2814932c1932b Mon Sep 17 00:00:00 2001 From: mibi88 Date: Tue, 6 Dec 2022 22:42:36 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- line.py | 59 +++++++++++++++++++++++++++++++++ main.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ main2.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ mainPE.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ mainPE2.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 423 insertions(+) create mode 100644 line.py create mode 100644 main.py create mode 100644 main2.py create mode 100644 mainPE.py create mode 100644 mainPE2.py diff --git a/line.py b/line.py new file mode 100644 index 0000000..469fe68 --- /dev/null +++ b/line.py @@ -0,0 +1,59 @@ +from casioplot import * + +def line(pos1,pos2): + x1=int(pos1[0]) + y1=int(pos1[1]) + x2=int(pos2[0]) + y2=int(pos2[1]) + """if x2>x1: + tmp=x1 + x1=x2 + x2=tmp + del tmp + if y2>y1: + tmp=y1 + y1=y2 + y2=tmp + del tmp + dx=x1-x2 + dy=y1-y2 + if dx=dy: + if x1==x2: + break + error=error+dy + x1+=sx + if e2<=dx: + if y1==y2: + break + error=error+dx + y1+=sy \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..91f1852 --- /dev/null +++ b/main.py @@ -0,0 +1,89 @@ +# 3D cube +# by mibi88 + +from line import * +import math + +#width(3) + +# unsed some code from https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/ + +vertices = [ + [1,0,1], + [2,0,-1], + [0,0,-1], + [1,1,-1], +] + +edges = [ + [0, 1], + [0, 2], + [0, 3], + [1, 2], + [2, 3], + [1, 3], +] + +a=0 + + + +def rotate_x(sx, sy, sz, deg): + rad = deg * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + y = sy * cosa - sz * sina + z = sy * sina + sz * cosa + return [sx, y, z] + +def rotate_y(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + z = sz * cosa - sx * sina + x = sz * sina + sx * cosa + return [x, sy, z] + +def rotate_z(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + x = sx * cosa - sy * sina + y = sx * sina + sy * cosa + return [x, y, sz] + +def project(sx, sy, sz, px, py, fov = 60, viewer_distance = 8): + factor = fov / (viewer_distance + sz) + x = sx * factor + px + y = -sy * factor + py + return [x, y] + +def render(vertices, edges, rx, ry, rz, px, py, fov = 60, viewer_distance = 6): + rx = rx%360 + ry = ry%360 + rz = rz%360 + points = [] + for i in vertices: + i = rotate_x(i[0], i[1], i[2], rx) + i = rotate_y(i[0], i[1], i[2], ry) + i = rotate_z(i[0], i[1], i[2], rz) + pos = project(i[0], i[1], i[2], px, py, fov, viewer_distance) + points.append(pos) + #clear() + for i in edges: + line(points[i[0]], points[i[1]]) + +def draw(): + clear_screen() + global a + global vertices, edges + #render(vertices, edges, 345, a, 0, 32, 16) + render(vertices, edges, a, a, 0, 64, 32) + a+=1 + show_screen() + +while 1: + draw() + """for i in range(1000): + pass""" +"""draw()""" \ No newline at end of file diff --git a/main2.py b/main2.py new file mode 100644 index 0000000..8b36f43 --- /dev/null +++ b/main2.py @@ -0,0 +1,90 @@ +# 3D cube +# by mibi88 + +from line import * +import math + +#width(3) + +# unsed some code from https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/ + +vertices = [ + [1,0,1], + [2,0,-1], + [0,0,-1], + [1,1,-1], +] + +edges = [ + [0, 1, 2], + [0, 1, 3], + [1, 2, 3], + [0, 2, 3] +] + +a=0 + + + +def rotate_x(sx, sy, sz, deg): + rad = deg * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + y = sy * cosa - sz * sina + z = sy * sina + sz * cosa + return [sx, y, z] + +def rotate_y(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + z = sz * cosa - sx * sina + x = sz * sina + sx * cosa + return [x, sy, z] + +def rotate_z(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + x = sx * cosa - sy * sina + y = sx * sina + sy * cosa + return [x, y, sz] + +def project(sx, sy, sz, px, py, pz, fov = 60, viewer_distance = 8, w=128, h=64): + factor = fov / (viewer_distance + (sz+pz)) + x = (sx+px) * factor + (w/2) + y = -(sy+py) * factor + (h/2) + return [x, y] + +def render(vertices, triangles, rx, ry, rz, px, py, pz, fov = 60, viewer_distance = 6, w=128, h=64): + rx = rx%360 + ry = ry%360 + rz = rz%360 + points = [] + for i in vertices: + i = rotate_x(i[0], i[1], i[2], rx) + i = rotate_y(i[0], i[1], i[2], ry) + i = rotate_z(i[0], i[1], i[2], rz) + pos = project(i[0], i[1], i[2], px, py, pz, fov, viewer_distance, w, h) + print(pos) + points.append(pos) + #clear() + for i in triangles: + line(points[i[0]], points[i[1]]) + line(points[i[1]], points[i[2]]) + line(points[i[0]], points[i[2]]) + +def draw(): + clear_screen() + global a + global vertices, edges + #render(vertices, edges, 345, a, 0, 32, 16) + render(vertices, edges, a, a, 0, 0, 0, 0) + a+=1 + show_screen() + +while 1: + draw() + """for i in range(1000): + pass""" +"""draw()""" \ No newline at end of file diff --git a/mainPE.py b/mainPE.py new file mode 100644 index 0000000..e71d961 --- /dev/null +++ b/mainPE.py @@ -0,0 +1,89 @@ +# 3D cube +# by mibi88 + +from PLANE.line import * +import math + +#width(3) + +# unsed some code from https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/ + +vertices = [ + [1,0,1], + [2,0,-1], + [0,0,-1], + [1,1,-1], +] + +edges = [ + [0, 1], + [0, 2], + [0, 3], + [1, 2], + [2, 3], + [1, 3], +] + +a=0 + + + +def rotate_x(sx, sy, sz, deg): + rad = deg * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + y = sy * cosa - sz * sina + z = sy * sina + sz * cosa + return [sx, y, z] + +def rotate_y(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + z = sz * cosa - sx * sina + x = sz * sina + sx * cosa + return [x, sy, z] + +def rotate_z(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + x = sx * cosa - sy * sina + y = sx * sina + sy * cosa + return [x, y, sz] + +def project(sx, sy, sz, px, py, fov = 60, viewer_distance = 8): + factor = fov / (viewer_distance + sz) + x = sx * factor + px + y = -sy * factor + py + return [x, y] + +def render(vertices, edges, rx, ry, rz, px, py, fov = 60, viewer_distance = 6): + rx = rx%360 + ry = ry%360 + rz = rz%360 + points = [] + for i in vertices: + i = rotate_x(i[0], i[1], i[2], rx) + i = rotate_y(i[0], i[1], i[2], ry) + i = rotate_z(i[0], i[1], i[2], rz) + pos = project(i[0], i[1], i[2], px, py, fov, viewer_distance) + points.append(pos) + #clear() + for i in edges: + line(points[i[0]], points[i[1]]) + +def draw(): + clear_screen() + global a + global vertices, edges + #render(vertices, edges, 345, a, 0, 32, 16) + render(vertices, edges, a, a, 0, 64, 32) + a+=1 + show_screen() + +while 1: + draw() + """for i in range(1000): + pass""" +"""draw()""" \ No newline at end of file diff --git a/mainPE2.py b/mainPE2.py new file mode 100644 index 0000000..94338bf --- /dev/null +++ b/mainPE2.py @@ -0,0 +1,96 @@ +# 3D cube +# by mibi88 + +from PLANE.line import * +import math, time + +#width(3) + +# unsed some code from https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/ + +vertices = [ + [1,0,1], + [2,0,-1], + [0,0,-1], + [1,1,-1], +] + +edges = [ + [0, 1, 2], + [0, 1, 3], + [1, 2, 3], + [0, 2, 3] +] + +a=0 + + + +def rotate_x(sx, sy, sz, deg): + rad = deg * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + y = sy * cosa - sz * sina + z = sy * sina + sz * cosa + return [sx, y, z] + +def rotate_y(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + z = sz * cosa - sx * sina + x = sz * sina + sx * cosa + return [x, sy, z] + +def rotate_z(sx, sy, sz, angle): + rad = angle * math.pi / 180 + cosa = math.cos(rad) + sina = math.sin(rad) + x = sx * cosa - sy * sina + y = sx * sina + sy * cosa + return [x, y, sz] + +def project(sx, sy, sz, px, py, pz, fov = 60, viewer_distance = 8, w=128, h=64): + factor = fov / (viewer_distance + (sz+pz)) + x = (sx+px) * factor + (w/2) + y = -(sy+py) * factor + (h/2) + return [x, y] + +def render(vertices, triangles, rx, ry, rz, px, py, pz, fov = 60, viewer_distance = 6, w=128, h=64): + rx = rx%360 + ry = ry%360 + rz = rz%360 + points = [] + for i in vertices: + i = rotate_x(i[0], i[1], i[2], rx) + i = rotate_y(i[0], i[1], i[2], ry) + i = rotate_z(i[0], i[1], i[2], rz) + pos = project(i[0], i[1], i[2], px, py, pz, fov, viewer_distance, w, h) + #print(pos) + points.append(pos) + #clear() + for i in triangles: + line(points[i[0]], points[i[1]]) + line(points[i[1]], points[i[2]]) + line(points[i[0]], points[i[2]]) + +def draw(): + global a + global vertices, edges + #render(vertices, edges, 345, a, 0, 32, 16) + start=time.time() + clear_screen() + render(vertices, edges, a, a, 0, 0, 0, 0) + show_screen() + tps=(1/(time.time()-start)*4) + print(tps) + a+=1 + +while 1: + try: + draw() + except: + break + """for i in range(1000): + pass""" +"""draw()""" \ No newline at end of file