Transforming squared x,y coordinates to circle coordinates

Viewed 168

I have a circle that I want to draw dots inside using x,y coordinates.

What could I use to, for instance, make that points that are on the extremes fall inside the circle?

Thanks

EDIT: Drawing showing the dot falling outside the circle and the equivalent dot:

enter image description here

2 Answers

Calculate the distance of the dot to the origin, using Pythagoras theorem. If that distance a^2 + b^2 = c^2 <= the circles radius, we must conclude that the point falls within the circle boundaries. If the point falls outside, simply set c == radius and save the difference, if you so wish.

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:

  1. detect which coordinate is abs max
  2. compute scale from square to circle based on angle against axis of abs max coordinate
  3. 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:

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...

Related