Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Algorithm gives wrong answer, what's wrong?

  1. Feb 10, 2016 #1
    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 (C):

    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 (Text):
    float x = 0.75;
    float y = 0.25;
    translateToHypotenusePositiveSlope(x, y, 0, 0, 1, 1);
    std::cout << x << ", " << y << std::endl;
     
    Last edited: Feb 10, 2016
  2. jcsd
  3. Feb 10, 2016 #2

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    ##\tan (\theta)= \frac{h}{w}##, not ##\sin (\theta)= \frac{h}{w}##.
     
  4. Feb 10, 2016 #3
    Grr... I spent so much energy on the translations that I didn't think to look at how I got the angle.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Algorithm gives wrong answer, what's wrong?
Loading...