equation in code

Started by
1 comment, last by nilkn 18 years, 4 months ago
Is the equation the same as the code shown? x1 and y1 are the location of the circle. x2 and y2 are the coordinates produced by the equation Equation: http://mathworld.wolfram.com/CircleTangentLine.html

int xs = x1*x1;
int ys = y1*y1;
int rs = r*r;
double t = Math.Acos( ((-r*x1) + (y1*Math.Sqrt( xs + ys - rs ))) / (xs + ys) );
double t2 = -Math.Acos( ((-r*x1) + (y1*Math.Sqrt( xs + ys - rs ))) / (xs + ys) );
double t3 = -Math.Acos( ((-r*x1) - (y1*Math.Sqrt( xs + ys - rs ))) / (xs + ys) );
double t4 = Math.Acos( ((-r*x1) - (y1*Math.Sqrt( xs + ys - rs ))) / (xs + ys) );
			

if( x1 < 0 )
{
	if( y1 > 0 )
	{
		x2 = (float)( x1 + 45*Math.Cos(t3) );
		y2 = (float)( y1 + 45*Math.Sin(t3) );
	}
	else
	{
		x2 = (float)( x1 + 45*Math.Cos(t) );
		y2 = (float)( y1 + 45*Math.Sin(t) );
	}
}
else if( x1 >= 0 )
{
	if( y1 > 0 )
	{
		x2 = (float)( x1 + 45*Math.Cos(t) );
		y2 = (float)( y1 + 45*Math.Sin(t) );
	}
	else
	{
		x2 = (float)( x1 + 45*Math.Cos(t) );
		y2 = (float)( y1 + 45*Math.Sin(t) );
	}
}


Advertisement
Essentially you want the Tangent to a circle at Point P{x,y}?
----------------------------

http://djoubert.co.uk
You don't need all this trickery to get the tangent to a circle.

If you situate your circle on the origin, the normal to the circle at a point (x,y) along the circle's circumference is the vector (x,y)/||(x,y)|| or simply (x,y) if this is the unit circle.

This describes a line segment which we may parameterize as thus:

n(t) = t(x,y).

Hence, the line normal to n(t) is given by

t(t) = t(-y,x).

This means the vector describing the direction of the tangent is (-x,y).

You may position this vector onto the circle properly by adding to it the offset (x,y). Thus, the parameterized line describing the tanget to the point (x,y) on a circle is

t(t) = (x,y) + t(-y,x).

Here is a graph demonstrating this:

Free Image Hosting at www.ImageShack.us

The blue circle is given by the parameterization

x = cos(t)
y = sin(t)

and the green line is the tangent to the point at (1, 45 degrees) in polar coordinates on the circle, or (1/sqrt(2), 1/sqrt(2)) in Cartesian coordinates, and is parameterized by

x = 1/sqrt(2) - t*(1/sqrt(2))
y = 1/sqrt(2) + t*(1/sqrt(2))

according to the above equation.

You may find the tangent to an ellipse similarly by mapping the ellipse into a new vector space in which it becomes a circle, and then proceeding with the math above.

[Edited by - nilkn on December 19, 2005 9:39:39 AM]

This topic is closed to new replies.

Advertisement