4 from pygame
.constants
import *
13 pygame
.display
.set_mode( (WIDTH
,HEIGHT
))
14 surface
= pygame
.display
.get_surface()
15 clock
= pygame
.time
.Clock()
17 def convert_3d_2d( threed
, h_factor
):
18 #x = threed.x * (0.1 * threed.y + 0.9)
19 #y = (threed.y - threed.z*h_factor) * (0.15 * threed.y + 0.85)
22 x
= threed
.x
* (a
* threed
.y
+ b
)
23 y
= (threed
.y
- threed
.z
*h_factor
) * (a
* threed
.y
+ b
)
28 class Coord3D(object):
29 def __init__( self
, x
, y
, z
= 0.0):
35 return Coord3D( self
.x
, self
.y
, 0.0)
38 return convert_3d_2d( self
, H_FACT
)
40 class Coord2D(object):
41 def __init__( self
, x
, y
):
47 ( Coord3D( -1, -1), Coord3D( -1, 1 ) ),
48 ( Coord3D( -1, -1), Coord3D( 1, -1) ),
49 ( Coord3D( -1, 1), Coord3D( 1, 1) ),
50 ( Coord3D( 1, -1), Coord3D( 1, 1) ),
51 ( Coord3D( -1, 0, 1), Coord3D( -1, 0, 0) ),
52 ( Coord3D( -1, 0, 1), Coord3D( 1, 0, 1) ),
53 ( Coord3D( 1, 0, 1), Coord3D( 1, 0, 0) ),
54 ( Coord3D( -1, 0, 0), Coord3D( 1, 0, 0) ),
57 def twod_to_real( twod
):
58 x
= twod
.x
* WIDTH
/2.1 + WIDTH
/2
59 y
= twod
.y
* HEIGHT
/2.1 + HEIGHT
/2
73 evt
= pygame
.event
.poll()
76 if evt
.type == KEYUP
and evt
.key
== K_ESCAPE
:
79 if evt
.type == MOUSEMOTION
:
80 x
= (evt
.pos
[0]*2.0/WIDTH
)-1.0
81 y
= (evt
.pos
[1]*2.0/HEIGHT
)-1.0
85 if x
> 1.0 or x
< -1.0:
87 if y
> 1.0 or y
< -1.0:
90 surface
.fill( (0,0,0))
92 twod_a
= twod_to_real( a
[0].twod())
93 twod_b
= twod_to_real( a
[1].twod())
94 pygame
.draw
.aaline( surface
, (255,255,255), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
96 currd
= Coord3D( x
, y
, 1.2+1.2*cos( y
*2*pi
))
108 twod_a
= twod_to_real( convert_3d_2d( old
, H_FACT
))
109 twod_b
= twod_to_real( convert_3d_2d( e
, H_FACT
))
110 pygame
.draw
.aaline( surface
, (0,u
,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
111 twod_a
= twod_to_real( convert_3d_2d( old
.bottom(), H_FACT
))
112 twod_b
= twod_to_real( convert_3d_2d( e
.bottom(), H_FACT
))
113 pygame
.draw
.aaline( surface
, (0,0,u
), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
115 u
+= int(255/len(hist
))
117 twod_a
= twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT
))
118 twod_b
= twod_to_real( convert_3d_2d( currd
, H_FACT
))
119 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
120 twod_a
= twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT
))
121 twod_b
= twod_to_real( convert_3d_2d( Coord3D( x
, y
, 0), H_FACT
))
122 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
123 twod_a
= twod_to_real( convert_3d_2d( currd
, H_FACT
))
124 twod_b
= twod_to_real( convert_3d_2d( Coord3D( x
, y
, 0), H_FACT
))
125 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
127 pygame
.display
.flip()