porting plot library to FB?

General FreeBASIC programming questions.
srvaldez
Posts: 3596
Joined: Sep 25, 2005 21:54

porting plot library to FB?

Post by srvaldez »

I found this nice but rather dated library http://koolplot.codecutter.org
the license is in the public domain, would anyone that's capable be willing to port this lib to FB?
I like the simplicity

Code: Select all

#include "koolplot.h"

int main()
{
   Plotdata x(-3.0, 3.0), y = sin(x) - 0.5*x;
   plot(x, y);
   return 0;
} 
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: porting plot library to FB?

Post by TJF »

srvaldez wrote:..., would anyone that's capable be willing to port this lib to FB?
Not me! It's very simple and very dated, and written in C++.

Years ago I made GooData, but never published it. It also provides bar/pie charts and is based on cairo/GooCanvas ==> PDF/SVG/PNG output.

I can provide the source if you like.

Regards
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: porting plot library to FB?

Post by jj2007 »

TJF wrote:I made GooData
Looks very nice, gut gemacht!
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: porting plot library to FB?

Post by TJF »

jj2007 wrote:..., gut gemacht!
Mille grazie e cordiali saluti :-)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: porting plot library to FB?

Post by jj2007 »

Simple graphics are one of my hobbies (-> example in Assembly), and your version definitely looks good ;-)
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: porting plot library to FB?

Post by TJF »

Wow! Graphics in ASM is a lot of work! Endless testing, using a magnifier for checking details on screen. And then file output and printing ...
jj2007 wrote:..., and your version definitely looks good ;-)
Thank you, but that's not my fault. The Cairo library offers fast and very good output routines, including GPU support and features like sub pixel shading. Pango library is used for text handling (UTF-8 character set with markup support), and GooCanvas library glues it all together. I just made some new widgets generating axes and curves.

This has never been a real project. The main target was to test the gtk-doc tool chain for auto-generating a manual from FreeBASIC source code. Check the Gallery for further examples. Later I switched to Doxygen, and the project died.
dodicat
Posts: 8236
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: porting plot library to FB?

Post by dodicat »

Cairo is good, but a bit slow on a freebasic graphics screen.
(Have tested this before)
Graphics.h seems to be missing from srvaldez's link.
I tried to download one, but some are C and some C++, some go back to Turbo C, so I gave up.
I knew it was too good to be true (Compile a simple C graphics routine)
Anyway, for fun, something similar jj2007's picture.

Code: Select all

 

Dim Shared As Integer xres,yres

Type V3
    As Single x,y,z
End Type

Type Line
    As v3 s,e '(s)tart,(e)nd
    As Ulong col
    Declare Sub Draw()
End Type

Type Circle Extends V3
    As Ulong clr
    As Long id
    Declare Sub Draw
End Type

Function contrast(c As Ulong) As Ulong 'for superimposing colours
    Randomize 1
    #define Intrange(f,l) Int(Rnd*((l+1)-(f))+(f))
    Dim As Ubyte r=Cptr(Ubyte Ptr,@c)[2],g=Cptr(Ubyte Ptr,@c)[1],b=Cptr(Ubyte Ptr,@c)[0],r2,g2,b2
    Do
        r2=Intrange(0,255):g2=IntRange(0,255):b2=IntRange(0,255)
    Loop Until Abs(r-r2)>120 Andalso Abs(g-g2)>120 Andalso Abs(b-b2)>120
    Return Rgb(r2,g2,b2) 
End Function

Sub line.draw()
    Line(s.x,s.y)-(e.x,e.y),col
End Sub

Sub circle.draw
    Circle(x,y),z,clr,,,,f
    Circle(x,y),z,Rgb(50,50,50)
    ..draw String(x-4*Len(Str(id)),y-8),Str(id),contrast(clr)
End Sub

Sub setupcircles(c() As Circle)
    #define map(a,b,x,c,d)  ((d)-(c))*((x)-(a))/((b)-(a))+(c)
    Dim As Long rad=30,ctr
    rad=(xres+300)/(11*4)
    Dim As Single pi=4*Atn(1)
    For n As Long=-150 To xres+150 Step rad
        ctr+=1
        Var x=n
        Var p=map(-150,(xres+150),n,0,(4*pi))
        Var y=map(-1,1,Sin(p),(.5*yres),(.8*yres))
        Redim Preserve c(1 To ctr)
        c(ctr)=Type<Circle>(x,y,.5*rad,Rgb(Rnd*255,Rnd*255,Rnd*255),ctr)
    Next
    Redim Preserve c(1 To Ubound(c)-1)
End Sub

Function perspective(p As V3,eyepoint As V3) As V3
    Dim As Single   w=1+(p.z/eyepoint.z)
    Return Type<V3>((p.x-eyepoint.x)/w+eyepoint.x,_
    (p.y-eyepoint.y)/w+eyepoint.y,_
    (p.z-eyepoint.z)/w+eyepoint.z)
End Function 

Function Regulate(Byval MyFps As Long,Byref fps As Long) As Integer
    Static As Double timervalue,lastsleeptime,t3,frames
    Var t=Timer
    frames+=1
    If (t-t3)>=1 Then t3=t:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-T+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=T
    Return sleeptime
End Function

sub setuplines(p() as line)
#define shade(c,n)  rgb(Cptr(Ubyte Ptr,@c)[2]*n,Cptr(Ubyte Ptr,@c)[1]*n,Cptr(Ubyte Ptr,@c)[0]*n)
Dim As Long counter
For n As Long=-400 To 1200 Step 10
    counter+=1
    Redim Preserve p(1 To counter)
    dim as ulong c=Rgb(100+Rnd*155,100+Rnd*155,100+Rnd*155)
    p(counter)=Type<Line>((n,350,-1500),(n,250,900),shade(c,.8))
Next n
end sub
'=====================
Screen 19,32
Color ,Rgb(207,229,255)
Screeninfo xres,yres

redim as line p()
setuplines(p())
Dim As Line w(1 To Ubound(p))'working array
Redim As Circle c()
setupcircles(c())

Dim As Single dx
Dim As Long fps
Do
    dx=.5
    Screenlock
    Cls
    Draw String(20,20),"Fps "&fps,0
    
    For n As Long=Lbound(p) To Ubound(p)
        p(n).s.x+=dx
        p(n).e.x+=dx
        If p(n).s.x>=1200 Then  p(n).s.x=-400:p(n).e.x=-400 'pop off end pop on beginning
        w(n).s=perspective(p(n).s,Type(400,300,1800))
        w(n).e=perspective(p(n).e,Type(400,300,1800))
        w(n).col=p(n).col
        w(n).draw
    Next n
    For n As Long=Lbound(c) To Ubound(c)
        c(n).x+=dx*4
        If c(n).x>=xres+150 Then  c(n).x=-150'cycle the circles
        c(n).draw
    Next n
    Screenunlock
    Sleep regulate(80,fps),1
Loop Until Len(Inkey)

 
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: porting plot library to FB?

Post by jj2007 »

dodicat wrote:Anyway, for fun, something similar jj2007's picture.
Compliments, that looks great!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: porting plot library to FB?

Post by MrSwiss »

@dodicat,

I've recoded your Contrast Function, for speed and enhanced efficiency.
The reason is, the complex end of loop check, which many times, if not
all three colors are found in 'one shot' causes a re-run.

It doesn't use any #define(s) or macro(s) ...

It uses three loops (one for each color ubyte), which is faster and with
less re-runs ... (can be checked easily, with CPU-load indicator from Task-
Manager in Win):

Code: Select all

Function Contrast( _                    ' for superimposing colours
    ByVal cin As ULong _                ' param:  'in' ARGB-Color
    ) As ULong                          ' result: ARGB-Color
    Dim As UByte    a = (cin Shr 24) And 255, _ ' save curr. alpha-chan
                    r = (cin Shr 16) And 255, _ ' save curr. red-chan
                    g = (cin Shr  8) And 255, _ ' save curr. green-chan
                    b = (cin And 255), _        ' save curr. blue-chan
                    r2, g2, b2                  ' temp. var's (uninit)
    ' minimal required difference from original: 127 / &h7F (aka: 50% of range)
    Do      ' run's only: until new red val found
        r2 = Rnd() * 256                ' get new random value
    Loop Until Abs(r - r2) > 126        ' when diff. big enough: quit loop
    Do      ' run's only: until new green val found (comments as above)
        g2 = Rnd() * 256
    Loop Until Abs(g - g2) > 126
    Do      ' run's only: until new blue val found (comments as above)
        b2 = Rnd() * 256
    Loop Until Abs(b - b2) > 126
    ' got them all: return result (leave alpha unchanged, 'as saved')
    Return (a Shl 24) + (r2 Shl 16) + (g2 Shl 8) + b2
End Function
For a quick check, just comment yours (in your demo) and insert mine instead.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: porting plot library to FB?

Post by jj2007 »

MrSwiss wrote:I've recoded your Contrast Function, for speed and enhanced efficiency.
Both versions run at 80 fps on my Core i5, but in dodicat's version, the numbers in the circles flicker much less.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: porting plot library to FB?

Post by MrSwiss »

jj2007 wrote:Both versions run at 80 fps on ...
You've clearly missed the point, since it's limited to 80 fps, anyway.
You may want to re-read my post ...
in dodicat's version, the numbers in the circles flicker much less.
Which means, it's slower ...
dodicat
Posts: 8236
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: porting plot library to FB?

Post by dodicat »

You have to randomise to 1 (or some other constant) in the sub, otherwise slightly different contrasting colours are returned, and you see a slight flashing.
But Mr Swiss's is faster.
See by asking for a silly framerate, 1000 fps or so, It'll do it's damnedest to get 1000.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: porting plot library to FB?

Post by MrSwiss »

dodicat wrote:... slightly different contrasting colours are returned ...
Yes, they are due, to the slightly enlarged difference (from: 120 to: 127),
the rest I don't really understand (it's a Function, not a Sub?).
dodicat
Posts: 8236
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: porting plot library to FB?

Post by dodicat »

ask for
Sleep regulate(10000,fps),1
and see what you get.
(put randomize 1 at the top of the function to stop flickering)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: porting plot library to FB?

Post by MrSwiss »

OK, I get an average of approximatly 480 fps, with mine and,
with yours, I'm getting about 333 fps ...
i7 mobile 3th gen. (ultra low power), but the limitting factor
seems to be, Intel built-in graphics 600 ...
Post Reply