How to efficiently find the total area of a union of circles? - geometry

I have a data set defined as:
[
[lat, long, radius],
[lat, long, radius],
... (100-10k)
[lat, long, radius]
]
How can I calculate the area of the reunion of all the circles?
The sum would be the total area of all the circles - the intersection (to remove duplicate counts)?

Related

How to use VBA to incorporate functions to avoid occupation of extra cells?

This spreadsheet aims to calculate distances between atoms, and we want to improve the functions so as to avoid the occupation of extra columns. (See image postscripted. Atom coordinates are given in the Column A to D, and the atom pair whose distance should be calculated is given in Column F to G.)
Currently in the first step, coordinates of specified atoms are picked up in columns I to O. e.g. Cell I4 is filled with the function:
=VLOOKUP($F4,$A$4:$E$1023,2,FALSE)
and then in the next step, the distance could be resolved in Column Q with Euclidean distance formula on the coordinates picked up. e.g. Cell Q4 is:
=SQRT(POWER((I4-M4),2)+POWER((J4-N4),2)+POWER((K4-O4),2))
According to the distance calculating algorithm, once the two atoms are specified, the distance is then determined. Thus, is it possible to write a function with VBA to gracefully incorporate these functions and take away these pilot processes from columns I to O? (Because these columns will be used otherwise in the future; and the code readability would be terrible if we put, for example, the six VLOOKUP functions directly into the final SQRT function.)
I'm new to VBA. Any help would be appreciated. Thanks!
The original data in this spreadsheet is as below: (From the third line)
Atom_No X_coordinate Y_coordinate Z_coordinate Atom_No1 Atom_No2 X1 Y1 Z1 X2 Y2 Z2 Distance
1 2.35739851 13.17160225 4.022993565 4 2 3.827347994 9.501971245 8.374602318 4.403610706 11.14351559 6.991936684 2.222276039
2 4.403610706 11.14351559 6.991936684 3 2 0.721047342 12.58075523 2.64032793 4.403610706 11.14351559 6.991936684 5.879067059
3 0.721047342 12.58075523 2.64032793 1 4 2.35739851 13.17160225 4.022993565 3.827347994 9.501971245 8.374602318 5.879068118
4 3.827347994 9.501971245 8.374602318 2 1 4.403610706 11.14351559 6.991936684 2.35739851 13.17160225 4.022993565 4.13699687
… … … … 3 1 0.721047342 12.58075523 2.64032793 2.35739851 13.17160225 4.022993565 2.22227577
Finally, these two modules will work and get the correct result when CoordinateTableRange and Atom_No1-2 pair is given like this table. You could load these two modules, and write in column R (e.g. R4 cell) with
=AtomsDistance(F4,G4)
, then you'll find the distance is the same as you got in col Q.
Function CoordinateVLookUp(Atom_No As Integer, CoordinateTableRange As Range, Column As Integer, isFuzzy As Boolean) As Double
'To find the atom coordinates according to columns, with CoordinateTableRange selected and isFuzzy specified.
Dim myResult As Variant
myResult = Application.WorksheetFunction.VLookup(Atom_No, CoordinateTableRange, Column, isFuzzy)
If IsError(myResult) Then
MsgBox ("No result found.")
Else
CoordinateVLookUp = myResult
End If
End Function
Function AtomsDistance(Atom_No1 As Integer, Atom_No2 As Integer) As Double
'To call CoordinateVLookUp function above to acquire the x, y, z coordinates of both atoms, and then calculate the distance through Euclidean distance formula.
Dim x1 As Double
Dim y1 As Double
Dim z1 As Double
Dim x2 As Double
Dim y2 As Double
Dim z2 As Double
Dim CoordinateTableRange As Range
Set CoordinateTableRange = Range("A4:E1023") 'set should be added
x1 = CoordinateVLookUp(Atom_No1, CoordinateTableRange, 2, False)
y1 = CoordinateVLookUp(Atom_No1, CoordinateTableRange, 3, False)
z1 = CoordinateVLookUp(Atom_No1, CoordinateTableRange, 4, False)
x2 = CoordinateVLookUp(Atom_No2, CoordinateTableRange, 2, False)
y2 = CoordinateVLookUp(Atom_No2, CoordinateTableRange, 3, False)
z2 = CoordinateVLookUp(Atom_No2, CoordinateTableRange, 4, False)
AtomsDistance = Math.Sqr((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2))
End Function

How to know the location of a point in a rotated grid with equal sized square-shaped cells

