Sign in to follow this  

equation in code

This topic is 4383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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) );
	}
}


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 4383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this