Jan 18, 2017 at 11:25pm UTC
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 Jan 18, 2017 at 11:25pm UTC
Jan 18, 2017 at 11:36pm UTC
What is a "right triangle"?
How do you get a tri angle from two points?
Jan 18, 2017 at 11:40pm UTC
object->setPosition(0,0);
second_object->setPosition(50,50);
std::cout << calculateDistance(object, second_object);
//prints 0
Last edited on Jan 18, 2017 at 11:40pm UTC
Jan 19, 2017 at 4:44am UTC
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 > = x1 x2 * y1 y2 = ||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 Jan 19, 2017 at 5:59am UTC