1. Help with Simple Graphics Demo

Here's something just for fun. I was wondering if somebody could 
complete the missing puzzle piece for me...

I'd like the results charted in real time, so that as each roll is made, 
one of the bar graphs for the respective value would pop up by one 
value. Thanks!!!

-- start of code
--  code generated by Win32Lib IDE v0.18.6m
 
include Win32lib.ew
without warning

--------------------------------------------------------------------------------
--  Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, Default, 
Default, 276, 300, 0, 0 )
constant txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 )
constant LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 )
constant LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 )
constant txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 )
constant LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 )
constant txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48, 
20, 0, 0 )
constant bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156, 
68, 0, 0 )
constant Bitmap9 = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252, 
184, 0, 0 )
---------------------------------------------------------
--------------------------------------------------------------------------------

procedure update_roll_button()   
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides ) 
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

--------------------------------------------------------------------------------
procedure txt_Sides_onChange (integer self, integer event, sequence 
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Sides, w32HChange, routine_id("txt_Sides_onChange"))
--------------------------------------------------------------------------------
procedure bttn_Roll_onClick (integer self, integer event, sequence 
params)--params is ()
integer dice_num, dice_sides, dice_rolls, roll
sequence dice_results

    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides)

    for t=1 to dice_rolls do
        roll = rand( dice_sides )
        dice_results[roll] += 1

--YOUR HELP NEEDED HERE!!!
-- chart results in a bar graph in the available pixmap

    end for

end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))
--------------------------------------------------------------------------------
procedure txt_QTY_onChange (integer self, integer event, sequence 
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_QTY, w32HChange, routine_id("txt_QTY_onChange"))
--------------------------------------------------------------------------------
procedure txt_Rolls_onChange (integer self, integer event, sequence 
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Rolls, w32HChange, routine_id("txt_Rolls_onChange"))


WinMain( Window1,Normal )
---end of code

As an example (use non-proportional font to view)...

   |
 ||||
||||||
123456

A nice bell curve should be expected on 3d6x1000.

new topic     » topic index » view message » categorize

2. Re: Help with Simple Graphics Demo

C. K. Lester wrote:

> As an example (use non-proportional font to view)...
>
>   |
> ||||
> ||||||
> 123456
>
> A nice bell curve should be expected on 3d6x1000.

Oops! That bell curve should be looking like this:

       ||
       |||
    ||||||||
  ||||||||||||
||||||||||||||||
3456789012345678

(valid values are 3 to 18)

new topic     » goto parent     » topic index » view message » categorize

3. Re: Help with Simple Graphics Demo

Brian Broker wrote:

>
>
>Where's the "fun" when you want somebody else to do it?
>  
>
It's only missing a piece of the puzzle! I've already done all the hard 
work. :)

Look, I've seen tons of questions posted to this group about, "Can 
somebody do this..." or "How do I do that?" Ultimately, somebody posts 
code to do what the requestor requested. I figured I'd get the same kind 
of response.

I'll do it myself when I have a spare moment... I just thought somebody 
might be interested in helping out (thereby getting it done quicker).

I apologize if I offended you in any way.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Help with Simple Graphics Demo

Brian Broker wrote:

>See code below...  it may need some tweaking but it's a start.
>
>-- Brian
>  
>
That's great, Brian! Well done!!

I'm making minor tweaks, like clearing the pixmap before each new graph, 
I made it so there's a minimum window width (otherwise it was hiding 
part of the "Roll" button), etc... I'll post results here later.

Thank you,
ck

new topic     » goto parent     » topic index » view message » categorize

5. Re: Help with Simple Graphics Demo

Here's my take.  Needs some tweaking.

include Win32lib.ew
without warning

--------------------------------------------------------------------------------
--  Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, Default,
Default, 276, 300, 0, 0 )
constant txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 )
constant LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 )
constant LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 )
constant txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 )
constant LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 )
constant txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48,
20, 0, 0 )
constant bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156,
68, 0, 0 )
constant Bitmap9 = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252,
184, 0, 0 )

procedure update_roll_button()  
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides )
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

