Author Topic: Rotating Pyramid  (Read 5183 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Rotating Pyramid
« on: March 01, 2022, 09:20:09 am »
Another quickie for this morning and Aurels Forum: http://basic4all.info8-hosting.info/index.php

Showing Aurel how he might do this more satisfactory with his MicroA interpreter with this QB64 example:
Code: QB64: [Select]
  1. _Title "Rotating Pyramid" 'b+ 2022-03-01
  2. cx = _Width / 2
  3. ax = cx: ay = 50 ' apex
  4. ex = cx: ey = 330 ' ellipse
  5. xr = cx * .7: yr = .33 * xr
  6.     Cls
  7.     x1 = ex + xr * Cos(a): y1 = ey + yr * Sin(a)
  8.     x2 = ex + xr * Cos(a + _Pi * .5): y2 = ey + yr * Sin(a + _Pi * .5)
  9.     x3 = ex + xr * Cos(a + _Pi): y3 = ey + yr * Sin(a + _Pi)
  10.     x4 = ex + xr * Cos(a + _Pi * 1.5): y4 = ey + yr * Sin(a + _Pi * 1.5)
  11.     l x1, y1, x2, y2
  12.     l x2, y2, x3, y3
  13.     l x3, y3, x4, y4
  14.     l x4, y4, x1, y1
  15.     l x1, y1, ax, ay
  16.     l x2, y2, ax, ay
  17.     l x3, y3, ax, ay
  18.     l x4, y4, ax, ay
  19.     a = a + .01
  20.     _Display
  21.     _Limit 30
  22.  
  23. Sub l (a, b, c, d) ' this is just way easier to code lines Aurel you dont need this part
  24.     Line (a, b)-(c, d)
  25.  

 
QB64 Rotate Pyramid.PNG

Offline SquirrelMonkey

  • Newbie
  • Posts: 29
  • Youtuber and GIPHY artist
    • View Profile
    • Joluijten.com
Re: Rotating Pyramid
« Reply #1 on: March 05, 2022, 05:18:52 pm »
Another quickie for this morning and Aurels Forum: http://basic4all.info8-hosting.info/index.php

Showing Aurel how he might do this more satisfactory with his MicroA interpreter with this QB64 example:
Code: QB64: [Select]
  1. _Title "Rotating Pyramid" 'b+ 2022-03-01
  2. cx = _Width / 2
  3. ax = cx: ay = 50 ' apex
  4. ex = cx: ey = 330 ' ellipse
  5. xr = cx * .7: yr = .33 * xr
  6.     Cls
  7.     x1 = ex + xr * Cos(a): y1 = ey + yr * Sin(a)
  8.     x2 = ex + xr * Cos(a + _Pi * .5): y2 = ey + yr * Sin(a + _Pi * .5)
  9.     x3 = ex + xr * Cos(a + _Pi): y3 = ey + yr * Sin(a + _Pi)
  10.     x4 = ex + xr * Cos(a + _Pi * 1.5): y4 = ey + yr * Sin(a + _Pi * 1.5)
  11.     l x1, y1, x2, y2
  12.     l x2, y2, x3, y3
  13.     l x3, y3, x4, y4
  14.     l x4, y4, x1, y1
  15.     l x1, y1, ax, ay
  16.     l x2, y2, ax, ay
  17.     l x3, y3, ax, ay
  18.     l x4, y4, ax, ay
  19.     a = a + .01
  20.     _Display
  21.     _Limit 30
  22.  
  23. Sub l (a, b, c, d) ' this is just way easier to code lines Aurel you dont need this part
  24.     Line (a, b)-(c, d)
  25.  

 
QB64 Rotate Pyramid.PNG


Love this, reminds me of the demo scene.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Rotating Pyramid
« Reply #2 on: March 06, 2022, 03:29:53 pm »
Ah yeah, _vince has posted the code for this. Beats the heck out of mine but mine was intended to run on a very simple interpreter.

I shall attempt to summon the god of Rotating Pyramids @_vince  please sir, show us how to do it! :)

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Rotating Pyramid
« Reply #3 on: March 06, 2022, 03:50:39 pm »
B+ mode time!

