Jump to content
xisto Community
mattaltepeter

Absolute Value In C (Rectilinear Distance)

Recommended Posts

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 (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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://forums.xisto.com/no_longer_exists/ used this tutorial, to create some pathfinding programs:http://www.policyalmanac.org/games/aStarTutorial.htm

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.