Calculate circle packing - haskell

I need to mathematically calculate the position [x, y] and the radius where C ∈ N and C is a circle that can be positioned inside another circle with scalable radius and being traversed until reaching a leaf.
Being x the horizontal scale, y the vertical scale and r the radius, I need to infinetely position subcircles inside it, in a perfect geometric form. I've made some math proofs to calculate the density, but I'm not having good results: https://gist.github.com/haskellcamargo/89384ac17ba0131115c7
I define Circle as:
data Shape = Circle Double Double Double deriving (Show)
But I cannot find a deterministic way to prove the x and y position when a circle is inserted inside, with a perfect geometric form.
I found ways to calculate the density of a subcircle, but with special calcs according to the amount of subcircles, but the x and y are variant, with the unique warranty that the composed form where n > 3 will be composed by triangles. I know that I must work on angle, but I'm stuck on this in the last 2 weeks.
The question is: Can I use deterministic calculations to have the position x, y and the radius giving n, being n the number of elements? The final result would be like http://bl.ocks.org/mbostock/7607535, but with the absolute position, as calculated by D3.

Related

How to find if a 3d point is inside the same surface as a 3d triangle

Imagine that we have a triangle where each point is represented using 3 coordinates. How can we tell if a point, which is also represented using 3 coordinates, isn't inside the triangle, but is on the same surface as the triangle ?
Having triangle points A, B, C, we can build vectors
AB = B - A
AC = C - A
and normal vector N using cross product
N = AB x AC
To check whether point P belongs to ABC plane, calculate dot product
AP = P - A
dp = AP.dot.N
dp is equal to zero for points in the plane (use some tolerance to compensate numerical errors)
To find whether point is outside triangle:
choose any non-zero component of normal N (say z-component)
calculate only this component of cross-product of
AB x AP (here we need only (AB.x*AP.y-AB.y*AP.x) to get z-component of result)
BC x BP
CA x CP
If all signs of results are equal (positive or negative) - P is inside, if signs differ - P is outside.
Basically if the points distance to the plane is zero.
Here is my first google hit https://mathinsight.org/distance_point_plane

Ellipse Overlap Area

I am working on an Eye Tracking application, and when I detect the pupil and enveloping it with an ellipse I have to compare it to a ground-truth (exact ellipse around the pupil).
There are always 3 cases of course:
No Overlap >> overlap = intersection = 0
Partial to Perfect Overlap >> overlap = intersection area / ground-truth area
Enclosing >> overlap = intersection area / ground truth
My problem is the 3rd case where e.g. found ellipse is much bigger than the ground-truth hence enclosing it inside so the total overlap is given as 1.0 which is mathematically right but detection-wise not really as the found ellipse doesn't only contain the pupil inside it but other non-pupil parts.
The question is:
What would be the best approach to measure and calculate the overlap percentage between the found and ground-truth ellipses? would be just mere division of the areas?
Please give some insights.
P.S.: I am coding with python and tried to use shapely library for the task as mentioned in the answer to this question as supposedly it does the transform to position the ellipses correctly regarding their rotational angle.
Let R be the reference ellipse, E the calculated ellipse.
We define score := area(E ∩ R) / area(E ∪ R). The larger the score the better the match.
As ∅ ⊆ E ∩ R ⊆ E ∪ R, we have 0 ≤ score ≤ 1, score=0 ⇔ (E ∩ R = ∅) and
score=1 ⇔ E=R.
Consider an ellipse that is completely enclosed by R and has half the area, as well as an ellipse that completely encloses R and has twice the area. Both would have a score of 0.5 . If they were closer to R, for example if the first had 4/5 the area and the second 5/4 the area both would have a score of 0.8 .

Find the position of a circle tangent to two other circles

Say there are 3 circles, A, centered at point a, B centered at point b, and C, centered at point c. Each has a known radius independent of the others, Ar, Br, and Cr. The positions of a and b are known, but the position of c isn't.
The distance between a and b will always be between (Ar + Br) and (Ar + Br + (2 * Cr)).
I'm looking for a pseudo-code algorithm to find the position of c so that circles A and C are tangent, and circles B and C are tangent. There ought to be two solutions unless a and b are at their maximum allowed distance, in which case there would only be one.
Thank you, any help is much appreciated.
"Inflate" the circles A and B by Rc at the same time as you shrink C to a single point. Then the center of C appears as the intersection of the two inflated circles.
Write the implicit equation of the two circles and subtract one from the other; the quadratic terms cancel out, leaving the equation of a straight line (the line through the two intersection points).
(X-Xa)² + (Y-Ya)² = (Ra+Rc)²
(X-Xb)² + (Y-Yb)² = (Rb+Rc)²
=> by subtraction
(Xa-Xb)(2X-Xa-Xb) + (Ya-Yb)(2Y-Ya-Yb) = (Ra-Rb)(Ra+Rb+2Rc)
To solve this, you can express Y as a function of X using the linear relation, then substitute Y in the equation of one of the circles and solve the second degree equation in X, yielding two solutions.

