Finding Two Touching circles with limited information - vector

I am working on a track editor and have found myself in a situation where I need to define two touching circles. Ideally I would like to know the centre point, and radius of these circles.
The information I have is a point on the circumference of each of the circles, and the tangent to the circle at that point.
On my own I have figured out that if I know the tangents at those points I know the lines on which the centre points must lie.
C1 is the centre of the first circle I am looking for
P1 is a point on the circumference of the circle at which I know the tangent
A is the normal to the tangent that I know at P1
C2 is the centre of the second circle I am looking for
P2 is a point on the circumference of the circle at which I know the tangent
B is the normal to the tangent that I know at P2
C1 = P1 - t1 * A
C2 = P2 - t2 * B
I also know that the distance between the two centres will be equal to the sum of the distance of the centres from the points on the circumference.
|C1 - P1| + |C2 - P2| = |C1 - C2|
I also want abs( |t1*A| - |t2*B| ) to be kept to a minimum.

If you have only the tangent vectors and the point, there's not enough information. You need at least 2 more points, one more for each circle, otherwise t1 and t2 can be any real. (By the way, A = -B)
Disregard, I assumed that circles are not overlapping. Either way, we cannot know how large they are (and hence where the center points are) without knowing more information.

Related

Creating a graph from a 2-d space populated with circles

I'm populating a 2-D space with circles of random position and radius.
I also want to create a graph where every circle is a vertex and has edges to other intersecting circles.
My question is: Is there an efficient way to create this kind of graph?
Obviously theres the brute force method of just checking each circle. I also figured that I can maybe overlay a grid on the 2-D plane and sort-of hash the circles so I can easily find circles in a specific region.
Here's an example of what I am working with:
The example image suggests you might have an upper bound R on the radii of the circles which is considerably smaller than the total edge length of your rectangle. If that is the case, you might want to try the followung:
Sort all circles by x coordinate of center
Maintain a list L of circles, sorted by y coordinate of center, initially empty
For each circle C1 in order of x coordinates:
Drop all elements C2 from L where x2 < x1 - 2*R (or x2 < x1 - r2 - R)
For each circle C2 in L where |y1 - y2| < r1 + R:
Check C1, C2 for intersection, possibly add to result
Add C1 to L, maintaining order by y coordinates
L could be a red-black tree or something similar where executing a range query (namely y1 - r1 - R < y2 < y1 + r1 + R) is simple. But to efficiently remove elements when they fall out of range, you may need a second structure, most likely a stack (with the loose - 2*R limit) or a priority queue (with the tighter - r2 - R limit).
If R is close to the typical size of your circles, this should work well. If not, a quad tree might be better, where you'd only visit adjacent cells if you actually encounter a big circle, instead of wasting work just because there might be a biger circle yet to come.

Tangent circle(s) for two other circles?

There are two circles: a centered at point A, and circle b (center at B). What is the equation to calculate 2D position of all or none tangent circles possible. Main constraint is, that radius is the same for all the circles. As far as I know, there should be either no solution (figure 2), or 2 solutions (figure 1). How to find out if there are solutions, and also position of centers of those solutions (C and D).
Figure 1: 2 solutions should be possible here
Figure 2: No solutions!
Update (solution):
1) Calculate distance from A to B -> |AB|:
2) Checks whether a solution exist, it exist only if:
3) If it exist, calculate half-point between points A and B:
4) Create normalized perpendicular vector to line segment AB:
5) Calculate distance from this H point to C point -> |HC|:
6) Finally calculate point C along the (HC) starting at X at distance |HC|:
I suppose this question should migrate to a more math related site.
Try to imagine where these two tangent circles go when the circles a and b get further and further apart. They get closer to the line AB. Once the AB segment equals 4r these two tangent circles will overlap. From now on, once circles a and b get further apart, there's no tangent circles whatsoever.
If you want to calculate the position of these circles, just assume that the distance between the centers is always 2r:
You should get two, one or none solutions for xC and yC, which will be the centers of your tangent circles. I hope I haven't messed something up.
Solutions
Provided you do know there are solutions ( just check if d(A,B) <= 4r ), these are the coordinates of your two circles:
http://pastebin.com/LeW7Ws98
A little scary, eh? But it's working. There are the following variables:
x_A, y_A - the coordinates of the circle A,
x_B, y_B - the coordinates of the circle B,
r - the radius.
I've checked the solutions with the values from one of my comments below. I think that you can copy these solutions and inject them into your code straight away (provided there's a sqrt function) and get the results after declaring some variables.
These solutions are loosely derived from the Save's proposition but I couldn't comment below his answer - I've got less than 50 reputation points, duh ... ( thanks SO! You're the man! ). However I'm pretty sure they should be valid for my system anyways. Cheers
A solution exists iff d(A,B) = sqrt(2)*2*r
To find the center of the solution circles, that will let you draw the circonferences, you can intersect the circle with center (x_m,y_m), that is the medium point of the segment AB, of radius sqrt(2)*r, with the line perpendicular to AB and passing from (x_m,y_m)
This should give you all the needed information to check if a solution exixsts, and if it does, to draw it.

