sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Update | Sketchucation Tools 5.0.8 released with licensing improvements and bugfixes Download

    Min. z value of some points

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 5 Posters 653 Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      niccah
      last edited by

      Hi!

      I have lots of 3D points: points = [[1, 2, 3], [2, 3, 5], ...]

      Is there a easy way to get for example the minimum z value of all these points in Ruby?

      At the moment I have a points.each loop and compare every z value with the last one....

      Thanks for your help!

      1 Reply Last reply
      Reply Quote 0
      • thomthomT
        thomthom
        last edited by

        min = points.inject(0) { |m,pt| ( pt.z < m ) ? pt.z : m }

        Inject have many neat usages - I noted a couple of examples: http://forums.sketchucation.com/viewtopic.php?t=41176&p=364915

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply
        Reply Quote 0
        • thomthomT
          thomthom
          last edited by

          Haven't checked if it is faster, but BoundingBox might help:

          <span class="syntaxdefault">bb </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">BoundingBox</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxdefault">bb</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> points </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">min </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> bb</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">min</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">z</span>
          

          Since it's SU's native code doing the handling of the points, it might yield faster calculations.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply
          Reply Quote 0
          • N
            niccah
            last edited by

            oh thank you very much!

            I saw the function "inject", but I ignored them because I thought, that's not interesting for me.

            Just for the sake of completeness: One have to add a "Geom::" in the front of "BoundingBox.new"

            Thanks a lot again for your help!

            br

            1 Reply Last reply
            Reply Quote 0
            • thomthomT
              thomthom
              last edited by

              @niccah said:

              Just for the sake of completeness: One have to add a "Geom::" in the front of "BoundingBox.new"

              👍 Good catch! Updated the example.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply
              Reply Quote 0
              • fredo6F
                fredo6
                last edited by

                to be absolutely accurate, I guess the inject statement should be

                min = points.inject(points[0].z) { |m,pt| ( pt.z < m ) ? pt.z : m }

                Fredo

                PS: Did not know this method, as it is not documented in the Pragmatic Programmer's guide. Seems useful and elegant

                1 Reply Last reply
                Reply Quote 0
                • Dan RathbunD
                  Dan Rathbun
                  last edited by

                  @unknownuser said:

                  PS: Did not know this method, as it is not documented in the Pragmatic Programmer's guide. Seems useful and elegant

                  Do not use the object reference from that OLD guide, which was written in the Ruby v 1.6.x timeframe. (The tutorial text is still relevant and recommended, however.)

                  Instead use the v 1.8.6 CHM here

                  or access on-line http://www.ruby-doc.org/core-1.8.6/index.html.

                  Note that these methods come from the mixin module Enumerable which is mixed into Array, Hash, IO (and subclasses, such as File,) Range, String, Struct, ... etc.

                  I'm not here much anymore.

                  1 Reply Last reply
                  Reply Quote 0
                  • Dan RathbunD
                    Dan Rathbun
                    last edited by

                    @unknownuser said:

                    to be absolutely accurate, I guess the inject statement should be

                    min = points.inject(points[0].z) { |m,pt| ( pt.z < m ) ? pt.z : m }

                    At, one time, in another thread, we were comparing various methods of getting min and max, speedwise.

                    There is also:

                    min_z = points.min {|a,b| a.z <=> b.z }

                    Can't remember which was the faster.

                    P.S.: Because min and max are method names in Enumerable, and that they are mixed into quite a few classes, .. it is recommended to not use them as var names.
                    Difference them. It seems that the API's tradition is to add an "_" and then a descriptor: min_x, min_y, min_z, min_height, min_width, min_size, etc.

                    I'm not here much anymore.

                    1 Reply Last reply
                    Reply Quote 0
                    • N
                      niccah
                      last edited by

                      This forum is amazing!

                      I learn a lot from you! Thanks a lot for all these information!

                      1 Reply Last reply
                      Reply Quote 0
                      • D
                        david.
                        last edited by

                        This is how I would do it:

                        minZ = points.collect { |pt| pt.z }.min
                        
                        1 Reply Last reply
                        Reply Quote 0
                        • Dan RathbunD
                          Dan Rathbun
                          last edited by

                          @david: Your example should be slower, as it must iterate 2 arrays, and also create a temporary array.

                          Why ?? (I suspect you like the "readability" of your version.)

                          I'm not here much anymore.

                          1 Reply Last reply
                          Reply Quote 0
                          • D
                            david.
                            last edited by

                            Yes, readability. If it was operating on large quantities of data, I would definitely use your suggested method.

                            1 Reply Last reply
                            Reply Quote 0

                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                            With your input, this post could be even better 💗

                            Register Login
                            • 1 / 1
                            • First post
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement