Liberty BASIC Community Forum
« Graphicbox code reduction ... HELP!!! »

Welcome Guest. Please Login or Register.
Oct 23rd, 2017, 02:49am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2 3  4 Notify Send Topic Print
 veryhotthread  Author  Topic: Graphicbox code reduction ... HELP!!!  (Read 1464 times)
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Graphicbox code reduction ... HELP!!!
« Thread started on: Mar 10th, 2017, 1:38pm »

I can usually get my programs to work, but am undoubtedly using tons of inefficient code that I feel could be greatly reduced. Here's an example:

if color = 0 then #tab3.g01, "down; fill green; backcolor green"
#tab3.g01, "place 7 14; font arial 10 Bold"
#tab3.g01 "\"; counter1
#tab3.g01, "flush"

if color = 1 then #tab3.g02, "down; fill red; backcolor red"
#tab3.g02, "place 7 14; font arial 10 Bold"
#tab3.g02 "\"; counter2
#tab3.g02, "flush"

This continues for a few dozen graphicboxes denoted by #tab3.g01 to #tab3.g40. The color for each group can vary depending on the "color" number, while the "counter" number can range from 1 to 10. Shouldn't there be an easy way to simplify the code using FOR/NEXT statements? I spent days trying to shortcut all this repetitive code without success. Much thanks in advance for any help!


User IP Logged

MKnarr
Senior Member
ImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 431
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #1 on: Mar 10th, 2017, 1:55pm »

I "think" Var$ and #Var$ may help.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #2 on: Mar 10th, 2017, 2:19pm »

MKnarr: Thanks, I'm trying to figure out how to use Var$ or #Var$, but could really use an example or two if you could point me in the right direction. smiley
User IP Logged

Jack Kelly
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 17
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #3 on: Mar 10th, 2017, 2:41pm »

I'm no expert, but how about this? It uses text boxes but graphic boxes would probably work also.

Code:
NoMainWin 
for box = 1 to 10
    statictext #WIN.st, str$(box), 95, 78 + box*20, 18, 20
    textbox #WIN.tb1, 115, 72 + box*20, 350, 25
    textbox #WIN.tb2, 461, 72 + box*20, 40, 25
    stylebits #WIN.tb2, _ES_RIGHT, 0, 0, 0
    maphandle #WIN.st, "#WIN.statictext";box
    maphandle #WIN.tb1, "#WIN.TEXT";box
    maphandle #WIN.tb2, "#WIN.TEXT";box;".TEMP"
next
WindowWidth = 600
open "Looped controls" for window as #WIN
#WIN "trapclose [Quit]"
wait
[Quit]
close #WIN
end
 
User IP Logged

JohnnyD
Guru
ImageImageImageImageImage


member is offline

Avatar

"If something should, then it doesn't, isn't, can't, hasn't or won't"


Homepage PM

Gender: Male
Posts: 759
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #4 on: Mar 10th, 2017, 3:03pm »

Hi Turtleman,

This is the way I'd do it. #h$ holds the name of the handle and it is appended with the value of the for-next counter "a" to give the handle pointer. You then print to it as you would any handle name.

Hold the colours in an array then they can be referenced by a number ie the loop counter as can the variable "counter"

Code:
dim counter(5),colour$(5)

colour$(1)="green"
colour$(2)="red"
colour$(3)="blue"
colour$(4)="yellow"
colour$(5)="white"

for a=1 to 5
    h$="#tab.g0"+str$(a)
    if color=a-1 then #h$,"down; fill "colour$(a)+";
backcolor "+colour$(a)
    #h$, "place 7 14; font arial 10 Bold"
    #h$ "\"; counter(a)
    #h$, "flush"
next a
 


John.
User IP Logged

cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 626
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #5 on: Mar 10th, 2017, 3:11pm »

What John says, or create a variable to hold the colors:
colors$="red green blue"
Then pick one color using the WORD$() function.

Jack's code is not going to work.
User IP Logged

