"Touches" Function

Hi there, I created a function for me to use in sfml that checks whether or not something is in the radius of another thing. Here is the function:
1
2
3
4
5
6
7
8
9
10
    float Dist(Vector2f a, Vector2f b) {
	float xDist = (b.x - a.x) * (b.x - a.x);
	float yDist = (b.y - a.y) * (b.y - a.y);
	float Dist = sqrtf(xDist + yDist);
	return Dist;
    }
    bool Touches(Vector2f a, float aR, Vector2f b, float bR) {
	float dist = Dist(a, b);
	return dist < aR + bR;
    }

Sorry for any basic mistakes I've made, I'm quite new to C++.
Do you see any errors in the functions, because when I use them in practice they don't work as expected.

In other words, is it a problem with some other code or with this code?
That looks okay to me.

As written, the code passes the Vector2f parameters by value, which means it makes a copy of them. If would be more efficient to pass them by constant reference:
1
2
3
4
5
6
7
8
9
10
    float Dist(const Vector2f &a, const Vector2f &b) {
	float xDist = (b.x - a.x) * (b.x - a.x);
	float yDist = (b.y - a.y) * (b.y - a.y);
	float Dist = sqrtf(xDist + yDist);
	return Dist;
    }
    bool Touches(const Vector2f &a, float aR, const Vector2f &b, float bR) {
	float dist = Dist(a, b);
	return dist < aR + bR;
    }


Also, if performance is an issue, you could compute the square of the distance in Touches and see if that's less than the square of the sum of the radii.
Alright, thanks dhayden, I'll keep looking for an error elsewhere in my code.
Topic archived. No new replies allowed.