Given a grid inclined at an angle θ (theta) with equal sized square-shaped cells with indices 00, 01, 02, ..., 55 (where first digit being x index and second being y index, for instance 00 means a cell at the intersection of row 0 and column 0), and a point p(x,y), how can we know that in which grid cell does the point lie without checking all the cells?
For instance, in the image below point p lies in the cell with index 23.
I found one answer at Checking if a point is inside a rotated rectangle that explains how to determine if a point lies inside a rotated rectangle, but with this approach I need to check all the grid cells.
Perhaps the simplest way - rotate point by the same angle (with negative sign) and check new coordinates in axis-aligned grid
nx = x * cos(theta) - y * sin(theta)
ny = x * sin(theta) + y * cos(theta)
row = (int) (ny / cellsize)
col = (int) (nx / cellsize)

How to calculate the total number of circle coordinates?

If having a centre(xc,yc) and radius(r), how can i calculate total number of coordinates(x,y) in the circle?
I suspect that you are talking about integer points in the circle. Otherwise question is senseless.
In this case you can apply Gauss formula
N = 1 + 4 * r + 4 * sum[i=1..r]{floor(sqrt(r^2-i^2))}
Note that center has integer coordinates too.

Chord height from circle edge in 3 tangent circles

I don't know if this question makes sense, but is there a formulaic way to calculate the height of a chord from a circle's edge in one of the circles from 3 tangent circles?
I have included a diagram to provide detail. Circles C1, C2 and C3 are connected at tangents and have equal radii (in this case 1 mm, but that is only for depiction). Triangle ABC is formed by connecting the centers of these circles. Line LM meets sides AB and AC and is tangential to Circle C1. Line PQ cuts through Circle C1 and triangle ABC and is tangential to both circles C2 and C3.
Diagram for question
What is the formula for the distance (x) between lines LM and PQ?
Given that the radii are equal (say, r), the total height of these 3 circles (line RS shown in diagram, which is my objective to calculate) is 4 times the radius minus the distance (x) between lines LM and PQ. In other words,
|RS| = {(4*r) - x}
variable x needs to be converted into a formula based exclusively on radius r so as to solve this equation.
It has been a while since I revisited my high school geometry lessons, so I hope this can be solved.
Distance A-LM is equal to r (circle center - tangent)
Distance BC-PQ is equal to r
If we add these distances and subtract distance PQ-LM (x), we'll get height of equilateral triangle ABC (with edge 2*r)
r + r - x = height of ABC = 2 * r * sqrt(3)/ 2
x = r * (2 - sqrt(3))

Graph permutation and rotation witn NetworkX

l work with Networkx to generate some class of graphs.
Now l would like to permute nodes and rotate the graph with (80°, 90°,120° degree)
How can l apply permutation and rotation on graphs with NetworkX ?
Edit_1:
Given an adjacency matrix of a graph, l would like to rotate the graph in the way that it preserves the edges and vertices link. The only thing that changes is the position of nodes.
What l would like to do is to rotate my graph with 90 degree.
Input :
Adjacency matrix of graph G
process :
Apply rotation on G with 90 degree
Output :
Rotated adjacency matrix
It means, the graph preserves its topology and just the index of adjacency matrix that changes position.
For example nodes 1 at index 0 after rotation will be at index 4 for instance.
What l have tried ?
1)l looked after numpy.random.permutation() but it does't seem to accept the rotation parameter.
2) In networkX l didn't find any function that allows to do rotation.
EDIT2
Given an adjacency matrix of 5*5 (5 nodes:
adj=[[0,1,0,0,1],
[1,0,1,1,0],
[0,0,0,1,1],
[0,0,1,0,1],
[1,1,1,1,0]
]
l would like to permute between indexes .
Say that node 1 takes the place of node 3 , node 3 takes the place of nodes 4 and node 4 takes the place of node 1.
It's just the permutation of nodes (preserving their edges).
l would like to keep in a dictionary the mapping between original index and the new index after permutation.
Secondly, l would like to apply permutation or rotation of this adjacency matrix with an angle of 90°. (It's like apply rotation on an image). I'm not sure how it can be done.
Take a look at the networkx command relabel_nodes.
Given a graph G, if we want to relabel node 0 as 1, 1 as 3, and 3 as 0 [so a permutation of the nodes, leaving 2 in place], we create the dict mapping = {0:1, 1:3, 3:0}. Then we do
H = nx.relabel_nodes(G, mapping)
And H is now the permuted graph.
import networkx as nx
G = nx.path_graph(4) #0-1-2-3
mapping = {0:1, 1:3, 3:0}
H = nx.relabel_nodes(G, mapping) #1-3-2-0
#check G's adjacency matrix
print(nx.to_numpy_matrix(G,nodelist=[0,1,2,3]))
> [[ 0. 1. 0. 0.]
[ 1. 0. 1. 0.]
[ 0. 1. 0. 1.]
[ 0. 0. 1. 0.]]
#check H's adjacency matrix
print(nx.to_numpy_matrix(H,nodelist=[0,1,2,3]))
> [[ 0. 0. 1. 0.]
[ 0. 0. 0. 1.]
[ 1. 0. 0. 1.]
[ 0. 1. 1. 0.]]

Resources