Sorting a List of 3d coplanar points to be clockwise or counterclockwise

I have a list of 3D points. I know they are all coplanar. I have the center that I want to sort them around and the normal to the plane on which the points and the center lie. How can I test if one point is right (or left) of another point?
I understand how to do it in 2D. Sort points in clockwise order? explains how to compare 2d points. So I think I need to somehow covert all the points and the center to local 2d plane coordinates. How can I do that? Is it the most efficient way to solve this problem?
//from link:
// a and b are points
//center is the center around which to determine order
//int num = (a.x-center.x) * (b.y-center.y) - (b.x - center.x) * (a.y - center.y);
//if num=0 then they're on the same line
//if num <0 or num>0 then a is to the left or right of b
How would I adapt this to handle 3d coplanar points?
There's no need to convert everything to 2D.
You have the center C and the normal n. To determine whether point B is clockwise or counterclockwise from point A, calculate dot(n, cross(A-C, B-C)). If the result is positive, B is counterclockwise from A; if it's negative, B is clockwise from A.

Translation coordinates for a circle under a certain angle

I have 2 circles that collide in a certain collision point and under a certain collision angle which I calculate using this formula :
C1(x1,y1) C2(x2,y2)
and the angle between the line uniting their centre and the x axis is
X = arctg (|y2 - y1| / |x2 - x1|)
and what I want is to translate the circle on top under the same angle that collided with the other circle. I mean with the angle X and I don't know what translation coordinates should I give for a proper and a straight translation!
For what I think you mean, here's how to do it cleanly.
Think in vectors.
Suppose the centre of the bottom circle has coordinates (x1,y1), and the centre of the top circle has coordinates (x2,y2). Then define two vectors
support = (x1,y1)
direction = (x2,y2) - (x1,y1)
now, the line between the two centres is fully described by the parametric representation
line = support + k*direction
with k any value in (-inf,+inf). At the initial time, substituting k=1 in the equation above indeed give the coordinates of the top circle. On some later time t, the value of k will have increased, and substituting that new value of k in the equation will give the new coordinates of the centre of the top circle.
How much k increases at value t is equal to the speed of the circle, and I leave that entirely up to you :)
Doing it this way, you never need to mess around with any angles and/or coordinate transformations etc. It even works in 3D (provided you add in z-coordinates everywhere).

width of a frustum at a given distance from the near plane

I'm using CML to manage the 3D math in an OpenGL-based interface project I'm making for work. I need to know the width of the viewing frustum at a given distance from the eye point, which is kept as a part of a 4x4 matrix that represents the camera. My goal is to position gui objects along the apparent edge of the viewport, but at some distance into the screen from the near clipping plane.
CML has a function to extract the planes of the frustum, giving them back in Ax + By + Cz + D = 0 form. This frustum is perpendicular to the camera, which isn't necessarily aligned with the z axis of the perspective projection.
I'd like to extract x and z coordinates so as to pin graphical elements to the sides of the screen at different distances from the camera. What is the best way to go about doing it?
Thanks!
This seems to be a duplicate of Finding side length of a cross-section of a pyramid frustum/truncated pyramid, if you already have a cross-section of known width a known distance from the apex. If you don't have that and you want to derive the answer yourself you can follow these steps.
Take two adjacent planes and find
their line of intersection L1. You
can use the steps here. Really
what you need is the direction
vector of the line.
Take two more planes, one the same
as in the previous step, and find
their line of intersection L2.
Note that all planes of the form Ax + By + Cz + D = 0 go through the origin, so you know that L1 and L2
intersect.
Draw yourself a picture of the
direction vectors for L1 and L2,
tails at the origin. These form an
angle; call it theta. Find theta
using the formula for the angle
between two vectors, e.g. here.
Draw a bisector of that angle. Draw
a perpendicular to the bisector at
the distance d you want from the
origin (this creates an isosceles
triangle, bisected into two
congruent right triangles). The
length of the perpendicular is your
desired frustum width w. Note that w is
twice the length of one of the bases
of the right triangles.
Let r be the length of the
hypotenuses of the right triangles.
Then rcos(theta/2)=d and
rsin(theta/2)=w/2, so
tan(theta/2)=(w/2)/d which implies
w=2d*tan(theta/2). Since you know d
and theta, you are done.
Note that we have found the length of one side of a cross-section of a frustrum. This will work with any perpendicular cross-section of any frustum. This can be extended to adapt it to a non-perpendicular cross-section.

Resources