Thread: A way to store the computation of the distance formula into a matrix?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    5

    A way to store the computation of the distance formula into a matrix?

    Hi,

    So I working on a search algorithm, however, we were asked to compute the distance between the nodes(points(x,y)), and once the distance is computed, then store it into a matrix, which then we perform a look up and let the search algorithm know which distance is closer to the goal node, and hence it will follow that route, and adding the distances.

    Here's my question:
    I been looking all over to find an specific examples, but did not find any.
    The problem is that my points are read from a input file(.txt), so while I am able to get it to read the input files and get the x's and y's, I am still lost on how can I compute their distances of each two points and then storing it to a matrix.

    This what I have so far:

    Code:
    ...
     int main() {
    
        int x, y;
        float distance;
        Graph g(4); //Create 4 vertices.
        ifstream read("test.txt");
        while(read>>x>>y) {
           //distance = sqrt(pow(x, 2) + pow(y, 2));
        }   
     
      //cout << distance << "" << endl;
    
      return 0;
    }
    Note that I the distance is probably wrong since there are different points, but am not sure how to get around it .
    Any example links, or explanations would be HELPFUL!!
    It been driving me nuts.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You're calculating the distance between the coordinates of a points.

    Between two points, it's
    distance = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2));

    Where say
    x1 = g[0].x
    y1 = g[0].y
    x2 = g[1].x
    y2 = g[1].y
    etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    5
    oh I see. So if my test.txt has 100-1000 points, do I need to make more x3 = g[2].x....x100 = g[99].x?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I guess if you have 100 points, then you need say
    float distance[100][100];

    Assuming that distance between two points is symmetrical, then this will be a matrix reflected in the diagonal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. distance formula help
    By Arex Bawrin in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 03:38 PM
  2. Matrix Formula
    By Junior89 in forum C++ Programming
    Replies: 15
    Last Post: 12-19-2004, 09:18 PM
  3. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM
  4. Distance Formula Implecations: Urgent!
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 03-20-2004, 10:52 PM

Tags for this Thread