Trig question re line/circle intersections

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.

1 Like

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 :slight_smile:

@psemme,

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

Consider:

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:

:)

1 Like

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.

2 Likes

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.

1 Like

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

	/**
	 * 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;
	}