Fitting 2D graphics nicely in the graphics window
Posted: Sun Apr 29, 2018 7:44 am
Fitting 2D graphics nicely in the graphics window with Orthographic
suppose your graphics window is 500*500
suppose you want to draw a function from -1 to 1 in x and y directions, ie we have a square of size 2*2. we need to project this square to the computer graphics window.
instead of multiplying x and y with a factor to make it bigger so we can see it on the screen, use this function:
and then we draw points without distracting ourselves to the process of projecting our real plotting within small or big intervals to the computer screen.
here is a graphics to show a circle and a "Cayley's_sextic" curve
use this screen ruler http://www.spadixbd.com/freetools/?referrer=JRulerUser
to measure the graphics in pixels or in centimeters, right click on the ruler to choose actions and options
needs PB 5.62
suppose your graphics window is 500*500
Code: Select all
#screenwidth = 500
#screenheight = 500
OpenWindowedScreen(WindowID(0), 0, 0, #screenwidth, #screenheight)
instead of multiplying x and y with a factor to make it bigger so we can see it on the screen, use this function:
Code: Select all
Width = 2: Height = 2
CameraProjectionMode(0, #PB_Camera_Orthographic, Width, Height)
here is a graphics to show a circle and a "Cayley's_sextic" curve
use this screen ruler http://www.spadixbd.com/freetools/?referrer=JRulerUser
to measure the graphics in pixels or in centimeters, right click on the ruler to choose actions and options
needs PB 5.62
Code: Select all
#screenwidth = 500
#screenheight = 500
Declare Curve()
Define.f KeyX, KeyY, MouseX, MouseY
InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0, 0, 0, 800, 600, "#PB_Camera_Orthographic ... Cayley's_sextic Curve + .... Circle", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 130, 40, #screenwidth, #screenheight)
;- Material
CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 10, #PB_Absolute)
;CameraFOV(0, 40)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(250, 250, 240))
;glEnable_(#GL_POINT_SMOOTH) ; effective only when compiling with opengl subsystem
CreateLine3D(1, -1, 0, 0, RGB(255,0,0), 1, 0, 0, RGB(255,0,0))
CreateLine3D(2, 0, -1, 0, RGB(255,0,0), 0, 1, 0, RGB(255,0,0))
Width.f = 2.5 * (#screenwidth / #screenheight); width of the plotting x range from -xMin to xMax : -1.25 to 1.25
Height.f = 2.5; from -1.25 to 1.125
CameraProjectionMode(0, #PB_Camera_Orthographic, Width, Height)
CreateText3D(5, "-1") : ScaleText3D(5, 0.2,0.2,0.2,#PB_Absolute)
Text3DColor(5, RGBA(255, 0, 0, 255))
MoveText3D(5, -1, 0, 0, #PB_Absolute)
Curve()
SetMeshMaterial(0, MaterialID(0))
Global Stars = CreateNode(#PB_Any)
AttachNodeObject(Stars, MeshID(0)) ; attach the graphics mesh to a node so when we move or rotate the node the graphics will follow it
Repeat
Repeat
event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
quit = 1
EndSelect
Until event = 0
ExamineKeyboard()
;RotateNode(Stars, 0, 0, 1, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
End
Procedure Curve()
;CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
t.f = -1
While t <= 2*#PI
;Cayley's_sextic curve parametric equations
x.f = Pow(Cos(t),3)* Cos(3*t)
y.f = Pow(Cos(t),3)* Sin(3*t)
MeshVertexPosition(x, y, 0)
MeshVertexColor(RGB(255,0,0))
; Circle parametric equations
x.f=Cos(t)*1
y.f=Sin(t)*1
MeshVertexPosition(x, y, 0)
MeshVertexColor(RGB(0,150,255))
t+0.001
Wend
FinishMesh(#False)
EndProcedure