“Sphere-Sphere Intersection” by frankie zafe
https://openprocessing.org/sketch/539606
License CreativeCommons Attribution
https://creativecommons.org/licenses/by/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Mouse, just move it.
CC Attribution
Sphere-Sphere Intersection
zafe
xxxxxxxxxx
var sphereA;
var sphereB;
function setup() {
createCanvas( 600, 400 );
sphereA = {};
sphereA.pos = [ 120, 200 ];
sphereA.radius = 100;
sphereB = {};
sphereB.pos = [ 400, 200 ];
sphereB.radius = 100;
}
function draw() {
sphereB.pos[0] = mouseX;
sphereB.pos[1] = mouseY;
sphereB.radius = 150 + sin( frameCount * 0.03 ) * 20;
// computation of direction from sphereA center to sphereB center
var dirAB = [
sphereB.pos[0] - sphereA.pos[0],
sphereB.pos[1] - sphereA.pos[1]
];
// computation of the vecto length
var l = sqrt( pow(dirAB[0],2) + pow(dirAB[1],2) );
// and normalizing the vector
dirAB[0] /= l;
dirAB[1] /= l;
if ( l < sphereA.radius && l < sphereB.radius ) {
dirAB[0] *= -1;
dirAB[1] *= -1;
}
// computation of the angle of the direction
var angle = atan2( dirAB[1], dirAB[0] ) + PI * 0.5;
// rendering the normal of the direction
var normal = [ cos(angle), sin(angle) ];
// computation of the radius of the spheres intersection
// http://mathworld.wolfram.com/Sphere-SphereIntersection.html
var a =
( 1.0 / ( 2.0 * l ) ) *
sqrt(
( 4 * pow(l,2) * pow(sphereA.radius,2) ) -
pow( pow(l,2) - pow(sphereB.radius,2) + pow(sphereA.radius,2), 2 )
);
// computation of the distance to find the center of the intersection circle
// hypo² = basis² + opposite²
var basis_length = sqrt( pow(sphereA.radius,2) - pow(a,2) );
// absolute position of the intersection center
var middot = [
sphereA.pos[0] + dirAB[0] * basis_length,
sphereA.pos[1] + dirAB[1] * basis_length
];
// display of values
background(0);
strokeWeight( 1 );
noFill();
stroke( 255 );
ellipse( sphereA.pos[0], sphereA.pos[1], sphereA.radius * 2, sphereA.radius * 2 );
ellipse( sphereB.pos[0], sphereB.pos[1], sphereB.radius * 2, sphereB.radius * 2 );
noStroke();
fill( 255,0,0 );
ellipse( middot[0], middot[1], 5, 5 );
fill( 0,255,255 );
ellipse( middot[0] + normal[0] * a, middot[1] + normal[1] * a, 10, 10 );
strokeWeight( 2 );
stroke( 0,255,0 );
line(
sphereA.pos[0], sphereA.pos[1],
middot[0], middot[1]
);
stroke( 0,255,255 );
line(
middot[0], middot[1],
middot[0] + normal[0] * a, middot[1] + normal[1] * a
);
noStroke();
fill(255);
text( "intersection radius: " + a, 10, 25 );
}
See More Shortcuts