setHandler( txt_Sides, w32HChange, routine_id("txt_Sides_onChange"))
integer BIGGEST, WIDTH, HEIGHT

    roll = 0
    BIGGEST = 0
    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num)
   
    WIDTH = floor(244/(dice_num*dice_sides))
    HEIGHT = floor(100/(dice_num*dice_sides))
   
    for t=1 to dice_rolls do
        roll = 0
        for t2 = 1 to dice_num do
            roll += floor(rand(dice_sides*1000)/1000)+1
           end for
        dice_results[roll] += 1

        if dice_results[roll] > BIGGEST then   
            BIGGEST = dice_results[roll]
        end if
    end for
    setPenColor(Bitmap9,Black)
    drawRectangle(Bitmap9,1,0,0,244,183)
    setPenColor(Bitmap9,BrightRed)
    for loop = 1 to length(dice_results) do
        drawRectangle(Bitmap9,1,(loop-1) * WIDTH,183,((loop-1) * 
WIDTH)+WIDTH,183-(183-floor((dice_results[loop]/BIGGEST)*100)))
    end for
    copyBlt(Window1,8,80,Bitmap9)
end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))
setHandler( txt_QTY, w32HChange, routine_id("txt_QTY_onChange"))
setHandler( txt_Rolls, w32HChange, routine_id("txt_Rolls_onChange"))


WinMain( Window1,Normal )

new topic     » goto parent     » topic index » view message » categorize

6. Re: Help with Simple Graphics Demo

Brian Broker wrote:

>See code below...  it may need some tweaking but it's a start.
>  
>
Brian, where did I screw this up? It's not drawing the complete lines... 
only the tops. And even some are not being drawn. :/

include Win32lib.ew
without warning

sequence last_graph_size
    last_graph_size = {0,0}
--------------------------------------------------------------------------------

-- Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, 0, 0, 276, 
113, 0, 0 ),
  txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 ),
  LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 ),
  LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 ),
  txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 ),
  LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 ),
  txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48, 20, 0, 0 ),
  bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156, 68, 0, 
0 ),
  Graph = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252, 184, 0, 0 )
--------------------------------------------------------------------------------

procedure update_roll_button()  
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides )
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

  ------------------------------

procedure bttn_Roll_onClick (integer self, integer event, sequence
params)--params is ()
    atom width, gWidth, height
    integer dice_num, dice_sides, dice_rolls, roll, slot, maxVal
    sequence dice_results, graph_size, rolls

    -- clear last graph
    if not equal( last_graph_size, {0,0}) then -- clear it
        setPenColor( Graph, getSysColor( COLOR_BTNFACE ) )
        drawRectangle ( Graph, W32_True, 0, 0, last_graph_size[1], 
last_graph_size[2] )
        copyBlt( Window1,8,84,Graph)
    end if

    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)

--    first get a set of numbers
    rolls = {}
   
    for i = 1 to dice_rolls do
      roll = 0
      for j = 1 to dice_num do
        roll += rand( dice_sides )
      end for
      slot = roll-dice_num+1
      dice_results[slot] += 1
      rolls &= slot
    end for
   
    maxVal = 0
    for t=1 to length(dice_results) do
        if maxVal < dice_results[t] then
            maxVal = dice_results[t]
        end if
    end for

    -- now set window height
    graph_size = { 30*(dice_sides*dice_num-dice_num+1)+25, maxVal }

    gWidth = graph_size[1]+20
    if gWidth < 276 then
        gWidth = 276
    end if
   
    setCtlSize( Graph, graph_size[1], graph_size[2] )
    setCtlSize( Window1, gWidth, graph_size[2]+113 )

      setPenColor( Graph, Black )
    for i = dice_num to dice_num*dice_sides do
      setPenPos( Graph, 30*(i-dice_num), graph_size[2]-15 )
      wPrintf( Graph, "%d", {i} )
    end for

    for i=1 to dice_rolls do
        slot = rolls[i]
      drawLine(Graph,30*(slot)-30,graph_size[2]-20-dice_results[slot],
                     30*(slot)-20,graph_size[2]-20-dice_results[slot])
      copyBlt(Window1,8,84,Graph)
    end for

    last_graph_size = graph_size