cundo
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #6 on: Mar 11th, 2017, 07:15am »

JohnnyD (and others): Thank you so much; I now have a much better understanding how to simplify things. Actually, I haven't put in the new color selection yet, as I'm now struggling to display the counter. (Currently, I can right or left click on a graphicbox to increment or decrement its associated counter, thus its displayed number. There are separate counters (labeled counter1, counter2, and so on), which go with the corresponding #tab. That is, counter1 goes with #tab.g01, counter 2 goes with #tab.g02, and so on.)

When a = 1, the counter needs to be counter1. When a = 2, the counter needs to be counter2, and so on. I imagine this is incredibly simple coding, yet I'm stumped how to go about it. I really hate showing off my ignorance, but can you offer any hints? Thanks!
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5545
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #7 on: Mar 11th, 2017, 09:31am »

forget about your static variable names. Use the counter() array that Johnny uses. So when a=1 counter(a) will be counter 1 when a=2 counter(a) will be counter 2
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #8 on: Mar 13th, 2017, 3:20pm »

A ton of repetitive code has been successfully reduced thanks to JohnnyD's "h$="#tab.g0"+str$(a)" suggestion and by adding:

if a = 1 then counter(a) = counter1
if a = 2 then counter(a) = counter2
if a = 3 then counter(a) = counter3
if a = 4 then counter(a) = counter4
if a = 5 then counter(a) = counter5.

Now I'm banging my head against the wall trying to do something similar in other sections of code. To grossly oversimplify, there are three parts to this. The third part, which has been covered, writes a digit (the contents of counters 1 5) to the graphicboxes. This is the code that has been greatly reduced.

The first part detects a left or right mouse click on one of five graphicboxes, which is used to increment or decrement the counters. For example:

'ROW 1
#tab3.g01 "when leftButtonUp [IncreaseBetCounter1]"
#tab3.g01 "when rightButtonUp [DecreaseBetCounter1]"

'ROW 2
#tab3.g02 "when leftButtonUp [IncreaseBetCounter2]"
#tab3.g02 "when rightButtonUp [DecreaseBetCounter2]"

And so on for the other three graphicboxes. Currently, separate graphicbox handles identify the grapicboxes. Perhaps this can be reduced too, although x and y mouse coordinates will be necessary to identify the graphicbox selected. For now, using separate graphicbox handles isn't too bad.

The second part, and the one with code that can probably be reduced, is used to increment or decrement each counter, which is subsequently displayed in the graphicboxes. For example:

[IncreaseBetCounter1]
counter1 = counter1 + 1
if counter1 < 1 then counter1 = max
if counter1 > max then counter1 = 1
gosub [DrawField1]
wait

[DecreaseBetCounter1]
counter1 = counter1 1
if counter1 < 1 then counter1 = max
if counter1 > max then counter1 = 1
gosub [DrawField1]
wait

[IncreaseBetCounter2]
counter2 = counter2 + 1
if counter2 < 1 then counter2 = max
if counter21 > max then counter2 = 1
gosub [DrawField2]
wait

[DecreaseBetCounter2]
counter2 = counter2 1
if counter2 < 1 then counter2 = max
if counter2 > max then counter2 = 1
gosub [DrawField2]
wait

and so on for the other three increase and decrease counters.

Obviously, there's a lot of redundant code; and unfortunately as before, I'm stuck! Anyone feel like showing me the "error of my ways?" Like I said, the examples furnished are greatly simplified; the actual code is around 10 times as much. That reason, and trying to learn how to write more efficient code, is my motivation for asking for help. Thanks again in advance! smiley
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5545
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #9 on: Mar 13th, 2017, 3:56pm »

If you use a sub to handle the mouse input instead of a named [branch] you will get to the sub and be given the handle of the control that was clicked. You can parse that handle to get the number of the control and so continue to use the counter(n) in place of static variable names.

So you can use one handler for multiple on screen controls. You use the handle given to the sub to update the control or to parse out the "number" to update variable(n) or create other relevant handles so that you can update almost any relevant control with a #handle variable
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5545
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #10 on: Mar 14th, 2017, 08:33am »

This code creates eight graphicbox sliders and uses the #handle technique to read and update those eight sliders with relatively little code. Most of the code is drawing the 3d box.

Code:
nomainwin

    global Dist,Perspective,MidX,MidY,Tmax,Tmin,degtorad,MaxPoints,MaxLines
    Dist = 180          'distance
    Perspective = 30    'perspective
    MidX = 250
    MidY = 250          'center of screen
    degtorad=atn(1)*4/180 'degrees x degtorad=radians

    WindowWidth = 800
    WindowHeight = 600
    graphicbox #w.g, 10,10,500,500
    graphicbox #w.g1, 520, 20, 182, 19
    statictext #w.st11, "0", 520, 5, 40, 15
    statictext #w.st12, "180", 600, 5, 40, 15
    statictext #w.st13, "359    X Axis", 680, 5, 140, 15

    graphicbox #w.g2, 520, 60, 182, 19
    statictext #w.st21, "0", 520, 45, 40, 15
    statictext #w.st22, "180", 600, 45, 40, 15
    statictext #w.st23, "359    Y Axis", 680, 45, 140, 15

    graphicbox #w.g3, 520, 100, 182, 19
    statictext #w.st31, "0", 520, 85, 40, 15
    statictext #w.st32, "180", 600, 85, 40, 15
    statictext #w.st33, "359    Z Axis", 680, 85, 140, 15

    graphicbox #w.g4, 520, 140, 182, 19
    statictext #w.st41, "-90", 520, 125, 40, 15
    statictext #w.st42, "0", 600, 125, 40, 15
    statictext #w.st43, "90     X Translate", 680, 125, 140, 15

    graphicbox #w.g5, 520, 180, 182, 19
    statictext #w.st51, "-90", 520, 165, 40, 15
    statictext #w.st52, "0", 600, 165, 40, 15
    statictext #w.st53, "90     Y Translate", 680, 165, 140, 15

    graphicbox #w.g6, 520, 220, 182, 19
    statictext #w.st61, "-90", 520, 205, 40, 15
    statictext #w.st62, "0", 600, 205, 40, 15
    statictext #w.st63, "90     Z Translate", 680, 205, 140, 15

    graphicbox #w.g7, 520, 260, 182, 19
    statictext #w.st71, "-180", 520, 245, 40, 15
    statictext #w.st72, "0", 600, 245, 30, 15
    statictext #w.st73, "180   Distance", 680, 245, 140, 15

    graphicbox #w.g8, 520, 300, 182, 19
    statictext #w.st81, "-90", 520, 285, 40, 15
    statictext #w.st82, "0", 600, 285, 30, 15
    statictext #w.st83, "90     Perspective", 680, 285, 140, 15



    open "3D Rotation" for window as #w
    #w "trapclose quit"
    #w.g "down ; fill white"

    sliders=8
    dim slide(sliders,2) 'slide bar position and scaled value
    'set the starting positions that are not zero
    slide(4,2)=90
    slide(5,2)=90
    slide(6,2)=90
    slide(7,2)=Dist
    slide(8,2)=Perspective+90


    'using handle variable set all slider fonts postions and events
    for n= 1 to sliders
        s$="#w.g"+str$(n)
        st1$="#w.st"+str$(n)+"1"
        st2$="#w.st"+str$(n)+"2"
        st3$="#w.st"+str$(n)+"3"

        #st1$ "!font arial 10"
        #st2$ "!font arial 10"
        #st3$ "!font arial 10"

        #s$ "down ; backcolor black"
        call display s$

        #s$ "when leftButtonMove mouse"
        #s$ "when leftButtonUp mouse"
        #s$ "when characterInput key"

    next

    'load point and line data
    restore [PointData] 'Set pointer to read from label PointData.
    read MaxPoints 'Reads MaxPoints from appended data.
    dim pointData(MaxPoints,3) 'x,y,z
    dim screenData(MaxPoints,3) 'x,y,z 'points after rotation
    dim scrnX(MaxPoints), scrnY(MaxPoints) 'points drawn to screen
    for i = 1 to MaxPoints
        read x,y,z
        pointData(i,1)=x
        pointData(i,2)=y
        pointData(i,3)=z

    next

    restore [LineData] 'Set pointer to read from label LineData.
    read MaxLines 'Reads MaxLines from appended data.
    dim l(MaxLines,3) '1ST,2ND,COLOR line data
    dim oldX1(MaxLines), oldY1(MaxLines) 'old line data
    dim oldX2(MaxLines), oldY2(MaxLines)
    for i = 1 to MaxLines
        read C1,C2,CLR
        l(i,1)=C1
        l(i,2)=C2
        l(i,3)=CLR
    next
    call draw
    wait





sub draw
    #w.g "discard"
    'rotate and translate the points acccording to the slider values
    'then draw over the old lines and draw the new lines
    'core code from WIRE3DUO.BAS by Matt Bross, 1997
    '*********************************ROTATION******************************
    'set R1 R2 or R3 in any combination to rotate x,y,z plane 360o
    R1 = slide(1,1) 'rotate around the x axis 0 to 359
    R2 = slide(2,1) 'rotate around the y axis 0 to 359
    R3 = slide(3,1) 'rotate around the z axis 0 to 359

    '********************************TRANSLATION****************************
    'set mmX mmY and mmZ to translate (slide) points on x,y,z plane
    mmX=slide(4,1) 'translate x -90 to +90
    mmY=slide(5,1) 'translate y -90 to +90
    mmZ=slide(6,1) 'translate z -90 to +90
    Dist=slide(7,1)
    Perspective=slide(8,1)

    '******************************ROTATE POINTS****************************
    S1 = sin(R1*degtorad): S2 = sin(R2*degtorad): S3 = sin(R3*degtorad)
    C1 = cos(R1*degtorad): C2 = cos(R2*degtorad): C3 = cos(R3*degtorad)
    for i = 1 to MaxPoints
        'Rotate points around the y axis.
        tempX = (pointData(i,1) * C2 - pointData(i,3) * S2)
        tempZ = (pointData(i,1) * S2 + pointData(i,3) * C2)
        'Rotate points around the x axis.
        screenData(i,3) = (tempZ * C1 - pointData(i,2) * S1)
        tempY = (tempZ * S1 + pointData(i,2) * C1)
        'Rotate points around the z axis.
        screenData(i,1) = (tempX * C3 + tempY * S3)
        screenData(i,2) = (tempY * C3 - tempX * S3)
        '*****************************CONVERT 3D TO 2D**************************
        tempZ = screenData(i,3) + mmZ - Perspective
        if tempZ < 0 then  'calculate points visible to the viewer
            scrnX(i) = (screenData(i,1) + mmX) * Dist / tempZ + MidX
            scrnY(i) = (screenData(i,2) + mmY) * Dist / tempZ + MidY
        end if
    next
    '******************************DRAW POLYGONS****************************
    for i = 1 to MaxLines
        p1 = l(i,1) : p2 = l(i,2) : color=l(i,3)
        'erase old line
        #w.g "color white"
        #w.g "line ";oldX1(i);" ";oldY1(i);" ";oldX2(i);" ";oldY2(i)
        'remember new points for erasing later
        oldX1(i) = scrnX(p1): oldY1(i) = scrnY(p1)
        oldX2(i) = scrnX(p2): oldY2(i) = scrnY(p2)
        'Draw line from p1 to p2 in color clr
        if color=1 then #w.g "color black"
        if color=2 then #w.g "color red"
        if color=3 then #w.g "color green"
        #w.g "line ";scrnX(p1);" ";scrnY(p1);" ";scrnX(p2);" ";scrnY(p2)
    next
    call delay
end sub


sub delay
    'timer must be encased with a sub to function properly
    timer 100,[done]
    wait
    [done]
    timer 0
    call draw
end sub

sub quit handle$
    'close the program
    timer 0
    close #w
    end
end sub

sub key handle$,c$
    'react to left and right cursor on the currently selected slider
    key=asc(right$(c$,1))
    s=val(right$(handle$,1))
    if key=_VK_LEFT then slide(s,2)=slide(s,2)-1
    if key=_VK_RIGHT then slide(s,2)=slide(s,2)+1
    slide(s,2)= min(180,max(0,slide(s,2)))
    call display handle$
end sub


sub mouse handle$,x,y
    'react to mouse movement and left button up on the selected slider
    s=val(right$(handle$,1))
    slide(s,2)= min(180,max(0,x))
    call display handle$
end sub

sub display handle$
    'update the slider display and save the new values
    s=val(right$(handle$,1))
    if s<4 then
        slide(s,1)=slide(s,2)*2
    else
        if s=7 then
        slide(s,1)=slide(s,2)*2-180
        else
        slide(s,1)=slide(s,2)-90
        end if
    end if
    st$="#w.st"+str$(s)+"2"
    #st$ slide(s,1)
    #handle$ "discard"
    #handle$ "fill darkgray"
    #handle$ "line 0 8 180 8"
    #handle$, "place ";slide(s,2)-4;" 0 ; boxfilled  ";slide(s,2)+4;" 17"
end sub






[PointData]
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CUBE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'number of points
DATA 8
'Location of points (x, y, z)
DATA -10, 10,-10,  -10,-10,-10,  -10, 10, 10,  -10,-10, 10
DATA  10, 10, 10,   10,-10, 10,   10, 10,-10,   10,-10,-10

[LineData]
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CUBE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'number of lines
DATA 12
'The point data above can be numbered, the first data statement is 1.  The
'line data lists point data numbers to link from, point 1 to point 2 in color 3.
DATA 1,2, 2,    1,3, 2,   3,4, 2,   2,4, 2,   1,7, 1,   3,5, 1,   5,7, 3
DATA 5,6, 3,    7,8, 3,   6,8, 3,   4,6, 1,   2,8, 1






 
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #11 on: Mar 14th, 2017, 11:59am »

Thanks Rod, though the slider code example will be a challenge to fully understand and apply to my application. Interestingly, as per your preceding post, I am calling a sub with mouse inputs elsewhere in my program. That code uses only a single graphicbox with a couple dozen blocks "partitioned" within. I had a problem, however, trying to selectively alter the color of individual blocks based on external events. That's why I went to defining separate graphicboxes, but ran into too much repetitive code. Frankly, I don't know which way to go.

All I'm trying to do is selectively increment, decrement, and save and display the count. I also need to be able to selectively change color based on external events. I've been working on this section for several weeks now, am totally burned out, and give up almost daily. Then, of course, I'm at it again the next day! Thanks for your help maybe I'll figure it out yet.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5545
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #12 on: Mar 14th, 2017, 12:30pm »

Then perhaps we all need to take a step back. Instead of coding solutions why not post a descriptive piece about your project. What is the ultimate purpose of all these graphic controls. You might be surprised how other folks would tackle the problem.
User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1684
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #13 on: Mar 14th, 2017, 3:58pm »

Quote:
why not post a descriptive piece about your project.

And the picture(s). Picture worth 1000 words.
Draw them, post it somewhere (like Postimage.org), post link here.
User IP Logged

damned Dog in the Manger
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: Graphicbox code reduction ... HELP!!!
« Reply #14 on: Mar 15th, 2017, 06:26am »

Rod & tsh73: Thanks again for your suggestions. I may post a pic and better description of what I'm trying to do, but am going to try a few more things first. I had forgotten that in another section of the program, I'm already selecting one of a couple dozen graphicboxes using a sub with mouse inputs and can selectively change and store colors for each box. So, I can figure out how to do color changes without mouse inputs, I'll have it made! Will report back later.
User IP Logged

Pages: 1 2 3  4 Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls