# equation in code

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

## 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 on other sites
Essentially you want the Tangent to a circle at Point P{x,y}?

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

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]

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009199
• ### Who's Online (See full list)

There are no registered users currently online

×