--    ? dice_results
end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))

  ------------------------------

procedure txt_onChange (integer self, integer event, sequence
params)--params is ()
    -- update button when txt_Sides, txt_Rolls, or txt_QTY changes
    update_roll_button()
end procedure
setHandler( {txt_Sides,txt_Rolls,txt_QTY}, w32HChange,
routine_id("txt_onChange"))

  ------------------------------
 
procedure paintWindow( integer self, integer event, sequence params )
    copyBlt(Window1,8,84,Graph)
end procedure
setHandler( Window1, w32HPaint, routine_id("paintWindow") )

  ------------------------------
  WinMain( Window1,Normal )
  ------------------------------

new topic     » goto parent     » topic index » view message » categorize

7. Re: Help with Simple Graphics Demo

Brian Broker wrote:

>Oh, now I see what you are trying to do.  You want to set the window 
>height to account for the biggest number you'll get in the set.  Since 
>the y-value of the line is based on dice_results[slot], you'll need to 
>rebuild that sequence...  try this:
>
>    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)
>    for i=1 to dice_rolls do
>      slot = rolls[i]
>      dice_results[slot] += 1
>      drawLine(Graph,30*(slot)-30,graph_size[2]-20-dice_results[slot],
>                     30*(slot)-20,graph_size[2]-20-dice_results[slot])
>      copyBlt(Window1,8,84,Graph)
>    end for
>  
>
Works great, Brian. Thanks a LOT for your help. :)

new topic     » goto parent     » topic index » view message » categorize

8. Re: Help with Simple Graphics Demo

----- Original Message ----- 
From: "Brian Broker" <bkb at cnw.com>
To: <EUforum at topica.com>
Subject: RE: Help with Simple Graphics Demo


> 
> 
> mmmm... crap.  My bad.  Meant to send this privately. (no offense 
> intended to the hard-working D.P.)
> 

No offense taken blink

Yes, a lot of the demo programs are crap. So do something about it, people.

I've already added the dice-throw demo to the collection. 

-- 
Derek

new topic     » goto parent     » topic index » view message » categorize

9. Re: Help with Simple Graphics Demo

This is a multi-part message in MIME format.
--------------040805020909040804040603

Derek Parnell wrote:

>Yes, a lot of the demo programs are crap. So do something about it, people.
>  
>
I think the demo programs serve a very good purpose. Maybe some could be
combined to cut down on the volume, but I use the demo programs
extensively while I'm learning to program win32lib. It's taking a long
time to learn because I have precious few minutes to devote to the
task... of course, the IDE makes it disgustingly easy... heheh.

>I've already added the dice-throw demo to the collection.
>  
>
I'm attaching the latest version of Brian's and Evan's
die-rolling-graphing code... Derek, if you made any modifications
yourself, would you send me a copy?

Thanks!
-ck




--------------040805020909040804040603
Content-Type: text/plain;
 name="rolling_die_1.exw"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="rolling_die_1.exw"

include Win32lib.ew
without warning

sequence last_graph_size
	last_graph_size = {0,0}
--------------------------------------------------------------------------------

-- Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, 0, 0, 276, 113, 0,
0 ),
  txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 ),
  LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 ),
  LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 ),
  txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 ),
  LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 ),
  txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48, 20, 0, 0 ),
  bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156, 68, 0, 0 ),
  Graph = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252, 184, 0, 0 )
--------------------------------------------------------------------------------

procedure update_roll_button()   
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides ) 
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

  ------------------------------

procedure bttn_Roll_onClick (integer self, integer event, sequence 
params)--params is ()
	atom width, gWidth, height
    integer dice_num, dice_sides, dice_rolls, roll, slot, maxVal
    sequence dice_results, graph_size, rolls

	-- clear last graph
	if not equal( last_graph_size, {0,0}) then -- clear it
		setPenColor( Graph, getSysColor( COLOR_BTNFACE ) )
drawRectangle ( Graph, W32_True, 0, 0, last_graph_size[1], last_graph_size[2]
)
		copyBlt( Window1,8,84,Graph)
	end if

    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)

