2

Possible Duplicate:
Circle line collision detection

I have a problem. I need to find point A. enter image description here

How can I do this best way?

Programming languale is Java.

Community
  • 1
  • 1
Val
  • 4,225
  • 8
  • 36
  • 55
  • 2
    Programming language doesn't matter. This is basic maths. – Robin Oct 21 '12 at 20:30
  • 1
    This is not a java-specific question, it is pure math. – Bernd Elkemann Oct 21 '12 at 20:30
  • Yes, but if anyone has a solution to this programming language, it can share it without any additional explanations. – Val Oct 21 '12 at 20:31
  • Is this `do my homework` question? – Pshemo Oct 21 '12 at 20:32
  • No, it's my 'I want to finish my project' question. – Val Oct 21 '12 at 20:33
  • why do you think that this is a mathematical problem? Perhaps, there is a method for determining the point of intersection of the line and circle, or another way to learn this point with the help of a programming language. – Val Oct 21 '12 at 20:38
  • @Max I think this is a more specific case, i.e. where the line starts at the centre of the circle - so the correct solution is potentially much simpler. – DNA Oct 21 '12 at 21:59

1 Answers1

4

Given: a circle with center C=[x2,y2] and radius R a line segment from C to B=[x1,y2] calculate their intersection.

This is easy since one of the endpoints is the center of the circle. You have to walk the distance of R from C towards B. The distance guarantees you will end up on the circle and the direction guarantees you will end up on the ray C->B. You still need to check if the intersection lies on the segment.

Here is the pseudocode if you have a vector library

- offset = B-C
- if length_square(offset) < R^2 output "no intersection"
- offset_a = normalize(B-C) * R
- output C + offset_a

Without a library, the code is longer:

- off_x = x1-x2;
- off_y = y1-y2;

- ls = off_x*off_x + off_y*off_y
- if ls < R*R
-- return an empty array, meaning "no intersections"

- scale = R / sqrt(ls)
- res_x = off_x * scale + x2
- res_y = off_y * scale + y2
- return [[off_x, off_y]]
John Dvorak
  • 26,799
  • 13
  • 69
  • 83