PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] How to draw circle at a specific area?

 August 14, 2022     bgi, c, output     No comments   

Issue

I wrote a simple C program to make a circle. It follow mid-point circle drawing algorithm.

This is my code right now:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void main()
{
   int gd = DETECT, gm ;
   int r, x, y, xc, yc, P;

   initgraph(&gd, &gm ,"..//bgi");
   clrscr();

   printf("\n Enter the radius : ");
   scanf("%d", &r);
   printf("\n Enter the centre as X and Y : ");
   scanf("%d%d", &xc,&yc);

   x = 0;
   y = r;
   P = 1 - r;

   do
   {
     putpixel( x,  y, WHITE);
     putpixel( y,  x, WHITE);
     putpixel(-x, -y, WHITE);
     putpixel(-y, -x, WHITE);
     putpixel(-x,  y, WHITE);
     putpixel( y, -x, WHITE);
     putpixel( x, -y, WHITE);
     putpixel(-y,  x, WHITE);

     if(P < 0)
     {
       x = x + 1 ;
       P = P + (2 * x) + 1 ;
     }
     else
     {
       x = x + 1 ;
       y = y - 1 ;
       P = P + (2 * x) - (2 * y) + 1 ;
     }
  }while(x < y);

    getch();
    closegraph();

}

The problem is that the output seems to go in the top left corner and I can't see the whole output. Is there any way I can set the output to show up at specific area of my screen or something like that?

Screen shot of my current output:

SCREENSHOT_OF_OUTPUT

EDIT: After the points suggested by LPs and koldewb, I re-wrote the whole code and now, I'm getting output in center, I mean, the output is much better. I added different color to see which quadrant was plotted... Here is the code :-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
   int gd = DETECT , gm ;
   int r , x , y , xc , yc  , P;

   initgraph(&gd , &gm ,"..//bgi") ;

   printf("\n Enter the radius : ") ;
   scanf("%d" , &r) ;
   printf("\n Enter the centre as X and Y : ") ;
   scanf("%d%d" , &xc,&yc) ;

   x = 0 ;
   y = r ;
   P = 1-r ;

   do
  {
     putpixel(xc+x,yc+y,WHITE) ;
     putpixel(yc+y,xc+x,WHITE) ;
     putpixel(xc-x,yc-y,WHITE) ;
     putpixel(yc-y,xc-x,WHITE) ;
     putpixel(xc-x,yc+y,WHITE) ;
     putpixel(yc+y,xc-x,WHITE) ;
     putpixel(xc+x,yc-y,WHITE) ;
     putpixel(yc-y,xc+x,WHITE) ;

     if(P<0)
     {
       x = x + 1 ;
       P = P + (2*x) + 1 ;
     }
     else
     {
       x = x + 1 ;
       y = y - 1 ;
       P = P + (2*x) - (2*y) + 1 ;
     }
     } while(x<y) ;

     getch() ;
     closegraph() ;
  }

I'm not sure if this would be of any help to anyone, But, well, I might as well post the updated code.

Screen shot of my Updated output:

SCREENSHOT_OF_OUTPUT


Solution

User inserts coos of circle center into xc an yc variables, but your code always start with x=0 and y=r.

I cannot test the code, but probably you can add xc and yc offset each time you calculate a new x and y.



Answered By - LPs
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing