0
\$\begingroup\$

I have two points that make a line. And I need to subtract a specific amount to each point keeping the direction of the line. I know the radius which is the amount to subtract.

Picture

The red dots of the first line is what I have, and the dots of the second line is an example of what I would like to calc.

\$\endgroup\$
1
\$\begingroup\$

This should work:

For each point of the line:

point = point + ((otherPoint - point).normalise() * radiusToSubtract)

Explanation:

First we get the direction from the point we're moving to the other point

(otherPoint - point).normalise()

Then we multiply that unit vector by the amount you want to budge the point toward the other point

* radiusToSubtract)

Lastly we add that to the point we're moving

point + ((otherPoint - point).normalise() * radiusToSubtract)

\$\endgroup\$
  • \$\begingroup\$ It works perfectly translating your formulas to Three.js thank you. \$\endgroup\$ – EnZo Mar 2 at 23:25
  • 1
    \$\begingroup\$ Great to hear! Glad I could help. \$\endgroup\$ – ImperialCoder Mar 2 at 23:30
1
\$\begingroup\$

Imperial's answer works fine if you are in a game engine that has the normalise() function, but if you are doing something a bit more homebrew, the actual math works like this:

Since both points are an object with a known X, Y, and radius value, you can use your basic slope formula tells us Slope=(Y2-Y1)/(X2-X1)

We also know that both objects will follow this slope over the distance of their radius at which point you are left with something like this to find your X: https://math.stackexchange.com/questions/566029/in-a-right-triangle-given-slope-and-length-of-hypotenuse-find-length-of-legs

With two known legs on a right triangle, the Y is just a simple pythagorean function equation.

Lastly, you need to check the directions of your rise and run because all that Squaring only produces positive values.

So what this would look like in a language agnostic since is something like this:

getCloser(target){
    rise = target.y-this.y;
    run = target.x-this.x;
    slope = rise/run;
    movX = this.radius/sqrt(slope^2+1);
    movY = sqrt(this.radius^2-movX^2);

    if (run > 0){
        this.x += movX;
    } else {
        this.x -= movX;
    }

    if (rise > 0){
        this.y += movY;
    }else{
        this.y -= movY;
    }
}

then

point1.getCloser(point2);
point2.getCloser(point1);
\$\endgroup\$
  • \$\begingroup\$ Yes, I'm using Three.js and have all the functions I need to work with vectors. But thank you for the deep/raw explanation. \$\endgroup\$ – EnZo Mar 2 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.