Two points P and Q with coordinates (Px, Py) and (Qx, Qy) satisfy the following properties:
- The coordinates Px, Py, Qx, and Qy are integers.
- Px = −Qx.
- The line PQ is tangent to a circle with center (0, 0) and radius r
- 0 < Px ≤ a for some integer limit a.
How do I find all such pairs of points P and Q?
For example, in the image below I have a circle with radius r=2 and the limit a = 6. The pair of points P = (6, 2) and Q = (−6, −7) are a solution, because:
- The coordinates of P and Q are integers.
- Px = −Qx.
- The line PQ is tangent to the circle.
- 0 < Px ≤ 6.
But this is just one pair. I need to find all such pairs. There are a finite number of solutions.
So, is there a way to check if coordinates of points are tangent to the circle and are integers, then to list them all? I’ve looked at slope equations and shortest path from the center of the circle to the line equations, however, in the first case it requires coordinates to be known (which I could do by brute forcing every single digit, but I cannot see the pattern, because my guts tell me there should be some sort of equation I should apply), and in the second case I have to know the slope equation.
This is the algorithm I came up with but I don’t think it is correct or good enough:
- Find the slope equation y = mx + b for all 1 ≤ Px ≤ a and −a ≤ Qx ≤ −1.
- For every y = mx + b check if it is tangent to the circle (how to do that???)
- If true, return the pair
Line PQ has equation:
Distance from zero point to this line (circle radius) is
Note that radius and nominator are integers, so denominator must be integer too. It is possible when 2*Px and |Qy-Py| are members of some Pythagorean triple (for your example – 12^2+9^2 =15^2).So you can use “Generating a triple” method from the above link to significantly reduce the search and find all the possible point pairs (with radius checking)
With a=6 max value of n is 2, and your example corresponds to (k, m, n) set of (3, 2, 1)