# Trig question re line/circle intersections

**URL:** <https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671>\
**Category:** Coding Questions\
**Created:** [April 27, 2021, 3:42am UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671 "2021-04-27T03:42:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![psemme](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psemme/32/12721_2.png) [@psemme](https://discourse.processing.org/u/psemme)\
**Post date:** [April 27, 2021, 3:42am UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/1 "2021-04-27T03:42:11Z")

</div>

I never really grok’d trig in school but have enjoyed learning it now as I’m older. And I stumble onto things that I really feel should have simple solutions but I end up staring at them for hours because I don’t have the toolset. I understand how to calculate most of the pieces of this puzzle (e.g. the hypotenuse) but can’t figure out how to calculate the angle that would allow me to use sin, cos to acquire the x,y coordinates for the intercepts. Below is a diagram. I’m hoping this doesn’t have a simple answer that I will kick myself for not seeing…Thanks for the insights. This forum is/has been awesome.

 ![Screen Shot 2021-04-26 at 11.31.54 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f9eab926b441c643e9265f855434093d943e616d.png)

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 27, 2021, 1:29pm UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/2 "2021-04-27T13:29:38Z")

</div>

You may have something in mind but I’m afraid that this question in itself is not related to processing/p5.js. I don’t want to sound bitter or sour but I recommend to ask on other forums (and of course, if the topic is related to, e.g., visualization on processing/p5.js then we can discuss)

By the way I think you can solve it without trigonometry - Pythagoras’s theorem is enough to solve it 🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2021, 1:56pm UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/3 "2021-04-27T13:56:16Z")

</div>

@psemme,

A couple of ways… I a sure there are many more.

Consider:

> **[Intersection (geometry) | A line and a circle](https://en.wikipedia.org/wiki/Intersection_(Euclidean_geometry)#A_line_and_a_circle)**
>
> For the intersection of
> 1.line 
>   
>     
>       
> a
> x
> +
> b
> y
> =
> c
>       
>     
> {\\displaystyle ax+by=c}
>   
> and circle 
>   
>     
>       
>         
> x
>           
> 2
>           
>         
> +
>         
> y
>           
> 2
>           
>         
> =
>         
> r
>           
> 2
>           
>         
>       
>     
> {\\displaystyle x^{2}+y^{2}=r^{2}}
>   
> one solves the line equation for x or y ...

You could draw a circle and then a line and use get(x, y) to detect where it intersects.  
I have used this technique to detect intersections of objects with a curve and following a curve.

Emulate a rotary encoder:

> [@Best way to emulate a rotary encoder wheel?](https://discourse.processing.org/t/best-way-to-emulate-a-rotary-encoder-wheel/21982/6):
>
> Hello, Thanks for asking this! Had some fun with it… Sharing my initial steps… I did not look at other examples. Create a PGraphic image. I used random circles but this could be your encoded wheel or anything. Move the mouse over the canvas to detect colors. Do something with the detected colors. The rest is up to your creativity and imagination. I used the same concepts in the past to do something similar. In my example the mouse location turned red or green depending on color it detected…

`:)`

---

<div class="post-metadata">

**Author:** ![psemme](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psemme/32/12721_2.png) [@psemme](https://discourse.processing.org/u/psemme)\
**Post date:** [April 27, 2021, 5:49pm UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/4 "2021-04-27T17:49:14Z")

</div>

Thanks again @giv - appreciate the prompt response. I wasn’t aware of the get(x,y) function. I’ll check it out. Not sure what a rotary encoder wheel is but I love the name! Will dig into that too.  
Thanks.

---

<div class="post-metadata">

**Author:** ![psemme](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psemme/32/12721_2.png) [@psemme](https://discourse.processing.org/u/psemme)\
**Post date:** [April 27, 2021, 5:51pm UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/5 "2021-04-27T17:51:42Z")

</div>

Thanks @micuat - of course you’re right. It’s easy for me to conflate trig and Processing/p5.js because I’m not using trig for anything else. I will ask those questions elsewhere. Thx again.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 28, 2021, 7:17am UTC](https://discourse.processing.org/t/trig-question-re-line-circle-intersections/29671/6 "2021-04-28T07:17:23Z")

</div>

The following code calculates the intersection points. I have copied it from one of my libraries that uses a lot of 2D geometry. and it would be easy enoung to convert to JS

```auto
	/**
	 * Calculate the points of intersection between a line and a circle. <br>
	 * An array is returned that contains the intersection points in x, y order.
	 * If the array is of length: <br>
	 * 0 then there is no intersection <br>
	 * 2 there is just one intersection (the line is a tangent to the circle) <br>
	 * 4 there are two intersections <br>
	 * 
	 * @param x0 start of line
	 * @param y0 start of line
	 * @param x1 end of line
	 * @param y1 end of line
	 * @param cx centre of circle x position
	 * @param cy centre of circle y position
	 * @param r radius of circle
	 * @return the intersection points as an array (2 elements per intersection)
	 */
	public static double[] line_circle_p(double x0, double y0, double x1, double y1, double cx, double cy, double r){
		double[] result = NONE;
		double f = (x1 - x0);
		double g = (y1 - y0);
		double fSQ = f*f;
		double gSQ = g*g;
		double fgSQ = fSQ + gSQ;

		double xc0 = cx - x0;
		double yc0 = cy - y0;

		double fygx = f*yc0 - g*xc0;
		double root = r*r*fgSQ - fygx*fygx;
		if(root > -ACCY){
			double[] temp = null;
			int np = 0;
			double fxgy = f*xc0 + g*yc0;
			if(root < ACCY){ // tangent so just one point
				double t = fxgy / fgSQ;
				if(t >= 0 && t <= 1)
					temp = new double[] { x0 + f*t, y0 + g*t};
				np = 2;
			}
			else {	// possibly two intersections
				temp = new double[4];
				root = Math.sqrt(root);
				double t = (fxgy - root)/fgSQ;
				if(t >= 0 && t <= 1){
					temp[np++] = x0 + f*t;
					temp[np++] = y0 + g*t;
				}
				t = (fxgy + root)/fgSQ;
				if(t >= 0 && t <= 1){
					temp[np++] = x0 + f*t;
					temp[np++] = y0 + g*t;
				}
			}
			if(temp != null){
				result = new double[np];
				System.arraycopy(temp, 0, result, 0, np);
			}
		}
		return result;
	}

```
