简体   繁体   中英

How to calculate the distance between 2 point in c#

I would like to calculate the distance between 2 point by using this formulae https://m.wikihow.com/Find-the-Distance-Between-Two-Points . But how do I write the formulae into it. How can I change the var to double? Because I cannot perform the mathematic operation because it the array is var The following is my sample code:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console1
{
    class Program
    {
        static void Main(string[] args)
        {

            DataTable dt1= ConvertToDataTable(@"C:\Users\manchunl\Desktop\Sample1.txt", 10);
            DataTable dt2= ConvertToDataTable2(@"C:\Users\manchunl\Desktop\Sample2.txt", 10);

            foreach (DataRow row in dt1.AsEnumerable())
            {
                string.Join(",", row.ItemArray.Select(x => x.ToString()));
            }

            foreach (DataRow row in dt2.AsEnumerable())
            {
                string.Join(",", row.ItemArray.Select(x => x.ToString()));
            }


            foreach (DataRow row1 in dt1.Rows)
            {
                foreach (DataRow row2 in dt2.Rows)
                {
                    var array1 = row1.ItemArray;
                    var array2 = row2.ItemArray;

                    if (array2[4].Equals(array1[4]))
                    {
                        var x1 = array1[7];
                        var y1 = array1[8];
                        var x2 = array2[7];
                        var y2 = array2[8];



                    }
                }
            }


            Console.WriteLine();
            Console.WriteLine("Press enter to exit.");
            Console.Read();
        }


        public static DataTable ConvertToDataTable(string filePath, int numberOfColumns)
        {


            DataTable tbl = new DataTable();

            for (int col = 0; col < numberOfColumns; col++)
                tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));


            string[] lines = System.IO.File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                var cols = line.Split(null);

                DataRow dr = tbl.NewRow();
                for (int cIndex = 0; cIndex < numberOfColumns; cIndex++)
                {
                    dr[cIndex] = cols[cIndex];

                }

                tbl.Rows.Add(dr);

            }

            return tbl;
        }

        public static DataTable ConvertToDataTable2(string filePath, int numberOfColumns)
        {


            DataTable tbl = new DataTable();

            for (int col = 0; col < numberOfColumns; col++)
                tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));


            string[] lines = System.IO.File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                var cols = line.Split(',');

                DataRow dr = tbl.NewRow();
                for (int cIndex = 0; cIndex < numberOfColumns; cIndex++)
                {
                    dr[cIndex] = cols[cIndex];

                }

                tbl.Rows.Add(dr);

            }

            return tbl;
        }

    }
}

You can use Math functionality to calculate slope between two points

int distX = int.Parse(x2) - int.Parse(x1);
int distY = int.Parse(y2) - int.Parse(y1);

double result = Math.Sqrt(distX * distX + distY * distY)

Sqrt() and Pow() , both are static functions of Math class.

Using Pow function

double result = Math.Sqrt(Math.Pow(distX, 2) + Math.Pow(distY,2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM