Find opposite point on a rectangle

Consider a rectangle $$ABCD$$, with the $$A$$ point in the $$(0,0)$$ position. Based on a point on the edge of the rectangle ($$E$$ in the picture) and a point inside the rectangle (multiple examples in the picture - $$F$$, $$G$$ and $$H$$) find the opposite point on the same rectangle (in my example $$I$$, $$J$$ and $$K$$).

This is how I solve it, using Slope–intercept form:

• if $$E$$ is on the bottom edge:
• calculate slope $$m$$ between $$EF$$, $$EG$$ or $$EH$$
• calculate $$y0$$
• calculate $$x$$ for $$y==height==b==d$$
• if $$x$$ is negative, then set $$x=0$$ and $$y=y0$$ ($$I$$ in my example)
• if $$0 then use x and y ($$J$$ in my example)
• if $$x>width$$ then set $$x=width$$ and calculate y ($$K$$ in my example)

I use similar logic for other three scenarios (when edge point is on left edge, top edge or right edge). I feel there should be an easier solution, which doesn't care on which edge the starting point is. My solution is very complex and error prone (additional exception are vertical lines where you cannot calculate $$slope$$). Any better ideas?

• I don't think there's a solution that avoids cases. If you want this to use in a computer program, your ugly method is probably OK. You can write tests for all the cases to find coding errors. You might need to worry about slopes that are near $0$ or very large to avoid floating point problems. The execution time will be very fast since there's really very little arithmetic. If it's called zillions of times then you can think about optimization. – Ethan Bolker yesterday

HINT:

Slant line has given variable slope $$m$$.

$$x-y/m= 2 \tag1$$

We can choose both horizontal /vertical lines of the square from its "equation"

$$x(4-x)y(4-y)=0 \tag2$$

Substitute for $$x$$ and $$y$$ separately from (1)

Out of the two cases eliminating $$x$$ we get

$$(2+y/m)(2-y/m)y(4-y) \tag3$$

a fourth degree polynomial in $$y$$. Two $$y$$ values are known as input and the the rest two solutions come out by solving a quadratic equation as:

$$(x,y)= (4\frac{2}{3},-3) \tag 4$$

Similarly eliminating $$y$$ is left as an exercise.

Choosing only one typical slope $$m=\frac32$$ (not given by you) for shown red transversal line we can note all intersections on the graph for any arbitrarily chosen $$m.$$

Hope you can now set up both the quadratic equations.

• can you please give an example? I don't fully understand your solution... For example, for the points (2,0) and (1,1) the expected point would be (0,2). What numbers should I use for 'slant line of slope m'? Which options from (2) or (3) should I use and what is the + operator when you say 'solve either (1) + (2)'? – sventevit Oct 20 at 16:52
• The slopes are is given in your problem, right? – Narasimham Oct 20 at 22:43
• No, I have one point on the edge, and one point in the middle, for example points E and F on the picture (or E and G, or E and H). – sventevit Oct 21 at 8:18
• Thats what... the slopes $m$ of lines through $(F,G,H)$ concurrent at $E$ can be found. – Narasimham Oct 21 at 8:23
• Sorry but my math is obviously very rusty... Can you help me with an example, let's say points (2,0) and (1,1), and the opposite point should be (0,2)? The slope is -1, so $(1)$ in your example is $y=-x+2$. Now what? I know that the opposite point should be on the $y$ axis (on the left edge of the rectangle), so I guess I have to take a vertical example from your example, but how? And what should $v$ be? And how would this look like when I don't know on which edge of the rectangle the opposite point is? – sventevit Oct 25 at 19:49