Calculating the fraction of the area of multiple squares overlapped by a circle

This is a geometrical question based on a programming problem I have. Basically, I have a MySQL database full of latitude and longitude points, spaced out to be 1km from each other, corresponding to a population of people who live within the square kilometer around each point. I then want to know the relative fraction of each of those grids taken up by a circle of arbitrary size that overlaps them, so I can figure out how many people roughly live within a given circle.
Here is a practical example of one form of the problem (distances not to scale):
I am interested in knowing the population of people who live within a radius of point X. My database figures out that its entries for points A and B are close enough to point X to be relevant. Point A in this example is something like 40.7458, -74.0375, and point B is something like 40.7458, -74.0292. Each of those green lines from A and B to its grid edge represents 0.5 km, so that the gray circle around A and B each represent 1 km^2 respectively.
Point X is at around 40.744, -74.032, and has a radius (in purple) of 0.05 km.
Now I can easily calculate the red lines shown using geographic trig functions. So I know that the line AX is about .504 km, and the distance line BX is about .309 km, for whatever that gets me.
So my question is thus: what's a solid way for calculating the fraction of grid A and the fraction of grid B taken up by the purple circle inscribed around X?
Ultimately I will be taking the population totals and multiplying them by this fraction. So in this case, the 1 km^2 grid around corresponds to 9561 people, and the grid around B is 10763 people. So if I knew (just hypothetically) that the radius around X covered 1% of the area of A and 3% of the area of B, I could make a reasonable back-of-the-envelope estimate of the total population covered by that circle by multiplying the A and B populations by their respective fractions and just summing them.
I've only done it with two squares above, but depending on the size of the radius (which can be arbitrary), there may be a whole host of possible squares, like so, making it a more general problem:
In some cases, where it is easy to figure out that the square grid in question is 100% encompassed by the radius, it is in principle pretty easy (e.g. if the distance between AX was smaller than the radius around X, I know I don't have to do any further math).
Now, it's easy enough to figure out which points are within the range of the circle. But I'm a little stuck on figuring out what fractions of their corresponding areas are.
Thank you for your help.
I ended up coming up with what worked out to be a pretty good approximate solution, I think. Here is how it looks in PHP:
//$p is an array of latitude, longitude, value, and distance from the centerpoint
//$cx,$cy are the lat/lon of the center point, $cr is the radius of the circle
//$pdist is the distance from each node to its edge (in this case, .5 km, since it is a 1km x 1km grid)
function sum_circle($p, $cx, $cy, $cr, $pdist) {
$total = 0; //initialize the total
$hyp = sqrt(($pdist*$pdist)+($pdist*$pdist)); //hypotenuse of distance
for($i=0; $i<count($p); $i++) { //cycle over all points
$px = $p[$i][0]; //x value of point
$py = $p[$i][1]; //y value of point
$pv = $p[$i][2]; //associated value of point (e.g. population)
$dist = $p[$i][3]; //calculated distance of point coordinate to centerpoint
//first, the easy case — items that are well outside the maximum distance
if($dist>$cr+$hyp) { //if the distance is greater than circle radius plus the hypoteneuse
$per = 0; //then use 0% of its associated value
} else if($dist+$hyp<=$cr) { //other easy case - completely inside circle (distance + hypotenuse <= radius)
$per = 1; //then use 100% of its associated value
} else { //the edge cases
$mx = ($cx-$px); $my = ($cy-$py); //calculate the angle of the difference
$theta = abs(rad2deg(atan2($my,$mx)));
$theta = abs((($theta + 89) % 90 + 90) % 90 - 89); //reduce it to a positive degree between 0 and 90
$tf = abs(1-($theta/45)); //this basically makes it so that if the angle is close to 45, it returns 0,
//if it is close to 0 or 90, it returns 1
$hyp_adjust = ($hyp*(1-$tf)+($pdist*$tf)); //now we create a mixed value that is weighted by whether the
//hypotenuse or the distance between cells should be used
$per = ($cr-$dist+$hyp_adjust)/100; //lastly, we use the above numbers to estimate what percentage of
//the square associated with the centerpoint is covered
if($per>1) $per = 1; //normalize for over 100% or under 0%
if($per<0) $per = 0;
}
$total+=$per*$pv; //add the value multiplied by the percentage to the total
}
return $total;
}
This seems to work and is pretty fast (even though it does use some trig on the edge cases). The basic logic is that when calculating edge cases, the two extreme possibilities is that the circle radius is either exactly perpendicular to the grid, or exactly at 45 degree angles from it. So it figures out roughly where between those extremes it falls and then uses that to figure out roughly what percentage of the grid square is covered. It gives plausible results in my testing.
For the size of the squares and circles I am using, this seems to be adequate?
I wrote a little application in Processing.js to try and help me work this out. Without explaining all of it, you can see how the algorithm is "thinking" by looking at this screenshot:
Basically, if the circle is yellow it means it has already figured out it is 100% in, and if it is red it is already quickly screened as 100% out. The other cases are the edge cases. The number (ranging from 0 to 1) under the dot is the (rounded) percentage of coverage calculated using the above method, while the number under that is the calculated theta value used in the above code.
For my purposes I think this approximation is workable.
With enough classification (sketched below) all computations can be reduced to a primitive calculation, the one that provides the angular area of the orange region depicted in the image
When y0 > 0, as illustrated above, and regardless of whether x0 is positive or negative, the orange area can be calculated accurately as the integral from x0 to x1 of sqrt(r^2 - y^2) minus the rectangular area (x1 - x0) * (y1 - y0). The integral has a well known closed expression and therefore there is no need to use any numerical algorithm for calculating it.
Other intersections between a circle and a square can be reduced to a combination of rectangles and right-angular shapes as the one painted in orange above. For instance, an intersection delimited by the horizontal and vertical orange rays in the following picture can be expressed by summing the area of the red rectangle plus two angular shapes: the blue and the green.
The blue area results from a direct application of the primitive case identified above (where the inferior rectangle collapses to nothing.) The green one can also be measured in the same way, once the negative y coordinate is replaced by its absolute value (the other y being 0).
Applying these ideas one could enumerate all cases. Basically, one should consider the case where just one, two, three or four corners of the square lie inside the circle, while the remaining (if any) fall outside. The enumeration is a problem in itself, but it can be solved, at least in theory, by considering a relatively small number of "typical" configurations.
For each of the cases enumerated as described a decomposition on some few rectangles and angular areas has to be calculated and the parts added up (or subtracted) as shown in the three-color example above. The area of every part would reduce to rectangular or primitive angular areas.
A considerably amount of work has to be done to turn this line of attack into a working algorithm. A deeper analysis could shed some light on how to minimize the number of "typical" configurations to consider. If not, I think that the amount of combinations to consider, however large, should be manageable.
In case your problem admits an approximate answer there is another technique you could use which is much simpler to program. The whole idea of this problem reduces to calculate the area of the intersection of a square and a circle. I didn't explain this in my other answer, but finding the squares that are likely to intercept the circle shouldn't be a problem, otherwise, let us know.
The idea of calculating the approximate area of the intersection is very simple. Generate enough points in the square at random and check how many of them belong in the circle. The ratio between the number of points in the circle and the total number of random points in the square will give you the proportion of the intersection with respect to the square's area.
Now, given that you have to repeat the same routine for all squares surrounding the circle (i.e., squares which center has a distance to the circle's center not very different from the circle's radius) you could re-use the random points by translating them from one square to the other.
I don't want to go into details if this method is not appropriate for your problem, so let me just indicate that generating random points uniformly distributed in the square is fairly easy. You only need to generate random numbers for the x coordinate and, independently, random numbers for y. Then just consider all pairs (x, y). Then, for every (x, y) verify whether (x - a)^2 + (y - b)^2 <= r^2 or not, where (a, b) stands for the circle's center and r for the radius.

Calculating the position of an object on a parabola

I am working on a simple 2D game. In the game, I have a 'robot' that throws a ball towards another robot, in the shape of a parabola. Both 'robots' are positioned on the x axis, aka their y co-ordinates are the same.
The program knows the positions of the two robots, and it knows the position of the vertex.
As I said, I need the ball to travel along the parabola. This means (correct me if I'm wrong), that at any given time, I need to be able to calculate the y position of the ball, since I know it's x position. (Or is there a better way to do this?)
If so, how can I calculate the y position of the ball at any given time, as I said while knowing the location of the parabolas' vertex, and knowing it's two points of intersection with the x axis?
Thanks
EDIT: Please try to make your answers as clear as possible, since my math knowledge is very basic. Thanks
Suppose your parabola is given by P(x) = a x^2 + b x + c. You'll know the formula of the parabola if you know a, b and c. You have three pieces of information:
P(x1) = 0 (Robot 1 is on the ground at x1)
P(x2) = 0 (Robot 2 is on the ground at x2)
P((x1 + x2)/2) = yMax (Half way between the two points the ball is at its maximum)
This is three linear equations in three unknowns (you know x1, x2, yMax, you don't know a, b, c). If you know how to solve three equations in three unknowns, you're all set.

Resources