locked
Calculating a distance between random points. RRS feed

  • Question

  • Hi everyone,

    so my situation is like this :

    1) I have created a graph containing random points (3 different series). And the graph have three buttons .The first button will plot 30 random points on the chart. the second and third button will plot random point with different colour.

    What I am trying to do now is I need to choose one random point from the 30 points, and calculate the distance from the chosen point to the qrand or qinit..

    How can I do that? I really have no idea. Help!

     public void ForBtn1()
            {
                 Random qrand = new Random();
                int[,] points = new int[100, 2];
                int qinitx, qinity;
    
                for (int i = 0; i < 30; i++)
                {
                    int pointx = qrand.Next(0, 100);
                    int pointy = qrand.Next(0, 100);
    
    
    
                    points[i, 0] = pointx;
                    points[i, 1] = pointy;
    
                    qinitx = points[0,0];
                    qinity = points[0, 1];
    
                    chart1.Series["Initial Point"].Points.AddXY(pointx, pointy);
                    chart1.Series["Initial Location"].Points.AddXY(qinitx,qinity);
                }
            }
            
            //To initialize random points and initial point
            private void button1_Click(object sender, EventArgs e)
            {
                ForBtn1();
            }
    
            private void chart1_Click(object sender, EventArgs e)
            {
    
            }
    
            //To initialize rand_config
            private void button2_Click(object sender, EventArgs e)
            {
                Random qrand = new Random();
                int[,] points = new int[100, 2];
                int qrandx, qrandy;
    
             
                    qrandx = qrand.Next(0,100);
                    qrandy = qrand.Next(0,100);
                    chart1.Series["Random Configuration"].Points.AddXY(qrandx, qrandy);
    
    
               
    
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                
            }

    Tuesday, March 31, 2015 5:10 AM

Answers

  • You can calculate the distance between two points by adding the square of the difference in x values to the square of the difference in y values,  then taking the square root .

    • Edited by Ante Meridian Tuesday, March 31, 2015 5:21 AM typo.
    • Marked as answer by Caillen Wednesday, April 8, 2015 7:06 AM
    Tuesday, March 31, 2015 5:21 AM
  • When getting random items in a list you don't want to pick the same item twice.  So normally you assign a random number to every item in the list and then sort by the random number.  Your two random point will be the first and second item in the sort list.  Try the code below. sortedList is a N x 2 array where one column is the points and the 2nd column is the points.

           private void button1_Click(object sender, EventArgs e)
            {
                Random rand = new Random();
                var sortedList = chart1.Series[0].Points.AsEnumerable()
                    .Select(x => new { ranNumber = rand.Next(), pnt = x })
                    .OrderBy(x => x.ranNumber).ToList();
              
                //get distancde between first and second point
                double xDistance = (double)sortedList[0].pnt.XValue - (double)sortedList[1].pnt.XValue;
                double yDistance = (double)sortedList[0].pnt.YValues[0] - (double)sortedList[1].pnt.YValues[0];
                double distance = Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance)); 
    
    
            }


    jdweng

    • Marked as answer by Caillen Wednesday, April 8, 2015 7:06 AM
    Tuesday, March 31, 2015 7:03 AM

All replies

  • To make it more clear, i put the screenshot here. I want to choose one random point from the blue point , and calculate the distance between the random point to the black or red point.. help me. 

    thanks

    i cannot copy the photo. heres the link to the photo ----  https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/11054299_811876005567689_3794441462666954824_n.jpg?oh=7c9576e4f1f9b2a7e2ac9cf744383051&oe=55AC5BD5&__gda__=1438193773_becbb041c6c51cfc4db4b38c2d18e7a2

    Tuesday, March 31, 2015 5:18 AM
  • You can calculate the distance between two points by adding the square of the difference in x values to the square of the difference in y values,  then taking the square root .

    • Edited by Ante Meridian Tuesday, March 31, 2015 5:21 AM typo.
    • Marked as answer by Caillen Wednesday, April 8, 2015 7:06 AM
    Tuesday, March 31, 2015 5:21 AM
  • When getting random items in a list you don't want to pick the same item twice.  So normally you assign a random number to every item in the list and then sort by the random number.  Your two random point will be the first and second item in the sort list.  Try the code below. sortedList is a N x 2 array where one column is the points and the 2nd column is the points.

           private void button1_Click(object sender, EventArgs e)
            {
                Random rand = new Random();
                var sortedList = chart1.Series[0].Points.AsEnumerable()
                    .Select(x => new { ranNumber = rand.Next(), pnt = x })
                    .OrderBy(x => x.ranNumber).ToList();
              
                //get distancde between first and second point
                double xDistance = (double)sortedList[0].pnt.XValue - (double)sortedList[1].pnt.XValue;
                double yDistance = (double)sortedList[0].pnt.YValues[0] - (double)sortedList[1].pnt.YValues[0];
                double distance = Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance)); 
    
    
            }


    jdweng

    • Marked as answer by Caillen Wednesday, April 8, 2015 7:06 AM
    Tuesday, March 31, 2015 7:03 AM
  • Hi Umar,

    private Point CalculatePoint()
    {
        var angle = _random.NextDouble() * Math.PI * 2;
        var radius = _random.NextDouble() * _radius;
        var x = _originX + radius * Math.Cos(angle);
        var y = _originY + radius * Math.Sin(angle);
        return new Point((int)x,(int)y);
    }


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Tuesday, March 31, 2015 4:05 PM
  • Why would you want to calculate distance by using angle which is less accurate.  Use Pythagoras!!!

    A **2 + B**2 = C**2


    jdweng

    Tuesday, March 31, 2015 4:24 PM
  • can you provide screen shot
    Tuesday, March 31, 2015 5:47 PM
  • i cannot attach the photo here .. i dont know why. but here's the photo . check this link.. i uploaded it to fb.. 

    https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/11054299_811876005567689_3794441462666954824_n.jpg?oh=7c9576e4f1f9b2a7e2ac9cf744383051&oe=55AC5BD5&__gda__=1438193773_becbb041c6c51cfc4db4b38c2d18e7a2

    Wednesday, April 1, 2015 6:53 AM
  • Hi Umar,

    I prefer Joel's and Ante's answer, have you tried them?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 1, 2015 10:01 AM