2

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

  • 1
    This seems to be a math question!? – Support Ukraine Jun 28 '21 at 18:50
  • 1
    Charles34 research `hypot(), atan2()` to go from x,y to circular co-ordinates. Use `sin(), cos()` to go the other way. – chux - Reinstate Monica Jun 28 '21 at 18:52
  • chux-ReinstateMonica, how do I obtain the equivalent circle(x,y) to be able to draw a point inside the circle given the squared(x,y)? Thanks! –  Jun 28 '21 at 19:36
  • @Charles34 could you add a drawing of this ? – Sorenp Jun 29 '21 at 08:28
  • @Sorenp added a drawing showing where the point falls in square coordiantes and where it should or I think it should fall... –  Jun 29 '21 at 08:37
  • Ok nice, I'm still in favor of my answer, because of simplicity. – Sorenp Jun 29 '21 at 08:51
  • *Why* should it fall there? I see that it's on the same ray (from the center of the circle), but how do you decide the distance from the center? If all that is necessary is that it be inside the circle, then you could move *every* point outside the circle to the center. You have a requirement in mind, which you are not telling us. – Beta Jun 29 '21 at 20:41

2 Answers2

0

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.

Sorenp
  • 320
  • 2
  • 12
  • that is not what OP is asking ... You just cut off the points that are outside square, but OP want to morph them into circle instead. – Spektre Jun 29 '21 at 07:20
  • I think we need more details from OP, maybe a drawing. – Sorenp Jun 29 '21 at 08:28
0

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

Spektre
  • 49,595
  • 11
  • 110
  • 380