Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
apollonius
Advanced tools
Solves Apollonius' problem by finding a fourth circle tangent to three given circles
The apollonius
module provides a function to find a circle that touches three known circles. The resulting circle is a solution to the Problem of Apollonius. In other words, it finds a circle that is tangent to each of the known three circles. The function is robust: the three known circles can be placed freely and are allowed to overlap each other.
Because a circle can be either internally or externally tangent to another circle, the problem of Apollonius has eight solutions in total, one for each combination of tangency rules of the three circles. The function here finds only one solution per call but can be used to find all eight.
The function is extremely efficient. It has time complexity of O(1) and does not call any trigonometric functions.
$ npm install apollonius
Specify your three known circles as { x, y, r }
objects, where x
and y
are the circle center coordinates and r
is the radius. Then call the function apollonius.solve
with the circles. The order of the circles does not matter.
import * as apollonius from 'apollonius'
// OR import solve from 'apollonius'
// OR import { solve } from 'apollonius'
// OR const apollonius = require('apollonius')
// Prepare three known circles.
const c1 = { x: 3, y: 2, r: 1 }
const c2 = { x: 7, y: 2, r: 2 }
const c3 = { x: 3, y: 5, r: 1 }
// Compute a fourth circle that touches the three.
const c = apollonius.solve(c1, c2, c3)
// Result equals { x: 4.367544..., y: 3.5, r: 1.029822... }
The result is a circle object { x, y, r }
or null
if such a circle cannot be found. By default, the resulting circle is externally tangent to each of the three given circles. To find a circle that is internally tangent to some of the circles, specify those circles with negative radius. See below for an example.
// Prepare circles.
const c1 = { x: 3, y: 2, r: -1 } // r < 0, thus internally tangent
const c2 = { x: 7, y: 2, r: 2 } // externally tangent
const c3 = { x: 3, y: 5, r: -1 } // r < 0, thus internally tangent
// Compute the fourth circle.
const c = apollonius.solve(c1, c2, c3)
// Result equals { x: 2.732213..., y: 3.5, r: 2.523715... }
The code above is illustrated below:
The resulting circle is internally tangent to the known circles c1
and c3
and externally tangent to the known circle c2
. Note that while the known circles can have negative radii, the output circle always has positive or zero radius.
The fourth circle cannot be found for some configurations of known circles. These configurations may appear when there are:
If the fourth circle cannot be found, the function will return null
.
The fourth circle may reduce to a point (a circle with zero radius) in some configurations of known circles. These configurations may appear when there are:
This function finds a circle that is tangent to three other circles. If no such circle exists, it returns null.
Parameters:
{ x, y, r }
, representing a circle in 2D. The properties x
, y
, and r
must be real numbers and are allowed to be negative.{ x, y, r }
{ x, y, r }
Returns:
{ x, y, r }
where r
is always positive or zero.null
if no tangent circle exists or if the radius of the circle is infinite.The function apollonius.solve
handles various (special cases)[#specialcases] by switching to alternative algorithms when certain internal variables turn zero. However, the variables rarely exactly equal zero because of rounding errors caused by floating point arithmetics. Computation with near-zero numbers would cause arbitrary results and therefore a margin of safety is needed.
The epsilon
defines the numerical margin in which an almost zero number is treated as zero. The default value for epsilon is 1e-10
. You can adjust it if needed. For example, if you know the properties of your circles will be large numbers then a larger epsilon may yield more robust behavior near the special cases:
apollonius.options.epsilon = 1e-4
Pull requests and bug reports are highly appreciated.
Clone the repository:
$ git clone git@github.com:axelpale/apollonius.git
Install development tooling:
$ cd apollonius; npm install
Please test your contribution. Run the test suite:
$ npm run test
Run only linter:
$ npm run lint
Thank you.
The apollonius source code is released under MIT license.
[1.0.0] - 2024-10-12
exports.require
module.exports
.The module root is now an object instead of a function. Therefore if you previously const apollonius = require('apollonius')
and then const c = apollonius(c1, c2, c3)
then just switch to const c = apollonius.solve(c1, c2, c3)
.
FAQs
Solves Apollonius' problem by finding a fourth circle tangent to three given circles
The npm package apollonius receives a total of 4 weekly downloads. As such, apollonius popularity was classified as not popular.
We found that apollonius demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.