x = Cos(u) * ( 1 + (v/2 * Cos(u/2)) )
y = Sin(u) * ( 1 + (v/2 * Cos(u/2)) )
z = v/2 * Sin(u/2)
0 <= u <= 2*Pi
-0.5 <= v <= 0.5
the code are too short and basic a few lines only and all the rest of the code are just for controlling the graphics, i prefer the textured one. the width of the strip are from -0.5 to 0.5 through 2 vertices .and we connect it to the second 2 vertices to form 2 triangles. and this goes through the whole circle ie 2*Pi .
i have tried the gnuplot version, and it takes too much time and too much hard disk flashing on/off while here in PureBasic it is instantaneous, suitable for science demonstrations. i'm thinking on how to attach a small ant model to the surface so it moves around the strip to show the strange case of this strip
PS: if you cut the paper mobius strip in half and through its longitude you will get a bigger circle, if you cut it again you will have 2 interwoven circles. this is always surprising kids try it yourself.
Press W for the wireFrame version
Code: Select all
Enumeration
#MESH
#LIGHT
#CAMERA_ONE
#BUTTON
#mainwin
#plane
EndEnumeration
Quit.b = #False
rot.l=1 :stopFlag = 1 : wireFrame.b = 0
xs.f = 0.3:ys.f = 0.3:zs.f = 0.3
x.f: y.f :z.f: x0.f: y0.f=1 :z0.f
rotx.f:roty.f=0.5:rotz.f :rotx0.f: roty0.f: rotz0.f
up.f = 1.8: depth.f=0
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "PgUp PgD scale mesh..Arrows for rotation, space: stop/rotate, QA far/near, key_pad R/L/U/D", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#BUTTON, 0, DesktopHeight(0)-60, 60, 30, "rotate/stop")
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-70, 0, 0, 0)
;WorldShadows(#PB_Shadow_Additive)
InitKeyboard()
SetFrameRate(60)
Add3DArchive("/", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))
CreateCamera(#CAMERA_ONE, 0, 0, 400, 400)
MoveCamera(#CAMERA_ONE, 0, 4, 9)
CameraLookAt(#CAMERA_ONE, 0, 0, 0)
RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf
CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(1))
SetActiveGadget(#BUTTON)
SkyDome("clouds.jpg", 100) ;for blue color background
; material for the plot
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))
;MaterialShadingMode(0, #PB_Material_Wireframe)
MaterialCullingMode(0, #PB_Material_NoCulling)
DisableMaterialLighting(0, #True)
CreateMesh(1, #PB_Mesh_TriangleList , #PB_Mesh_Static )
SetMeshMaterial(1, MaterialID(0))
x.f: y.f :z.f : u.f: v.f: r.f
i.l: j.l
v.f = -0.5 : u.f=0 :txu.f : txv.f
While u <= 2*#PI
While v<= 0.5
t+1 ; total number of vertices
x = Cos(u) * ( 1 + (v/2 * Cos(u/2)) )
y = Sin(u) * ( 1 + (v/2 * Cos(u/2)) )
z = v/2 * Sin(u/2)
MeshVertexPosition(x, y, z);
MeshVertexTextureCoordinate(txu, txv)
MeshVertexNormal(x, y, z)
txv = txv + 1 ; texture coordinates
v + 1
Wend
txv = 0
txu = txu + 1/(2*#PI/0.1) ;texture coordinates
v = -0.5
u + 0.1
Wend
;Debug t ; here 126 vertices from 0 to 125
For j=0 To t-3 Step 2
MeshFace(j,j+2,j+1)
MeshFace(j+1,j+2,j+3 )
Next
;connecting the first 2 vertices index with the last 2 vertices (twisted!!!)
MeshFace(0,125,1)
MeshFace(1,125,124)
NormalizeMesh(1)
FinishMesh(#True)
CreateEntity(1, MeshID(1), MaterialID(0))
ScaleEntity(1,3,3,3)
MoveEntity(1,0,up,depth,#PB_Absolute)
;Main loop
x=90: y=0: z=0 : h.f
Repeat
Event = WindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case #BUTTON
If rot = 0
rot = 1
rotx= rotx0:roty=roty0:rotz=rotz0 ; restore rotation status
stopFlag = 1
Else
rot = 0
rotx0= rotx:roty0=roty:rotz0=rotz ;back up rotation status
rotx=0:roty=0:rotz=0
stopFlag = 0
EndIf
EndSelect
EndIf
If stopFlag=1
x + rotx
y + roty
z + rotz
EndIf
RotateEntity(1, x, y, z)
RenderWorld()
FlipBuffers()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up) ; rotate left
rotx=1:roty=0:rotz=0
rotx0 = rotx: roty0 = roty :rotz0 = rotz
x + rotx
y + roty
z + rotz
stopFlag=0
rot = 0
ElseIf KeyboardPushed(#PB_Key_Down) ; rotate right
rotx=-1:roty=0:rotz=0
rotx0 = rotx: roty0 = roty :rotz0 = rotz
x + rotx
y + roty
z + rotz
stopFlag=0
rot = 0
ElseIf KeyboardPushed(#PB_Key_Right) ; rotate up
rotx=0:roty=1:rotz=0
rotx0 = rotx: roty0 = roty :rotz0 = rotz
x + rotx
y + roty
z + rotz
stopFlag=0
rot = 0
ElseIf KeyboardPushed(#PB_Key_Left) ; rotate down
rotx=0:roty=-1:rotz=0
rotx0 = rotx: roty0 = roty :rotz0 = rotz
x + rotx
y + roty
z + rotz
stopFlag=0
rot = 0
EndIf
If KeyboardPushed(#PB_Key_PageUp) ; scale up model
xs.f = 1.1:ys.f = 1.1:zs.f = 1.1
ScaleEntity(1,xs,ys,zs)
ElseIf KeyboardPushed(#PB_Key_PageDown) ; scale down model
xs = 0.9:ys = 0.9:zs= 0.9
ScaleEntity(1,xs,ys,zs)
EndIf
If KeyboardPushed(#PB_Key_Pad8) ; up move
up + 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_Pad2) ; down move
up - 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_Pad6)
h + 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_Pad4)
h - 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_Q) ; forward move
depth - 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_A) ; inward move
depth + 0.1
MoveEntity(1,h,up,depth,#PB_Absolute)
ElseIf KeyboardReleased(#PB_Key_W) ; display wire frame for the material
If wireFrame=0
MaterialShadingMode(0, #PB_Material_Wireframe)
wireFrame ! 1
ElseIf wireFrame=1
MaterialShadingMode(0, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
If KeyboardPushed(#PB_Key_Escape)
Quit = #True
EndIf
Until Quit = #True Or Event = #PB_Event_CloseWindow

