sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Angle = vector1.angle_between vector2

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 4 Posters 293 Views 4 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      When calculating the angle between two vectors using the method in the following statement: angle = vector1.angle_between vector2, where vector1 is the positive x axis, and vector2 is another vector in the x,y plane, the method provides correct answers for the first quadrant (0 to 90 degrees), but results in incorrect answers for the other quadrants (90 to 360 degrees). The following provided the correct angles:

      angle = vector1.angle_between vector2        #first quad
      if point_to.x < 0.0 and point_to.y > 0.0     #second quad
         angle = (Math;;PI/2) + (Math;;PI/2-angle)
      elsif point_to.x < 0.0 and point_to.y < 0.0  #third quad
         angle = (Math;;PI) + angle
      elsif point_to.x > 0.0 and point_to.y < 0.0  #fourth quad
         angle = (Math;;PI*1.5) + (Math;;PI/2-angle)
      end
      

      Not very elegant, so am I using the method incorrectly?

      1 Reply Last reply Reply Quote 0
      • R Offline
        remus
        last edited by

        angle_between seems to work as expected for me, this is my test code:

        vecx = Geom::Vector3d.new(1,0,0) vec1 =Geom::Vector3d.new(-1,-1,0) vecx.angle_between(vec1)

        which returns 2.35619449019234 radians(=135 degrees.)

        http://remusrendering.wordpress.com/

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          Perhaps its the way I generate vector2 =Geom::Vector3d.new ([v.x,v.y,0.0)], where v is a point somewhere in the model?

          1 Reply Last reply Reply Quote 0
          • daikuD Offline
            daiku
            last edited by

            vec1.angle_between(vec2) always returns a positive value. I struggled with this when trying to create a transform to make one object parallel with another. What I ended up doing was rotate by the angle returned by angle_between, and then checking the result to see if the objects were indeed parallel. If not, I would instead rotate by the opposite (negative) value. CB.

            Clark Bremer
            http://www.northernlightstimberframing.com

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

              Have you tried vector1.angle_between(vector2.reverse)?

              1 Reply Last reply Reply Quote 0
              • honoluludesktopH Offline
                honoluludesktop
                last edited by

                Tossed everything, went "mushin", empty mind, back to the api, and found in one line, exactly what I was trying to do:

                new_transformation = Geom::Transformation.new(point,vector_z)

                Sigh.....what was I thinking before?-(, Thanks for trying to help.

                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