About 4D Systems PTY LTD
4D Systems, Australia
Home Company Products 4DGL Developers Center Support forum Distributors News & Events Contact us App Notes
4DGL - Sharing our bytes to the world

AnalogueStyleMetering.4DG


Codebase / PICASO-GFX / PICASO-GFX2 and 4DGL Display Modules / Generic Examples / Graphics

Concentric circular dials with triangular pointers demo.

When drawing analogue style meters, a triangle is often used as a pointer. This demo shows how to rotate triangles for that purpose.
About this code
Author: The 4D team
Uploaded on: 28/03/2011

Back to previous page

Pictures:

Previous Next

Source code:

#platform "uLCD-32032-P1_GFX2"


// Multiple circular dials with triangular pointers demo


#inherit "4DGL_16bitColours.fnc"



#constant FACECOLOUR GRAY

#constant POINTERS 4

    // global variables
    var vals[POINTERS]      := [20,50,80,130];          // initial values
    var oldvals[POINTERS];
    var placement[POINTERS] := [18,36,54,72];           // pointer distance
    var colors[POINTERS]    := [RED,LIME,CYAN,MAGENTA]; // pointer colours

    var integrators[POINTERS];  // for demo values

    var targetX, targetY;
    var screenwidth, screenheight;
    var xc, yc, r;



// draw required pointer
func DrawPointer(var length, var angle, var colour)
    var x1,y1,x2,y2,x3,y3;

    gfx_Set(OBJECT_COLOUR, colour);

    gfx_MoveTo(xc, yc);

    gfx_Orbit(angle, length);                       // create points for triangle
    x2 := targetX;
    y2 := targetY;

    gfx_Orbit(angle - 6, length - 12);
    x1 := targetX;
    y1 := targetY;

    gfx_Orbit(angle + 6, length - 12);
    x3 := targetX;
    y3 := targetY;

    gfx_TriangleFilled(x1,y1,x2,y2,x3,y3,colour);   // draw the pointer
    //gfx_Triangle(x1,y1,x2,y2,x3,y3,colour);       // draw the pointer

    gfx_Orbit(angle, length - 12);
    gfx_MoveTo(xc, yc);                             // line from centre to pointer
    gfx_LineTo(targetX,targetY);                    // (nqr, should always be drawn)

endfunc

func Draw()
    var n;
    var private first;
    for(n := 0; n < POINTERS; n++)
        if(vals[n] != oldvals[n])
            if(first) DrawPointer(r - placement[n], oldvals[n], FACECOLOUR);  // undraw the old pointer
            DrawPointer(r - placement[n], vals[n], colors[n]);                // redraw the pointer
            oldvals[n] := vals[n];
        endif
        gfx_Circle( xc, yc, r - placement[n]+2, colors[n]);
    next
    gfx_CircleFilled( xc, yc, 3, ORANGE);
    first := 1;
endfunc


func main()
    var n, t;

    // get the screen resolution and centrepoint
    screenwidth := gfx_Get( X_MAX );
    screenheight := gfx_Get( Y_MAX );
    xc := screenwidth >> 1;
    yc := screenheight >> 1;

    // calculate a radius suitable for landscape or portrate mode
    r := MIN(screenwidth, screenheight) >> 1;

    // draw the clock face
    gfx_CircleFilled( xc, yc, r - 10, FACECOLOUR );


    // target variables for the orbit command
    gfx_OrbitInit(&targetX, &targetY);


    repeat

        t := sys_T() + 30;
        while(sys_T() != t);                                    // wait 30msec before we redraw

        // demo, generate some random deviations
        for(n:=0; n < POINTERS; n++)
            integrators[n] += (RAND() & 3) - 1 ;                // +1, 0 or -1
            vals[n] += ((integrators[n] & 48) >> 3) - 4;
            vals[n] := vals[n] % 360;
        next

        Draw();

    forever



endfunc

//==================================================================================================



//==================================================================================================

//==================================================================================================


Copyright © 2007 4D Systems Pty Ltd, Sydney, Australia - All Rights Reserved | Terms and Conditions