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!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Is Point between Points?

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 372 Views 3 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.
    • thomthomT Offline
      thomthom
      last edited by

      Given three points A, B and C which are co-linear, what is the best practice way to determine if C is between A and B?

      I searched the net and there appear to be various solutions, but there is the question of dealing with precision errors...

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

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

        I hacked together this:

        
        def self.point_between(a, b, c)
          v1 = c.vector_to(a)
          v2 = c.vector_to(b)
          return true if !v1.valid? || !v2.valid?
          !v1.samedirection?(v2)
        end
        
        

        How bad is that?

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

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          RickW did something...
          point1.between?(point2,point3)
          ???
          Not sure where it is ...

          TIG

          1 Reply Last reply Reply Quote 0
          • C Offline
            cjthompson
            last edited by

            Do you know why just comparing the individual X,Y and Zs won't work?

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

              @cjthompson said:

              Do you know why just comparing the individual X,Y and Zs won't work?

              Like how?

              I'm curious in terms of performance and accuracy.

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

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Here's one of several examples that are around

                
                class Geom;;Point3d
                  def between?(pt1, pt2, cond)
                    # original idea by RickW
                    # self is the point3d to test
                    # cond if true returns true if self is at and end point, pt1 or pt2
                    # cond if false does not include for self on an end point, pt1 or pt2
                    # returns true if self is on the line between pt1 and pt2
                    # returns false if not.
                    d1 = self.distance(pt1) + self.distance(pt2)
                    d2 = pt1.distance(pt2) + 0.0
                    if (d1 <= d2) || (d1-d2 < 1e-10)
                      if cond
                        return true 
                      else
                        return true if self != pt1 && self != pt2
                      end#if
                    end#if
                    return false
                  end#def
                end#class
                

                TIG

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

                  1e-10 - is that the tolerance?

                  I refined my previous hack. I have tried this method in hopes that the tolerance in the methods used makes for a result that matches how SU works. But I'm not all too sure. And I have not benchmarked it.

                  
                  # Checks if point C is between line segment AB.
                  # Uses Sketchup methods to match it's accuracy. (Reliable?)
                  # Compares the vectors from C to A and B. Assumes Zero vector means that C
                  # is on the same point as A or B, which counts as 'between'.
                  def self.point_between(a, b, c)
                    v1 = c.vector_to(a)
                    v2 = c.vector_to(b)
                    return true unless v1.valid? && v2.valid?
                    v1.samedirection?(v2.reverse!)
                  end
                  
                  

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

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

                    Btw, here is one of the links I looked at while reading up on this: http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment

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

                    1 Reply Last reply Reply Quote 0
                    • C Offline
                      cjthompson
                      last edited by

                      @TIG
                      You should probably convert d1 and d2 to float before comparing them:
                      0.0001.inch == 0.001.inch #=> true

                      BTW, do you have to determine whether the point is on the line, or is it just assumed?

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        cjthompson
                        last edited by

                        something like this:

                        
                        def pointBetween(a,b,c)
                        	array = [[a.x.to_f,b.x.to_f],[a.y.to_f,b.y.to_f],[a.z.to_f,b.z.to_f]]
                        	x = c.x.to_f.between?(*array[0].sort)
                        	y = c.y.to_f.between?(*array[1].sort)
                        	z = c.z.to_f.between?(*array[2].sort)
                        	return x && y && z
                        end
                        
                        

                        I'm not saying it will work, I'm just wondering whether it will or not.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement