Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to get a point in a diagonal segment

Look at this image(sorry, I just have paint brush at my works' machine):

enter image description here

There you see two black rectangles, one major and one minor, inside the major. Consider they are divs. The red dots are the center point of each one(top and left).

The green rectangle is a psychical(not draw on screen) boundary that symmetricly embraces the major reactangle, and it's calculated by JavaScript.

The blue line is the diagonal line segment of those two red dots.

I already have all positions of the above objects calculated on JavaScript.

The yellow dot is the point I want to get. It have to be on the diagonal line of the red dots but at the boundary(green line). It can't be out or inside it.

I have created this jsFiddle with my algorithm. As you can see that the yellow point is outside the green boundaries. It have to be in the diagonal segment and over the green line limitation.

Any help with this algorithm?

like image 223
DontVoteMeDown Avatar asked Jul 12 '26 10:07

DontVoteMeDown


1 Answers

More about slope: https://www.khanacademy.org/math/algebra/linear-equations-and-inequalitie/slope-and-intercepts/v/slope-of-a-line

You can use the point-slope formula to find points on a line(in order to find the one that will intersect the green top line):

(x1, y1) - one of the red points
(x2, y2) - another of the red points
(x, y) - the green point
slope = (y2 - y1) / (x2 - x1) 
y - y1 = slope * (x - x1)

EDIT: Thanks to Imre Kerr:

Now, you already have either the point's y(if your line intersects the top green line), or the point's x(if it intersects the left green line). From this, you could find the other coordinate. A sample that assumes you're intersecting the top line (http://jsfiddle.net/BGSacho/TDH7q/6/).

like image 146
Sacho Avatar answered Jul 14 '26 23:07

Sacho