How to determine if 2 points in 2D space make right triangle?

So I'm making calculateDistance function and I don't know how to determine if two points of an object make an right triangle.
Here is my code:
1
2
3
4
5
6
7
8
9
	float ObjectManager::calculateDistance(ls::Object* object, ls::Object* second_object) {

		float a = (object->getPosition()->x - second_object->getPosition()->x) * (object->getPosition()->x - second_object->getPosition()->x)
			, b = (object->getPosition()->y - second_object->getPosition()->y) * (object->getPosition()->y - second_object->getPosition()->y), c = a - b;

		if (c < 0) c *= -1;

		return sqrt(c);
	}
Last edited on
What is a "right triangle"?

How do you get a triangle from two points?
object->setPosition(0,0);
second_object->setPosition(50,50);

std::cout << calculateDistance(object, second_object);
//prints 0
Last edited on
Assuming the third point is the origin -- take the inner product of the two vectors. The two vectors are orthogonal if and only if their inner product is zero.

Let
u = <x1, y1>, v = <x2, y2>:
<x1, y1> * <x2, y2> = x1x2 * y1y2 = ||u|| ||v|| cos(t) = 0 |(t = pi / 2)

Orthogonality (or "making a right triangle") is not a requirement to compute distance between two points. Points cannot be orthogonal.
To get the distance:
1
2
3
4
const auto x1 = object->getPosition()->x, x2 = second_object->getPosition()->x;
const auto y1 = object->getPosition()->y, y2 = second_object->getPosition()->y;
const auto delta_x = x2 - x1, delta_y = y2 - y1;
return std::sqrt(delta_x * delta_x + delta_y * delta_y);
Last edited on
Topic archived. No new replies allowed.