1 Replies - 3360 Views - Last Post: 20 May 2012 - 05:09 AM

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Calculate the Distance between two 2D or 3D points.

Posted 21 September 2009 - 01:43 PM

Description: Use: //Gets the distance between two points on a 2D surface MessageBox.Show(Distance2D(-2, -3, -4, 4).ToString()); //Gets the distance between two points on a 3D surface MessageBox.Show(Distance3D(3, -4, 7, -4, 9, 13).ToString());Gets the distance between two 2D or 3D points.
        /// 
        /// Finds the distance between two points on a 2D surface.
        /// 
        /// The point on the x-axis of the first point
        /// The point on the x-axis of the second point
        /// The point on the y-axis of the first point
        /// The point on the y-axis of the second point
        /// 
        public int Distance2D(int x1, int y1, int x2, int y2)
        {
            //     ______________________
            //d = √ (x2-x1)^2 + (y2-y1)^2
            //

            //Our end result
            int result = 0;
            //Take x2-x1, then square it
            double part1 = Math.Pow((x2 - x1), 2);
            //Take y2-y1, then sqaure it
            double part2 = Math.Pow((y2 - y1), 2);
            //Add both of the parts together
            double underRadical = part1 + part2;
            //Get the square root of the parts
            result = (int)Math.Sqrt(underRadical);
            //Return our result
            return result;
        }

        /// 
        /// Finds the distance between two points on a 3D surface.
        /// 
        /// The point on the x-axis of the first point
        /// The point on the x-axis of the second point
        /// The point on the y-axis of the first point
        /// The point on the y-axis of the second point
        /// The point on the z-axis of the first point
        /// The point on the z-axis of the second point
        /// 
        public int Distance3D(int x1, int y1, int z1, int x2, int y2, int z2 )
        {
            //     __________________________________
            //d = √ (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2
            //

            //Our end result
            int result = 0;
            //Take x2-x1, then square it
            double part1 = Math.Pow((x2 - x1), 2);
            //Take y2-y1, then sqaure it
            double part2 = Math.Pow((y2 - y1), 2);
            //Take z2-z1, then square it
            double part3 = Math.Pow((z2 - z1), 2);
            //Add both of the parts together
            double underRadical = part1 + part2 + part3;
            //Get the square root of the parts
            result = (int)Math.Sqrt(underRadical);
            //Return our result
            return result;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate the Distance between two 2D or 3D points.

#2 DanielLeone  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: Calculate the Distance between two 2D or 3D points.

Posted 20 May 2012 - 05:09 AM

Nice, works perfectly, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1