Giải thuật sinh đường Ellipse

Người đăng: share-nhungdieuhay on Thứ Năm, 29 tháng 8, 2013


#include <graphics.h>
#include <conio.h>
#define ROUND(a) ((long)(a+0.5))
void plot(int xc, int yc, int x, int y, int color){
      putpixel(xc+x, yc+y, color);
      putpixel(xc-x, yc+y, color);
      putpixel(xc+x, yc-y, color);
      putpixel(xc-x, yc-y, color);
}
void Mid_ellipse(int xc, int yc, int a, int b, int color){
     long x, y, fx, fy, a2, b2, p;
     x = 0;
     y = b;
     a2 = a * a;
     b2 = b * b;
     fx = 0;
     fy = 2 * a2 * y;  
     plot(xc, yc, x,y, color);
     p = ROUND(b2-(a2*b)+(0.25*a));
     while (fx < fy){
         x++;
         fx += 2*b2;
         if (p<0)
              p += b2*(2*x +3);
        else{
              y--;
              p+= b2*(2*x +3) + a2*(-2*y +2);
              fy -= 2*a2;
       }
       plot(xc, yc, x, y, color);
       }
       p = ROUND(b2*(x+0.5)*(x+0.5) + a2*(y-1)*(y-1) - a2*b2);
       while (y>0){
              y--;
              fy -= 2*a2;
              if (p>=0)
              p+=a2*(3 - 2*y);
             else{
                x++;
                fx += 2*b2;
                p += b2*(2*x+2) + a2*(-2*y +3);
            }
        plot(xc, yc, x, y, color);
       }
}
void main(){
       int gr_drive = DETECT, gr_mode;
       initgraph(&gr_drive, &gr_mode, "");
       Mid_Ellipse(getmaxx() / 2, getmaxy() / 2, 150, 80, 4);
       getch();
       closegraph();
}
More about

Các giải thuật sinh đường tròn trong C/C++

Người đăng: share-nhungdieuhay


1. Giải thuật sinh đường tròn Bresenham:
void Bre_circle(int xc, int yc, int Radius, int color) 
     int x, y, p; 
     x = 0; 
     y = Radius; 
     p = 3 - 2 * Radius; 
while (x <= y) 
     putpixel(xc + x, yc + y, color); 
     if (p < 0) 
           p += 4 * x + 6; 
     else 
           p += 4 * (x-y) + 10; 
           y--; 
     } 
     x++;} 
}
2. Giải thuật sinh đường tròn Midpoint :
void Mid_circle(int xc, int yc, int Radius, int color) 
    int x, y, d; 
    x = 0; 
    y = Radius; 
    d = 1- Radius; 
    while (x <= y) 
    putpixel(xc + x, yc + y, color); 
         if (d< 0) 
         d +=2 * x + 3; 
        else 
           d += 2 * (x-y) + 5; 
           y--; 
        } 
       x++; 
       } 
}
More about

Các thuật toán vẽ đoạn thẳng trong C/C++

Người đăng: share-nhungdieuhay on Thứ Bảy, 17 tháng 8, 2013


