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\$

2 Answers 2

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\$
2
  • \$\begingroup\$ It works perfectly translating your formulas to Three.js thank you. \$\endgroup\$
    – Enzo
    Mar 2, 2019 at 23:25
  • 1
    \$\begingroup\$ Great to hear! Glad I could help. \$\endgroup\$
    – georgeous
    Mar 2, 2019 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\$
1
  • \$\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, 2019 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .