Algorithm gives wrong answer, what's wrong?

  • Thread starter Thread starter newjerseyrunner
  • Start date Start date
  • Tags Tags
    Algorithm
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
newjerseyrunner
Messages
1,532
Reaction score
637
I have a point inside of a triangle and I want to move it to a position on the hypotenuse in the direction normal to it.

IMG_0751.JPG


The triangle's tip is located at position (l, b), the original point inside of it is located at (x, y).
The triangle's sides have lengths w and h.
That makes the measure of the angle (θ) to be asin(h / w).
dy is defined as y - b, dx is defined as x - l.
Ix being defined as dy times the ratio of width and height (w/h).

Now I have a secondary right triangle inside, in the diagram it,s defined by sides K and Jx.
I determine the value of Jx by taking dx and removing Ix.
Then find the value of K by taking the sinf(θ) * Jx.

Now for the final triangle to determine the translation amounts I take the K value that I just determined and multiply it times the sin and cosine of θ for the Δx and Δy. I know that the angle is θ because the one next to it with side K is complementary to it.

I then translate x and y by their deltas.

Code:
static void translateToHypotenusePositiveSlope(float & point_x, float & point_y, float triange_left, float triange_bottom, float triange_width, float triange_height){
    const float dx = point_x - triange_left;
    const float dy = point_y - triange_bottom;
    const float ix = dy * triange_width / triange_height;
    const float jx = dx - ix;
    const float theta = asinf(triange_height / triange_width);
    const float sinTheta = sinf(theta);
    const float k = sinTheta * jx;
    const float deltaX = sinTheta * k;
    const float deltaY = cosf(theta) * k;
    point_x -= deltaX;
    point_y += deltaY;
}

Either the code is wrong or the algorithm is, I doubt it's the code so I'm asking here for a double check of the math.I have an example triangle that's 1x1 (so a 45 degree slope on the hypotenuse) and placing the point at (.75, ,25) so it should translate to (0.5, 0.5,) but it doesn't. It translates to (.25, .25)
Code:
float x = 0.75;
float y = 0.25;
translateToHypotenusePositiveSlope(x, y, 0, 0, 1, 1);
std::cout << x << ", " << y << std::endl;
 
Last edited:
on Phys.org
newjerseyrunner said:
I have a point inside of a triangle and I want to move it to a position on the hypotenuse in the direction normal to it.

View attachment 95631

The triangle's tip is located at position (l, b), the original point inside of it is located at (x, y).
The triangle's sides have lengths w and h.
That makes the measure of the angle (θ) to be asin(h / w).
dy is defined as y - b, dx is defined as x - l.
Ix being defined as dy times the ratio of width and height (w/h).

Now I have a secondary right triangle inside, in the diagram it,s defined by sides K and Jx.
I determine the value of Jx by taking dx and removing Ix.
Then find the value of K by taking the sinf(θ) * Jx.

Now for the final triangle to determine the translation amounts I take the K value that I just determined and multiply it times the sin and cosine of θ for the Δx and Δy. I know that the angle is θ because the one next to it with side K is complementary to it.

I then translate x and y by their deltas.

Code:
static void translateToHypotenusePositiveSlope(float & point_x, float & point_y, float triange_left, float triange_bottom, float triange_width, float triange_height){
    const float dx = point_x - triange_left;
    const float dy = point_y - triange_bottom;
    const float ix = dy * triange_width / triange_height;
    const float jx = dx - ix;
    const float theta = asinf(triange_height / triange_width);
    const float sinTheta = sinf(theta);
    const float k = sinTheta * jx;
    const float deltaX = sinTheta * k;
    const float deltaY = cosf(theta) * k;
    point_x -= deltaX;
    point_y += deltaY;
}

Either the code is wrong or the algorithm is, I doubt it's the code so I'm asking here for a double check of the math.I have an example triangle that's 1x1 (so a 45 degree slope on the hypotenuse) and placing the point at (.75, ,25) so it should translate to (0.5, 0.5,) but it doesn't. It translates to (.25, .25)
Code:
float x = 0.75;
float y = 0.25;
translateToHypotenusePositiveSlope(x, y, 0, 0, 1, 1);
std::cout << x << ", " << y << std::endl;
##\tan (\theta)= \frac{h}{w}##, not ##\sin (\theta)= \frac{h}{w}##.
 
  • Like
Likes   Reactions: berkeman and newjerseyrunner
Grr... I spent so much energy on the translations that I didn't think to look at how I got the angle.