Voronoi colorfull tiles
Posted: Mon Dec 07, 2015 9:02 am
adapted from the first algorithm in this page http://forum.basicprogramming.org/index ... 127.0.html
to produce a voronoi color scheme
run it with debugger off to see instant result, but if you run it with debugger on you need to wait 15 seconds (seems from the far inner loop in line 22)

with ImageGadget:
with opengl glDrawArrays:
feel free to add examples or to correct the above code
to produce a voronoi color scheme
run it with debugger off to see instant result, but if you run it with debugger on you need to wait 15 seconds (seems from the far inner loop in line 22)

with ImageGadget:
Code: Select all
DisableDebugger
Procedure.f RandF() ; simulate RND function from 0 to 1
ProcedureReturn Random(10000) * 0.0001
EndProcedure
If OpenWindow(0, 0, 0, 640, 640, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If CreateImage(0, 640, 640) And StartDrawing(ImageOutput(0))
sq=639:s2.f=sq/2:points=240
Dim x.f(points):Dim y.f(points):Dim kl.f(points)
For i = 0 To points
x(i)=RandF()*sq: y(i)=RandF()*sq
;g=127-127*(s2-x(i)/s2)+127-127*(s2-y(i)/s2)
g.f=127-127*(Abs(s2-x(i))/s2)+127-127*(Abs(s2-y(i))/s2)
kl(i)=RGB(255-x(i)/sq*255,g,y(i)/sq*255)
Next
a.f:b.f:q.f
;StartTime = ElapsedMilliseconds()
For xx = 0 To sq
For yy = 0 To sq
d = 307201
For i = 0 To points
a=x(i)-xx: b=y(i)-yy
q=a*a+b*b
If q < d
d = q: kkl = i
EndIf
Next
;pset xx,yy,kl(kkl)
Plot(xx, yy, kl(kkl))
Next
Next
;ElapsedTime = ElapsedMilliseconds()-StartTime
;MessageRequester(" ", Str(ElapsedTime/1000) +" seconds")
StopDrawing()
ImageGadget(0, 0, 0, 640, 640, ImageID(0))
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
with opengl glDrawArrays:
Code: Select all
DisableDebugger
Procedure.f RandF() ; simulate RND function from 0 to 1
ProcedureReturn Random(10000) * 0.0001
EndProcedure
Structure Point3D
x.f
y.f
z.f
EndStructure
Structure color3
r.f
g.f
b.f
EndStructure
Define event, quit
Declare FillArrays()
Declare Display()
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "OpenGL..Voronoi, using glDrawArrayss... ")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0) , WindowHeight(0), #PB_OpenGL_Keyboard )
SetActiveGadget(0)
Global Nb.l = 1000000
; -- Define Data For it
Global Dim VertexA.Point3D(Nb)
Global Dim ColorA.Color3(Nb)
FillArrays()
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective_(60.0, WindowWidth(0)/WindowHeight(0), 1.0, 100.0)
glMatrixMode_(#GL_MODELVIEW)
glScalef_(0.5,0.5, 1)
glTranslatef_(-60, -60, -50)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
gluLookAt_( 0, 1, 20,
0, 1, -20,
0, 1, 0 )
Repeat
Display()
event = WindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_KeyDown
key = GetGadgetAttribute(0,#PB_OpenGL_Key )
Select key
Case #PB_Shortcut_Escape
quit = 1
EndSelect
EndIf
EndIf
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(2)
Until quit = 1 Or Event = #PB_Event_CloseWindow
Procedure FillArrays()
sq=640:s2.f=sq/2:points=240 :g.f
;sq=240:s2.f=sq/2:points=40 :g.f
Dim x.f(points)
Dim y.f(points)
Dim kl.color3(points)
For i = 0 To points
x(i)=RandF()*sq: y(i)=RandF()*sq
;g=127-127*(s2-x(i)/s2)+127-127*(s2-y(i)/s2)
g=127-127*(Abs(s2-x(i))/s2)+127-127*(Abs(s2-y(i))/s2)
kl(i)\r = 255-x(i)/sq*255
kl(i)\g = g
kl(i)\b = y(i)/sq*255
Next
a.f: b.f: q.f: radius.f: hei.f: zz.f
xx.l: yy.l: d.l : kkl.l
For xx = 0 To sq
For yy = 0 To sq
d = 307201
For i = 0 To points
a=x(i)-xx: b=y(i)-yy
q=a*a+b*b
If q < d
d = q
kkl = i
EndIf
Next
;pset xx,yy,kl(kkl)
;indexX * arrayWidth + indexY
ind = yy*sq + xx ;ind = (yy-1)*sq + xx
VertexA(ind)\x = xx*0.2
VertexA(ind)\y = yy*0.2
VertexA(ind)\z = 0
ColorA(ind)\r = kl(kkl)\r /255
ColorA(ind)\g = kl(kkl)\g /255
ColorA(ind)\b = kl(kkl)\b /255
;pset xx,yy,kl(kkl)
Next
Next
EndProcedure
Procedure display()
glClear_(#GL_COLOR_BUFFER_BIT)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glClearColor_(1, 1, 1, 1)
;glScalef_(0.6,0.6, 1)
glEnableClientState_(#GL_VERTEX_ARRAY )
glEnableClientState_(#GL_COLOR_ARRAY)
glVertexPointer_(3, #GL_FLOAT,SizeOf(Point3D),@VertexA(0)\x)
glColorPointer_(3, #GL_FLOAT, SizeOf(Color3), @ColorA(0)\r)
glDrawArrays_(#GL_POINTS, 0, Nb )
glDisableClientState_(#GL_COLOR_ARRAY)
glDisableClientState_(#GL_VERTEX_ARRAY)
;**************************************************
EndProcedure