Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a program in which many weird shapes are drawn onto a canvas. Right now i'm trying to implement the last, and possebly hardest, one.

In this particular shape i need a way to find the location (on a 2d canvas) where the line hits the shape. The following image is an example of what i have right now.


The black dots are the points that a known to me (i also have the location of the center of the three open circles and the radius of these circles). Each of the three outer lines needs a line towards the center dot, ending at the point that it hits the circle. This shape can be turned 90, 180 or 270 degrees.

The shape should look something like the following:


If you need any other information, please ask me in the comments. I'm not very good at math so please be gentle, thanks!

share|cite|improve this question
Is the center of each circle located on the line connecting the center black point with the respective side black point? Or is just coincidental on the picture? – Dennis Gulko Oct 12 '12 at 13:47
That doesn't have to be true (and in the picture that is only true for the right circle, the bottom and top circles have the line just right of the center if you look closely). – dragon112 Oct 15 '12 at 10:24
up vote 1 down vote accepted

The points on the line have the form $(x,y)=(a+(c-a)t,b+(d-b)t)$ with $0\le t\le 1$, where one end point is $(a,b)$, the other $(c,d)$. The points on the circle or radius $r$ around $(e,f)$ are given by $(x-e)^2+(y-f)^2=r^2$. Substituting th eline coordinates in the circle equation gives: $$ ((a-c)^2+(d-b)^2)\cdot t^2+2(a(c-a)+b(d-b))\cdot t+(a^2+b^2-r^2)=0$$ A quadratic equation in $t$. As such, it may have two real solutions or one or no real solutions. If there are no real solutions, the line does not pass through the circle. If there is only one real solution, the line is tangent. If there are two real solutions $t_1<t_2$, then the line is inside the circle for $t_1<t<t_2$ and outside otherwise. If you know that $(a,b)$ is inside the circle, you may only need $t_2$ (and $t_1<0$ automatically). If $t_2>1$, the compltete line segment is inside the circle, otherwise the line from $(a+(c-a)t_2,b+(d-b)t_2)$ to $(c,d)$ is the visible part of the line.

share|cite|improve this answer
Thanks! I changed this slightly to fit my code but you got the general idea. – dragon112 Oct 15 '12 at 10:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.