logic question

Mar 13, 2009 at 12:30am
Im helping in a group project and I just wanted to know if my code is logically correct. The reason I asked is when i compare it to other diameter calculators i get different answers .
here's the code:

double cal_distance(int& d_x1,int& d_x2,int& d_y1,int& d_y2)
{
double distance,tempx,tempy;
tempx = (d_x2 - d_x1);
tempy = (d_y2 - d_y1);
tempx = pow (tempx,2);
tempy = pow (tempy,2);
distance = tempx + tempy;
distance = sqrt(distance);

return distance ;
}
any help would be appreciated
thanks
btw, snytax wise its correct , and this is only part of the code(just the function I'm having some difficulties with.)
Mar 13, 2009 at 12:59am
It looks correct to me. That will calculate the distance between two points.
Mar 13, 2009 at 2:07am
You could do in fewer lines:
1
2
3
4
5
6
//References changed to straight ints, since there's no need to change their
//values.
double cal_distance(int d_x1,int d_x2,int d_y1,int d_y2)
{
	return sqrt(pow(d_x2 - d_x1,2)+pow(d_y2 - d_y1,2));
}

It can made even faster by changing the function to a macro.
Otherwise, it's fine.
Last edited on Mar 13, 2009 at 2:08am
Mar 13, 2009 at 4:17am
oh ok, thanks :D jdd & helios
Mar 13, 2009 at 1:12pm
just inline the function. macros are evil.
Topic archived. No new replies allowed.