--	first get a set of numbers
	rolls = {}
	
    for i = 1 to dice_rolls do
      roll = 0
      for j = 1 to dice_num do
        roll += rand( dice_sides )
      end for
      slot = roll-dice_num+1
      dice_results[slot] += 1
      rolls &= slot
	end for
	
	maxVal = 0
	for t=1 to length(dice_results) do
		if maxVal < dice_results[t] then
			maxVal = dice_results[t]
		end if
	end for

	-- now set window height
	graph_size = { 30*(dice_sides*dice_num-dice_num+1)+25, maxVal }

	gWidth = graph_size[1]+20
	if gWidth < 276 then
		gWidth = 276
	end if
	
	setCtlSize( Graph, graph_size[1], graph_size[2] )
    setCtlSize( Window1, gWidth, graph_size[2]+113 )

      setPenColor( Graph, Black )
    for i = dice_num to dice_num*dice_sides do
      setPenPos( Graph, 30*(i-dice_num), graph_size[2]-15 )
      wPrintf( Graph, "%d", {i} )
    end for

    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)
    for i=1 to dice_rolls do
      slot = rolls[i]
      dice_results[slot] += 1
      drawLine(Graph,30*(slot)-30,graph_size[2]-20-dice_results[slot],
                     30*(slot)-20,graph_size[2]-20-dice_results[slot])
      copyBlt(Window1,8,84,Graph)
    end for
    
    last_graph_size = graph_size

--    ? dice_results
end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))

  ------------------------------

procedure txt_onChange (integer self, integer event, sequence 
params)--params is ()
    -- update button when txt_Sides, txt_Rolls, or txt_QTY changes
    update_roll_button()
end procedure
setHandler( {txt_Sides,txt_Rolls,txt_QTY}, w32HChange, 
routine_id("txt_onChange"))

  ------------------------------
  
procedure paintWindow( integer self, integer event, sequence params )
    copyBlt(Window1,8,84,Graph)
end procedure
setHandler( Window1, w32HPaint, routine_id("paintWindow") )

  ------------------------------
  WinMain( Window1,Normal )
  ------------------------------

--------------040805020909040804040603
Content-Type: text/plain;
 name="rolling_die_2.exw"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="rolling_die_2.exw"

include Win32lib.ew
without warning

-- 
-- Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, Default,
Default, 276, 300, 0, 0 )
constant txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 )
constant LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 )
constant LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 )
constant txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 )
constant LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 )
constant txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48,20, 0, 0 )
constant bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156,68, 0, 0
)
constant Bitmap9 = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252,184, 0, 0 )

-- 


procedure update_roll_button()
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides )
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

-- 

procedure txt_Sides_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Sides, w32HChange, routine_id("txt_Sides_onChange"))

-- 

procedure bttn_Roll_onClick (integer self, integer event, sequence
params)--params is ()
integer dice_num, dice_sides, dice_rolls, roll
sequence dice_results

integer BIGGEST, WIDTH, HEIGHT

    BIGGEST = 0
    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num)

    WIDTH = floor(252/(dice_num*dice_sides))
    HEIGHT = floor(100/(dice_num*dice_sides))

    for t=1 to dice_rolls do
        roll = 0
        for loop = 1 to dice_num do
            roll += floor(rand( (dice_sides-1)* 1000)/1000)+1
        end for
        dice_results[roll] += 1
        if dice_results[roll] > BIGGEST then
            BIGGEST = dice_results[roll]
       end if
    end for
    setPenColor(Bitmap9,Black)
    drawRectangle(Bitmap9,1,0,0,244,183)
    setPenColor(Bitmap9,BrightRed)
    for loop = 1 to length(dice_results) do
drawRectangle(Bitmap9,1,(loop-1) * WIDTH,183,((loop-1) *
       WIDTH)+WIDTH,183-(floor((dice_results[loop]/BIGGEST)*183)))
    end for
    copyBlt(Window1,8,80,Bitmap9)

end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))

-- 

procedure txt_QTY_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_QTY, w32HChange, routine_id("txt_QTY_onChange"))

-- 

procedure txt_Rolls_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Rolls, w32HChange, routine_id("txt_Rolls_onChange"))


WinMain( Window1,Normal ) 

--------------040805020909040804040603--

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu