Simon
Simon

Reputation: 2246

Calculate a point offset along a relative diagonal

I have a diagonal line / plane (A, B), for which I have an offset point (C). I now need to move this point (C) along the same plane as the diagonal line, keeping the same offset distance, to a percentage of the line A-B.

enter image description here

This is probably quite "simple", however I can't seem to get my head around the maths involved. I have googled alot for this but have not found the answer which will get what I am needing.

Help to translate the "pure" maths formula into a code form would be greatly appreciated also as I am (obviously) not a maths oriented person.

Upvotes: 2

Views: 605

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533820

You can do

public static Point moveInDirection(Point a, Point b, Point c,  double ratio) {
    return new Point(
            (int) Math.round(c.x + (b.x - a.x) * ratio),
            (int) Math.round(c.y + (b.y - a.y) * ratio));
}

Upvotes: 1

Related Questions