Jump to content
  • Advertisement
Sign in to follow this  
web_scripter

equation in code

This topic is 4593 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!