Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:01:39+00:00 2026-05-27T16:01:39+00:00

for circle i have tried to check for all points within radius distance from

  • 0

for circle i have tried to check for all points within radius distance from center.For square, i test for all points from bottom-left-corner to upper-right-corner and for triangle i test the signs of determinants as suggested here. I get correct answer while i enter individual values i.e either 1 cirlce or 1 square or 1 triangle , but not when there are >1 of them. E.g for the case:

C 10 10 3
S 9 8 4
T 7 9 10 8 8 10

where C is circle, S is square and T is triangle,and (10,10) is center of circle with radius 3. (9,8) is the left-most corner of square of side 4 and (7,9),(10,8) and (8,10) are the three vertices of the triangle , the total distinct points covered by them is 34 but i am getting 37.

Here’s what i’ve tried:

typedef pair<int,int> point;
set<point>myset;
set<point>::iterator it;

int findDeter(int x1,int y1,int x2,int y2,int x0,int y0)
{
int ret = x1*(y2-y0)-y1*(x2-x0)+(x2*y0-x0*y2)
         -x2*(y1-y0)+y2*(x1-x0)-(x1*y0-x0*y1)
         +x0*(y1-y2)-y0*(x1-x2)+(x1*y2-x2*y1);
return ret;
}
bool sameSign(int x, int y)
{
if(x==0||y==0)
return true;
    return (x >= 0) ^ (y < 0);
}
int main()
{
int t,i,j,k,n;
int x,y,r,x1,y1,len;
int xmax,ymax,xmin,ymin;
int D1,D2,D3;
int ax,ay,bx,by,cx,cy;
char shape,dump;
scanf("%d",&t);
while(t--)
{
    myset.clear();
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%c",&dump);
        scanf("%c",&shape);
        if(shape=='C')
        {
            scanf("%d %d %d",&x,&y,&r);
            for(j=x;j<=x+r;j++)
            {
                for(k=y;k<=y+r;k++)
                {
                    point p(j,k);
                    myset.insert(p);
                }
            }
            for(j=x-r;j<x;j++)
            {
                for(k=y-r;k<y;k++)
                {
                    point p(j,k);
                    myset.insert(p);
                }
            }   

        }
        else if(shape=='S')
        {
            scanf("%d %d %d",&x1,&y1,&len);
            for(j=x1;j<=x1+len;j++)
            {
                for(k=y1;k<=y1+len;k++)
                {
                    point p(j,k);
                    myset.insert(p);
                }
            }

        }
        else
        {
            //printf("here\n");
            scanf("%d %d %d %d %d %d",&ax,&ay,&bx,&by,&cx,&cy);
            /*a1=ax;a2=ay;
            b1=bx;b2=by;
            c1=cx;c2=cy;*/
            xmax = max(ax,max(bx,cx));
            ymax = max(ay,max(by,cy));
            xmin = min(ax,min(bx,cx));
            ymin = min(ay,min(by,cy));
            /*for each point P check if sum(the determinants PAB,PAC and PBC have the same signs)*/
            for(j=xmin;j<=xmax;j++)
            {
                for(k=ymin;k<=ymax;k++)
                {
                    D1 = findDeter(ax,ay,bx,by,j,k);
                    //printf("D1 : %d\n",D1);
                    D2 = findDeter(bx,by,cx,cy,j,k);
                    //printf("D2 : %d\n",D2);
                    D3 = findDeter(cx,cy,ax,ay,j,k);
                    //printf("D3 : %d\n",D3);
                    if(sameSign(D1,D2)&&sameSign(D2,D3)&&sameSign(D1,D3))
                    {
                        //printf("here\n");
                        point p(j,k);
                        myset.insert(p);
                    }
                }
            }
        }

    }
    printf("%d\n",myset.size());
}
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:01:40+00:00Added an answer on May 27, 2026 at 4:01 pm

    After refactoring your code heavily so that it was clearer to me what is going on – I’d say the error is in the circle code. I’ve included the complete refactored code below but the troublesome section amount s to this:

    struct Circle
    {
       int x;
       int y;
       int r;
    
       void add_covered_points( set<points> & pts ) const
       {
          for(int j=x;j<=x+r;j++)
          {
             for(int k=y;k<=y+r;k++)
             {
                pts.insert(point(j,k));
             }
          }
          for(int j=x-r;j<x;j++)
          {
             for(int k=y-r;k<y;k++)
             {
                pts.insert(point(j,k));
             }
          }  
       }
    };
    

    This seems to add points from two rectangular sections, one above and the other below the center of the circle. I’d expect the code to look more like this:

       void add_covered_points( set<points> & pts ) const
       {
          for(int j=-r;j<=+r;j++)
          {
             for(int k=-r;k<=+r;k++)
             {
                if (j*j + k*k < r*r )
                {
                  pts.insert(point(x+j,x+k));
                }
             }
          } 
       }
    

    Heres the complete refactored case for your reference

    typedef pair<int,int> point;
    
    
    int findDeter(int x1,int y1,int x2,int y2,int x0,int y0)
    {
    int ret = x1*(y2-y0)-y1*(x2-x0)+(x2*y0-x0*y2)
             -x2*(y1-y0)+y2*(x1-x0)-(x1*y0-x0*y1)
             +x0*(y1-y2)-y0*(x1-x2)+(x1*y2-x2*y1);
    return ret;
    }
    bool sameSign(int x, int y)
    {
    if(x==0||y==0)
    return true;
        return (x >= 0) ^ (y < 0);
    }
    
    struct Circle
    {
       int x;
       int y;
       int r;
    
       void add_covered_points( set<points> & pts ) const
       {
          for(int j=x;j<=x+r;j++)
          {
             for(int k=y;k<=y+r;k++)
             {
                pts.insert(point(j,k));
             }
          }
          for(int j=x-r;j<x;j++)
          {
             for(int k=y-r;k<y;k++)
             {
                pts.insert(point(j,k));
             }
          }  
       }
    };
    
    struct Square
    {
      int x1,y1,len;
      void add_covered_points( set<points> & pts ) const
      {
         for(int j=x1;j<=x1+len;j++)
         {
            for(int k=y1;k<=y1+len;k++)
            {
               myset.insert(point(j,k));
            }
         }
      }
    };
    
    struct Triangle
    {
      int ax,ay,bx,by,cx,cy;
      void add_covered_points( set<points> & pts ) const
      {
         int xmax = max(ax,max(bx,cx));
         int ymax = max(ay,max(by,cy));
         int xmin = min(ax,min(bx,cx));
         int ymin = min(ay,min(by,cy));
    
         /*for each point P check if sum(the determinants PAB,PAC and PBC have the same signs)*/
         for(int j=xmin;j<=xmax;j++)
         {
           for(int k=ymin;k<=ymax;k++)
           {
              int D1 = findDeter(ax,ay,bx,by,j,k);
              int D2 = findDeter(bx,by,cx,cy,j,k);
              int D3 = findDeter(cx,cy,ax,ay,j,k);
    
              if(sameSign(D1,D2)&&sameSign(D2,D3)&&sameSign(D1,D3))
              {
                pts.insert(point(j,k));
              }
            }
         }
      }
    };
    
    
    int main()
    {
    set<point>myset;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        myset.clear();
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            char dump;
            char shape;
            scanf("%c",&dump);
            scanf("%c",&shape);
            if(shape=='C')
            {
                Circle c;
                scanf("%d %d %d",&c.x,&c.y,&c.r);
                c.add_covered_points( myset );
            }
            else if(shape=='S')
            {
                Square s;
                scanf("%d %d %d",&s.x1,&s.y1,&s.len);
                s.add_covered_points( myset );
            }
            else
            {
                Triangle t;
                int ax,ay,bx,by,cx,cy;
                scanf("%d %d %d %d %d %d",&t.ax,&t.ay,&t.bx,&t.by,&t.cx,&t.cy);
                t.add_covered_points( myset );
            }
        }
    }
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im searching for radius and the center coordinates of circle in a image. have
To draw a circle on map I have a center GLatLng (A) and a
I have this svg: <circle cx=50 cy=100 r=50 stroke-width=0 fill=orange/> <polygon points=0,100, 50,50 100,100
Let's say I have a super type (Shape) and two children (like Square, Circle).
In Python, say I have some class, Circle, that inherits from Shape. Shape needs
how to draw a circle in openlayer map? i have tried in diff way,
I have seen this everywhere and tried all the solutions and am not seeming
I need to have all point coordinates for a given circle one after another,
I have two packages, Shapes and Fruits : com.myproject.Shapes. Circle Square Triangle com.myproject.Fruits. Apple
I have drawn a circle and applied radial gradient. fine. But when i changed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.