6
$\begingroup$

Using circle and square equations I was able to draw this. Any way to blend these two shapes(equations) like this and this. I want to achieve metaballs like effect.

enter image description here

I'm using unity engine to render the above, I want a 2D version of it. I basically colored all pixels which are inside a circle. Eg: using unit circle at origin equation $x^2 + y^2 = 1$ I'm coloring all pixels if $p_x^2 + p_y^2 <= 1$, where $p_x$ and $p_y$ are position of pixel.So I want a equation(or method) which blends the shapes and determines if a pixel is inside the overall shape.

$\endgroup$
11
  • 1
    $\begingroup$ What is a metaball ? $\endgroup$
    – Narasimham
    May 27 at 9:40
  • 3
    $\begingroup$ Your question is too general. You should be more explicit about 1) The software you use. Do you want to stay with Desmos, switch to Geogebra or another software ? 2) Whether you want, out of a set of 2D points generate a of 3D points mesh, with for example which kind of lighting (are you familiar with Gouraud, Phong ?...) $\endgroup$
    – Jean Marie
    May 27 at 9:40
  • 2
    $\begingroup$ Could you add the details of the software in the post so it has the maximum chance of being re-opened @HelloHumans $\endgroup$ Jun 2 at 10:22
  • 3
    $\begingroup$ I've voted to open already. and posted a meta thread here math.meta.stackexchange.com/questions/34839/…. Hopefully there is some community support to open it. You yourself can cast one open vote I think $\endgroup$ Jun 2 at 15:25
  • 1
    $\begingroup$ @Aplateofmomos your efforts to reopen have paid off! $\endgroup$ Jun 4 at 6:52

2 Answers 2

5
$\begingroup$

Finally got working pseudocode:

//distance between point p and circle with radius r
float sdCircle( vec2 p, float r )
{
    return length(p) - r;
}

//function to blend
float smin( float a, float b, float k )
{
    float res = exp2( -k*a ) + exp2( -k*b );
    return -log2( res )/k;
}

float circle1_Dis = sdCircle(...);
float circle2_Dis = sdCircle(...);
float k = 0.5; // how much to blend

if(smin(circle1_Dis ,circle2_Dis ,k) < 0)
{
    //draw
}

reference:

$\endgroup$
3
  • $\begingroup$ You use the (binary) log of the sum of 2 negative exponential to express the distance function ; interesting, I had never seen that before... $\endgroup$
    – Jean Marie
    May 27 at 21:13
  • $\begingroup$ You will be possibly interested by this Wikipedia article on "LogSumExp" where the geometrical "blending" you use has a correspondence in the domain of deep learning $\endgroup$
    – Jean Marie
    May 27 at 22:39
  • 1
    $\begingroup$ @JeanMarie It's amazing that such simple functions are used in diverse fields. $\endgroup$ May 28 at 3:01
1
$\begingroup$

Take a look at the Geogebra figure (sorry, I don't use that much Desmos) I just made in order to convey the two main ideas :

    1. An angular parametrization of the circle and as well of the square by an angle in the range $[0, 2 \pi)$. For the circle, it is the very natural :

$$N=(\cos(t),\sin(t))$$

with an added horizontal shift as you have done.

For the square, you have to divide the previous expression by a tuned quantity shrinking the circle into the square by a certain factor depending on the angle $t$ ; this quantity can be given the closed form $\max(|\cos(t)|,|\sin(t)|)$ explaining coordinates:

$$M=N/\max(|\cos(t)|,|\sin(t)|)$$

    1. Intermediate moving point generating the level line labelled $s$ with $0 \le s \le 1$ given by barycentric expression

$$P=(1-s)M+sN$$

(for example if $s=\frac12$, you get the midpoint of line segment $MN$ ; the smallest $s$, the closer you are to point $M$, etc.).

You see that the locus of point $P$ gives one (among many) intermediate shapes between the square and the circle.

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, but this approach produces third intermediate shape P. I want M and N to slowly blend together like metaballs. I have updated my question to make it clear. $\endgroup$ May 27 at 11:03
  • $\begingroup$ Yes but what I have displayed is a "didactic" example for a particular values of $s$ ; make $s$ vary now. $\endgroup$
    – Jean Marie
    May 27 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.