So you want to morph square to inscribed circle. First take a look at:
its very similar problem to yours just in 3D and scaling to surface instead of just scaling. Porting of that to suite your needs looks like this:
- detect which coordinate is abs max
- compute scale from square to circle based on angle against axis of abs max coordinate
- apply scale
Here simple C++/OpenGL example assuming 2D axis aligned square of size 2*r
centered at (0,0)
:
void gl_draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
int i;
float x,y,xx,yy,a,r=0.75;
RandSeed=123456;
glBegin(GL_POINTS);
for (i=0;i<10000;i++)
{
// random square point <-1,+1>
x=2.0*(Random()-0.5);
y=2.0*(Random()-0.5);
// with 75% hollow inside
xx=fabs(x);
yy=fabs(y);
if ((xx>=yy)&&(xx<0.75)) continue;
if ((xx< yy)&&(yy<0.75)) continue;
// size 2*r
x*=r;
y*=r;
// render square point
glColor3f(1.0,0.3,0.3);
glVertex2f(x,y);
// morph to circle
xx=fabs(x);
yy=fabs(y);
if (xx+yy<=1e-10) // avoid division by zero
{
x=0; y=0;
}
else if (xx>=yy) // x is major axis
{
a=atan(yy/xx); // angle
a=cos(a); // inscribed circle scale
x*=a;
y*=a;
}
else{ // y is major axis
a=atan(xx/yy); // angle
a=cos(a); // inscribed circle scale
x*=a;
y*=a;
}
// render circle point
glColor3f(0.3,0.3,1.0);
glVertex2f(x,y);
}
glEnd();
glFlush();
SwapBuffers(hdc);
}
And preview:

I used hollow square so its clearly seen the correspondence between square and circle points (with full area it was not as nicely seen)
So the morphing of (x,y)
is done like this:
// morph square (x,y) to circle (x,y)
float xx,yy,a;
xx=fabs(x);
yy=fabs(y);
if (xx+yy<=1e-10) a=0.0;
else if (xx>=yy) a=cos(atan(yy/xx));
else a=cos(atan(xx/yy));
x*=a;
y*=a;
And do not forget to include math.h
or equivalent...