Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Absolute Value In C (Rectilinear Distance)


4 replies to this topic

#1 mattaltepeter

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 05 September 2010 - 02:47 PM

I am working on a C program to calculate the shortest distance and the rectilinear distance between two points. However, when the program tries to calculate absolute value it doesn't do it correctly every time. any and all help is greatly appreciated

#include<stdio.h>
#include<stdlib.h>

int main()
{
  int x1; //variable to hold the x-coordinate of the first point
  int x2; //variable to hold the x-coordinate of the second point
  int y1; //variable to hold the y-coordinate of the first point
  int y2; //variable to hold the y-coordinate of the second point
  float short_distance; //variable to hold the shortest distance
  int rectil; //variable to hold the rectilinear distance
  

  printf("Enter the first point: ");
  scanf("%d", &x1);
  scanf("%d", &y1);
  printf("\nEnter the second point: ");
  scanf("%d", &x2);
  scanf("%d", &y2);

  short_distance = sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
  printf("\nShortest distance between points: %.2f\n",short_distance);

  rectil = abs(x1-y1) + abs(x2-y2);
  printf("Rectilinear distance between points: %d\n", rectil);

  return(0);
}

Edited by rvalkass, 05 September 2010 - 03:11 PM.


#2 rvalkass

    apt-get moo

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3,107 posts
  • Gender:Male
  • Location:Devon, England
  • Interests:At the moment, Physics mainly!
  • myCENT:69.42
  • Spam Patrol

Posted 05 September 2010 - 03:14 PM

I believe your formula for the rectilinear distance is incorrect. Rather than just give you the answer, see if you can work out what is wrong. Come back with your debugging attempts if you can't work it out, and I'll give you some more advice. Debugging is a valuable skill, and one you won't learn with people giving you the answers. Harsh but what I believe to be important :P

#3 mattaltepeter

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 05 September 2010 - 03:40 PM

okay so i messed around with it a little more. is it: |x1-x2|+|y1-y2|?

#4 mattaltepeter

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 05 September 2010 - 03:43 PM

Okay my program is working correctly now. thanks for your advice

#5 Quatrux

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 605 posts
  • Gender:Male
  • Location:Lithuania, Vilnius
  • Interests:Programming
  • myCENT:49.19

Posted 06 September 2010 - 09:19 AM

I suggest you to read into a* star algorithm, which is using your formula to calculate the path, it should go.. Your formula only gets the shortest distance, but what if there are walls?

You can read about it here:

http://en.wikipedia....earch_algorithm

I used this tutorial, to create some pathfinding programs:

http://www.policyalm...tarTutorial.htm




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users