Code: QB64: [Select]
  1. const d = 300
  2. const z0 = 550
  3. const oy = -210
  4.  
  5. pi = 4*atn(1)
  6.  
  7. dim x(14), y(14), z(14)
  8.  x( 0) =  0:  y( 0) =   0:  z( 0) =  0
  9.  x( 1) = 50:  y( 1) =-140:  z( 1) = 50
  10.  x( 2) =-50:  y( 2) =-140:  z( 2) = 50
  11.  x( 3) =-50:  y( 3) =-140:  z( 3) =-50
  12.  x( 4) = 50:  y( 4) =-140:  z( 4) =-50
  13.  x( 5) = 50:  y( 5) =-140:  z( 5) = 50
  14.  
  15. zoom = 5
  16.  
  17. sw = 640
  18. sh = 480
  19.  
  20.  
  21. a = 0
  22.         cls
  23.  
  24.         a=a+0.01
  25.  
  26.         xx = x(0)
  27.         yy = y(0)
  28.         zz = z(0)
  29.         rot xx, zz, a
  30.         proj p0, q0, xx, yy, zz
  31.  
  32.         'draw all triangles
  33.         for i=1 to 4
  34.                 x1 = x(i)
  35.                 y1 = y(i)
  36.                 z1 = z(i)
  37.                 rot x1, z1, a
  38.  
  39.                 x2 = x(i + 1)
  40.                 y2 = y(i + 1)
  41.                 z2 = z(i + 1)
  42.                 rot x2, z2, a
  43.  
  44.                 c = _rgb(70,70,70)
  45.  
  46.                 proj p, q, x1, y1, z1
  47.                 pset (sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  48.                 line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  49.  
  50.                 proj p, q, x2, y2, z2
  51.                 line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  52.                 line -(sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  53.         next
  54.  
  55.         'draw the visible triangles
  56.         for i=1 to 4
  57.                 x1 = x(i)
  58.                 y1 = y(i)
  59.                 z1 = z(i)
  60.                 rot x1, z1, a
  61.  
  62.                 x2 = x(i + 1)
  63.                 y2 = y(i + 1)
  64.                 z2 = z(i + 1)
  65.                 rot x2, z2, a
  66.  
  67.                 'vector cross product
  68.                 'cx = y1*z2 - z1*y2
  69.                 'cy = x1*z2 - z1*x2
  70.                 cz = x1*y2 - y1*x2
  71.  
  72.                 if cz >= 0 then
  73.                         c = _rgb(255,255,255)
  74.  
  75.                         proj p, q, x1, y1, z1
  76.                         pset (sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  77.                         line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  78.  
  79.                         proj p, q, x2, y2, z2
  80.                         line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  81.                         line -(sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  82.                 end if 
  83.         next
  84.  
  85.         _display
  86.         _limit 30
  87.  
  88. 'rotate
  89. sub rot(x, y, a)
  90.         xx = x*cos(a) - y*sin(a)
  91.         yy = x*sin(a) + y*cos(a)
  92.         x = xx
  93.         y = yy
  94.  
  95. 'perspective projection
  96. sub proj(p, q, x, y, z)
  97.         dz = z0 + z
  98.         p = x*d/dz
  99.         q = y*d/dz
  100.  
  101.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Rotating Pyramid
« Reply #4 on: March 06, 2022, 06:15:55 pm »
Now all we have to do is rotate that apex around the sphere.

The god of Rotating Pyramids' work is never done.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Rotating Pyramid
« Reply #5 on: March 08, 2022, 07:16:48 am »
B+ mod #2

seems my calculations are inaccurate, maybe someone can fix why

Code: QB64: [Select]
  1. defdbl a-z
  2.  
  3. const d = 300
  4. const z0 = 550
  5. const oy = 00
  6.  
  7. pi = 4*atn(1)
  8.  
  9. dim x(5), y(5), z(5)
  10.  x( 0) =  0:  y( 0) = 70:  z( 0) =  0
  11.  x( 1) = 70:  y( 1) =-70:  z( 1) = 70
  12.  x( 2) =-70:  y( 2) =-70:  z( 2) = 70
  13.  x( 3) =-70:  y( 3) =-70:  z( 3) =-70
  14.  x( 4) = 70:  y( 4) =-70:  z( 4) =-70
  15.  x( 5) = 70:  y( 5) =-70:  z( 5) = 70
  16.  
  17. zoom = 4
  18.  
  19. sw = 640
  20. sh = 480
  21.  
  22. screen _newimage(sw,sh,32)
  23.  
  24. a = 0
  25.         cls
  26.  
  27.         a = a + 0.01
  28.  
  29.         xx = x(0)
  30.         yy = y(0)
  31.         zz = z(0)
  32.  
  33.         rot yy, zz, a
  34.         rot xx, zz, a
  35.  
  36.         proj p0, q0, xx, yy, zz
  37.  
  38.         'draw all triangles
  39.         for i=1 to 4
  40.                 x1 = x(i)
  41.                 y1 = y(i)
  42.                 z1 = z(i)
  43.        
  44.                 rot y1, z1, a
  45.                 rot x1, z1, a
  46.                
  47.        
  48.                 x2 = x(i + 1)
  49.                 y2 = y(i + 1)
  50.                 z2 = z(i + 1)
  51.        
  52.                 rot y2, z2, a
  53.                 rot x2, z2, a
  54.        
  55.                 c = _rgb(35,35,35)
  56.        
  57.                 proj p, q, x1, y1, z1
  58.                 pset (sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  59.                 line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  60.        
  61.                 proj p, q, x2, y2, z2
  62.                 line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  63.                 line -(sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  64.         next
  65.  
  66.         'draw the visible triangles
  67.         for i=1 to 4
  68.                 x1 = x(i)
  69.                 y1 = y(i)
  70.                 z1 = z(i)
  71.  
  72.                 rot y1, z1, a
  73.                 rot x1, z1, a
  74.  
  75.                 x2 = x(i + 1)
  76.                 y2 = y(i + 1)
  77.                 z2 = z(i + 1)
  78.  
  79.                 rot y2, z2, a
  80.                 rot x2, z2, a
  81.        
  82.                 'vector cross product
  83.                 cz = (x1 - xx)*(y2 - yy) - (y1 - yy)*(x2 - xx)
  84.  
  85.                 if cz > 0 then
  86.                         c = _rgb(255,255,255)
  87.        
  88.                         proj p, q, x1, y1, z1
  89.                         pset (sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  90.                         line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  91.        
  92.                         proj p, q, x2, y2, z2
  93.                         line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  94.                         line -(sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  95.                 end if 
  96.         next
  97.  
  98.         'draw the base
  99.         xx = x(1)
  100.         yy = y(1)
  101.         zz = z(1)
  102.         rot yy, zz, a
  103.         rot xx, zz, a
  104.  
  105.         x1 = x(2)
  106.         y1 = y(2)
  107.         z1 = z(2)
  108.         rot y1, z1, a
  109.         rot x1, z1, a
  110.  
  111.         x2 = x(3)
  112.         y2 = y(3)
  113.         z2 = z(3)
  114.         rot y2, z2, a
  115.         rot x2, z2, a
  116.  
  117.         cz = (x1 - xx)*(y2 - yy) - (y1 - yy)*(x2 - xx)
  118.  
  119.         if cz < 0 then
  120.                 c = _rgb(255,255,255)
  121.                 proj p0, q0, xx, yy, zz
  122.                 pset (sw/2 + zoom*p0, sh/2 - zoom*q0 + oy), c
  123.                 for i=2 to 5
  124.                         xx = x(i)
  125.                         yy = y(i)
  126.                         zz = z(i)
  127.  
  128.                         rot yy, zz, a
  129.                         rot xx, zz, a
  130.  
  131.                         proj p, q, xx, yy, zz
  132.                         line -(sw/2 + zoom*p, sh/2 - zoom*q + oy), c
  133.                 next
  134.         end if
  135.  
  136.         _display
  137.         _limit 30
  138.  
  139. 'rotate
  140. sub rot(x, y, a)
  141.         xx = x*cos(a) - y*sin(a)
  142.         yy = x*sin(a) + y*cos(a)
  143.         x = xx
  144.         y = yy
  145.  
  146. 'perspective projection
  147. sub proj(p, q, x, y, z)
  148.         dz = z0 + z
  149.         p = x*d/dz
  150.         q = y*d/dz
  151.  
  152.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Rotating Pyramid
« Reply #6 on: March 08, 2022, 09:46:24 am »
Hey, not bad, the only thing I see less that perfect is the apex remaining on the left side.