Algorithm gives wrong answer, what's wrong?

  • Thread starter Thread starter newjerseyrunner
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread Summary
The discussion centers around a mathematical problem involving the translation of a point inside a triangle to the hypotenuse, specifically in the direction normal to it. The triangle is defined with a tip at (l, b) and sides of lengths w and h, leading to the angle θ calculated as asin(h/w). The user defines variables for the point's position and calculates intermediary values to determine the translation amounts. The provided code aims to perform this translation, but the user encounters an issue where the point does not translate correctly from (0.75, 0.25) to the expected (0.5, 0.5). The user suspects either the code or the underlying algorithm may be incorrect. A key realization is made that the angle should be derived using the tangent function rather than the sine function, which is crucial for accurate calculations. This oversight in determining the angle significantly impacts the translation results.
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 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

2
Replies
83
Views
21K
Back
Top