đź’ˇ LightUp 7.1 | SketchUp's only real-time renderer that uses object-based rendering
Download Trial
Test array generation...
-
I made a 'array sort' for some testing, but wonder if it can be done better [in ruby 1.8.5] or if anyone has better to share...
<span class="syntaxdefault"></span><span class="syntaxcomment"># I'm drawing cubes when the user draws a line, this is †o simulate the user adding lines,<br /># I needed a large set of arrays, or a lot of time,<br /># I looked for pre-made sets to no avail so I made this for testing my core code.<br /># I can also deduct this time from the same settings with my 'cube' code, to gauge the real time it takes.<br />#<br /></span><span class="syntaxdefault">t1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Time</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">now<br /></span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">at_long_list </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">lng_lst </span><span class="syntaxkeyword">= ((Array.new(</span><span class="syntaxdefault">10</span><span class="syntaxkeyword">){|</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">*-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">}).</span><span class="syntaxdefault">reverse</span><span class="syntaxkeyword">)+(Array.new(</span><span class="syntaxdefault">9</span><span class="syntaxkeyword">){|</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">}) </span><span class="syntaxcomment"># => [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />#<br /></span><span class="syntaxdefault">test_counter </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">tst_cnt </span><span class="syntaxkeyword">= (Array.new(</span><span class="syntaxdefault">10</span><span class="syntaxkeyword">){|</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">*+</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">}) </span><span class="syntaxcomment"># set cluster f•ck size with this one , Array.new(1) will run the 'long_list' 19 times. 100 >> 1900<br />#<br /># test_counter runs a block,<br />#<br /></span><span class="syntaxdefault">tst_cnt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">map</span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">c</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxcomment">#<br /> # I have moved essentials inside, just to make it stand alone for others to try...<br /> #<br /> </span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">entities </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"JCB.cube"</span><span class="syntaxkeyword">) </span><span class="syntaxcomment">### name of my tool, could be anything...<br /> #<br /> # at_long_list runs an inner block, that does the work.<br /> #<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">lng_lst</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">map</span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">n</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxcomment">#<br /> # it names the group, and adds geometry to the model<br /> #<br /> </span><span class="syntaxdefault">grp </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br /> grp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"grp; #{c} #{n}"<br /> </span><span class="syntaxdefault">gents </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">grp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> </span><span class="syntaxcomment">#<br /> # short_list is derived using long_list .sort_by { rand }<br /> #<br /> </span><span class="syntaxdefault">shrt_lst </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">lng_lst</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sort_by </span><span class="syntaxkeyword">{ </span><span class="syntaxdefault">rand </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxcomment">#<br /> #<br /> </span><span class="syntaxdefault">a </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">shrt_lst</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0..2</span><span class="syntaxkeyword">]; </span><span class="syntaxcomment"># [] of 3 random number<br /> </span><span class="syntaxdefault">b </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">shrt_lst</span><span class="syntaxkeyword">[-</span><span class="syntaxdefault">3.</span><span class="syntaxkeyword">.-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]; </span><span class="syntaxcomment"># [] of 3 random number<br /> #<br /> # added a filter for zero pairings, I'm zipping a and b to check for zero and shuffle b if true...<br /> #<br /> </span><span class="syntaxdefault">d </span><span class="syntaxkeyword">= (</span><span class="syntaxdefault">a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zip b</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">map</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">m</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">n</span><span class="syntaxkeyword">| (</span><span class="syntaxdefault">m </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">n </span><span class="syntaxkeyword">)};<br /> if </span><span class="syntaxdefault">d </span><span class="syntaxkeyword">== [</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">];<br /> </span><span class="syntaxdefault">b1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">shrt_lst</span><span class="syntaxkeyword">[-</span><span class="syntaxdefault">4.</span><span class="syntaxkeyword">.-</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">];<br /> </span><span class="syntaxdefault">puts </span><span class="syntaxstring">";zero #{d};" </span><span class="syntaxcomment"># keep tabs on frequency<br /> </span><span class="syntaxdefault">end</span><span class="syntaxkeyword">; </span><span class="syntaxcomment">#if<br /> #<br /> # for me the line is a diagonal_line = diagl_ln<br /> #<br /> </span><span class="syntaxdefault">diagl_ln </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">gents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_line a</span><span class="syntaxkeyword">, (</span><span class="syntaxdefault">b </span><span class="syntaxkeyword">|| </span><span class="syntaxdefault">b1</span><span class="syntaxkeyword">); </span><span class="syntaxcomment"># if c = [0,0,0], use b1...<br /> #p b<br /> #<br /> # all my other code goes in here<br /> #<br /> </span><span class="syntaxdefault">p grp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxcomment">#<br /> #<br /> </span><span class="syntaxkeyword">} </span><span class="syntaxcomment">#close the inner block<br /> #<br /> #<br /> </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /> </span><span class="syntaxcomment">#<br /> #<br /></span><span class="syntaxkeyword">} </span><span class="syntaxcomment">#close the outer block<br />#<br />#<br /></span><span class="syntaxdefault">puts Time</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">now </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">t1<br /></span><span class="syntaxcomment">#<br /># </span><span class="syntaxdefault"></span>
as this is I get 180 edges 'grouped' in 0.99999
which is fast enough for my needs, but I wondered if there are better ways.
john -
Sorry, but can you do a short explanation of what you are doing? There's some cryptic variable names there, and some funky indentation. :s
-
TT
added commenting to the original code block to try and explain things better, I can't get the formatting to hold...
but this is better.
john
Advertisement