Algorithm gives wrong answer, what's wrong?

  • Thread starter Thread starter newjerseyrunner
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
SUMMARY

The discussion centers on a mathematical algorithm for translating a point inside a triangle to its hypotenuse. The algorithm uses trigonometric functions to determine the translation amounts based on the triangle's dimensions and the point's position. A critical error was identified in the calculation of the angle θ, where the tangent function should have been used instead of the sine function. This mistake led to incorrect translations, as demonstrated with a specific example using a 1x1 triangle.

PREREQUISITES
  • Understanding of trigonometric functions, specifically sine and tangent.
  • Familiarity with coordinate geometry and triangle properties.
  • Basic knowledge of C++ programming and function syntax.
  • Ability to debug mathematical algorithms and code logic.
NEXT STEPS
  • Review the correct application of trigonometric functions in geometry.
  • Learn about debugging techniques for mathematical algorithms in C++.
  • Study the properties of right triangles and their applications in programming.
  • Explore advanced topics in computational geometry for more complex translations.
USEFUL FOR

Mathematicians, software developers, and game developers who require precise point translations within geometric shapes, particularly in graphics programming and physics simulations.

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:
Technology news 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 83 ·
3
Replies
83
Views
22K
  • · Replies 1 ·
Replies
1
Views
10K