1. Thuật toán vẽ đoạn thẳng thông thường: 
void dline(int x1,int y1, int x2,int y2, int color) { 
   float y; 
   int x; 
   for (x=x1; x<=x2; x++) { 
         y = y1 + (x-x1)*(y2-y1)/(x2-x1) ; 
         putpixel(x, Round(y), color ); 
   } 

2. Thuật toán DDA (Digital Differential Analizer):
void ddaline (int x1,int y1,int x2,int y2,int c){ 
   int x=x1; 
   float y=y1; 
   float k=(float)(y2-y1)/(x2-x1); 
   putpixel(x,round(y),c); 
   for(int i=x1;i<=x2;i++) { 
       x++; 
       y=y+k; 
   putpixel(x,round(y),c); 
   } 

3. Thuật toán Bresenham
/*Thuat toan Bresenham ve dthang (0<k<1) */ 
void Bre_line(int x1, int y1, int x2, int y2, int c) 
{int x, y, dx, dy,p,const1,const2; 
     y = y1; 
    dx = x2 - x1; 
    dy = y2 - y1; 
    p = 2*dy - dx; 
    const1 = 2*dy; 
    const2 = 2*(dy-dx); 
    for (x=x1; x<=x2; x++) { 
           putpixel(x, y, c); 
           if (p < 0) 
               p += const1; // p=p + 2dy 
          else { 
               p +=const2; //p=p+2dy-2dx 
               y++; 
   } 

}
4. Thuật toán Trung điểm - Midpoint 
/* Thuat toan Midpoint de ve doan thang  (0<k<1) */ 
void Mid_line(int x1, int y1, int x2, int y2, int c){ 
        int x, y, dx, dy,d; 
        y = y1; 
       dx = x2 - x1; 
       dy = y2 - y1; 
       d= dy - dx/2; 
       for (x=x1; x<=x2; x++){ 
           putpixel(x, y, c); 
           if (d <= 0) 
                d = d + dy; 
          else { 
                y ++; 
               d = d + dy - dx; 
          }} 
}

More about

Code C-C++: Bài toán Tháp Hà Nội

Người đăng: share-nhungdieuhay on Chủ Nhật, 4 tháng 8, 2013


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

#define MAX 12
#define BegPos   105
#define AuxPos   305
#define EndPos   505

int width;
typedef struct disc
  {
char val1[MAX];
char top,pos;
};

void push(disc *tt,int x);
pop(disc *tt);
void tower(int,disc *,disc *,disc *);
void draw_stack(disc *beg,disc *,disc *);
int main(void)
{

   int gdriver = DETECT, gmode, errorcode;
   int i,x=2;
   disc beg,end,aux;
   printf("

TOWER OF HANOI
");
   printf("=======================================================");
   printf("

How Many Disks[1-10]:-  ");
   scanf("%d",&x);

   initgraph(&gdriver, &gmode, "d:\TC\BGI");
   errorcode = graphresult();
   if (errorcode != grOk)
   {
 printf("Graphics error: %s
", grapherrormsg(errorcode));
 printf("Press any key to halt:");
 getch();
 exit(1);
   }
width=50/x;

beg.top=end.top=aux.top=0;
beg.pos=1;end.pos=3;aux.pos=2;

for(i=0;i<x;i++)
push(&beg,(x-i)+1);

draw_stack(&beg,&end,&aux);
tower(x,&beg,&end,&aux);

   closegraph();
   return 0;
}
void tower(int n,disc *beg,disc *aux,disc *end)
{
if(n>0)
/* {
push(end,pop(beg));
draw_stack(beg,end,aux);
}
else*/
{
tower(n-1,beg,end,aux);
push(end,pop(beg));
draw_stack(beg,end,aux);
tower(n-1,aux,beg,end);
}
//
}
void push(disc *tt,int x)
{
tt->val1[tt->top]=x;
tt->top++;
}

pop(disc *tt)
{
int a;
tt->top--;
a=tt->val1[tt->top];
tt->val1[tt->top]=0;
return a;
}

void draw_stack(disc *beg,disc *end,disc *aux)
{
int ypos=295,i,height=10,xpos;
int ver=0;
cleardevice();

setfillstyle(1,2);
bar(20,300,580,310);

bar(100,100,110,300);
bar(300,100,310,300);
bar(500,100,510,300);

rectangle(20,300,580,310);

rectangle(100,100,110,300);
rectangle(300,100,310,300);
rectangle(500,100,510,300);

/* END TOWER*/
ypos=295;
if(end->pos==1)
xpos=BegPos;
else if(end->pos==2)
xpos=AuxPos;
else if(end->pos==3)
xpos=EndPos;

for(i=0;i<end->top;i++)
{
setfillstyle(end->val1[i],end->val1[i]);

bar(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height);

rectangle(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height);
ypos-=(height+2);
}
ver=end->pos;

/* BEG TOWER*/
if(beg->pos==1)
xpos=BegPos;
else if(beg->pos==2)
xpos=AuxPos;
else if(beg->pos==3)
xpos=EndPos;

ypos=295;
for(i=0;i<beg->top;i++)
{
setfillstyle(beg->val1[i],beg->val1[i]);

bar(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height);

rectangle(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height);
ypos-=(height+2);
}

/* AUX TOWER*/
ver=ver*10+beg->pos;

if(ver<20)
{
if(ver%10==2)
xpos=EndPos;
else
xpos=AuxPos;
}
else if(ver<30)
{
if(ver%10==1)
xpos=EndPos;
else
xpos=BegPos;
}
else if(ver<40)
{
if(ver%10==1)
xpos=AuxPos;
else
xpos=BegPos;
}

ypos=295;
for(i=0;i<aux->top;i++)
{
setfillstyle(aux->val1[i],aux->val1[i]);

bar(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height);

rectangle(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height);
ypos-=(height+2);
}
getch();
}
More about