?? elipse.bak
字號:
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include<dos.h>
#include<math.h>
#define ROUND(a) (int)(a+0.5)
#define CIRCLE 1
#define ELIPSE 2
#define FILL 3
#define EXIT 4
#define bool int
#define Color int
//this is list for stack
void FillContiguousSpan(int x, int y, Color bound, Color fill, int *xLeft, int *xRight);
class node{
int x,y;
node *next;
public:
node(int a,int b){x=a;y=b;}
void setnext(node * a){next = a;}
node* getnext(){return next;}
getx(){return x;}
gety(){return y;}
};
class stack{
node *index;
public:
stack(){index=NULL;}
void push(int x,int y);
bool pop(int *a,int *b);
};
void stack::push(int x,int y)
{
if(index==NULL){index = new node(x,y);}
else
{
node *t=new node(x,y);
t->setnext(index);
index=t;
}
}
bool stack::pop(int *a,int *b)
{
if(index==NULL)return 0;
else
{
*a=index->getx();
*b=index->gety();
node* t=index;
index=index->getnext();
delete t;
return 1;
}
}
stack st;
// pixel setter and getter
void SetPixel(int x, int y, int c){putpixel(x,y,c);}
int GetPixel(int x, int y){return getpixel(x,y);}
// this algorithm uses a global stack of pixel coordinates
void pushSeed(int x, int y)
{
st.push(x,y);
}
bool popSeed(int *x, int *y) // returns false iff stack was empty
{
return st.pop(x,y);
}
// the main routine
void FillSeedsOnStack(Color bound, Color fill)
{
Color col1, col2;
int x, y; // current seed pixel
int xLeft, xRight; // current span boundary locations
int i;
while (popSeed(&x, &y)) {
if (GetPixel(x, y) != bound) {
FillContiguousSpan(x, y, bound, fill, &xLeft, &xRight);
// single pixel spans handled as a special case in the else clause
if (xLeft != xRight) {
// handle the row above you
y++;
for(i=xLeft+1; i<=xRight; i++) {
col1 = GetPixel(i-1, y);
col2 = GetPixel(i, y);
if (col1 != bound && col1 != fill && col2 == bound)
pushSeed(i-1, y);
}
if (col2 != bound && col2 != fill)
pushSeed(xRight, y);
// handle the row below you
y -= 2;
for(i=xLeft+1; i<=xRight; i++) {
col1 = GetPixel(i-1, y);
col2 = GetPixel(i, y);
if (col1 != bound && col1 != fill && col2 == bound)
pushSeed(i-1, y);
}
if (col2 != bound && col2 != fill)
pushSeed(xRight, y);
} else {
col1 = GetPixel(xLeft, y+1);
col2 = GetPixel(xLeft, y-1);
if (col1 != fill)
pushSeed(xLeft, y+1);
if (col2 != fill)
pushSeed(xLeft, y-1);
}
} // end if (GetPixel)
} // end while (popSeed)
}
// fill pixels to the left and right of the seed pixel until you hit
// boundary pixels. Return the locations of the leftmost and rightmost
// filled pixels.
void FillContiguousSpan(int x, int y, Color bound, Color fill, int *xLeft, int *xRight)
{
Color col;
int i;
// fill pixels to the right until you reach a boundary pixel
i = x;
col = GetPixel(i, y);
while(col != bound) {
SetPixel(i, y, fill);
i++;
col = GetPixel(i, y);
}
*xRight = i-1;
// fill pixels to the left until you reach a boundary pixel
i = x-1;
col = GetPixel(i, y);
while(col != bound) {
SetPixel(i, y, fill);
i--;
col = GetPixel(i, y);
}
*xLeft = i+1;
}
void FloodFill(int x, int y, Color bound, Color fill)
{
PushSeed(x, y);
FillSeedsOnStack(bound, fill);
}
//////////////mouse
int MouseInbox(int x,int y,int x1,int y1,int x2,int y2)
{
if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
return 1;
return 0;
}
int chk_mouse(void)
{
REGS r;
r.x.ax=0x0000;
int86(0x33,&r,&r);
return r.x.ax;
}
//mouse function
void show_mouse(void)
{
REGS r;
r.x.ax=0x0001;
int86(0x33,&r,&r);
}
//mouse function
void hide_mouse(void)
{
REGS r;
r.x.ax=0x0002;
int86(0x33,&r,&r);
}
//mouse function
void get_mouse(int *x1,int *y1)
{
REGS r;
r.x.ax=0x0003;
int86(0x33,&r,&r);
*x1=r.x.cx;
*y1=r.x.dx;
}
//mouse function
int mouse_pressed(int btn_no)
{
REGS r;
r.x.ax=0x0005;
r.x.bx=btn_no;
int86(0x33,&r,&r);
return r.x.bx;
}
//mouse function
int mouse_released(int btn_no)
{
REGS r;
r.x.ax=0x0006;
r.x.bx=btn_no;
int86(0x33,&r,&r);
return r.x.bx;
}
//////////////////////
void Clear_Drawing_Screen()
{
setfillstyle(1, BLACK);
bar(101,0,getmaxx(),getmaxy());
}
void lineBres(int xa,int ya,int xb,int yb)
{
if(xa>101&&ya>1&&xb>101&&yb>1)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
if(xa>xb)
{
x=xb; y=yb; xEnd=xa;
}
else
{
x=xa; y=ya; xEnd=xb;
}
putpixel(x,y,1);
while(x<xEnd)
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,1);
}
}
}
void ellipsePlotPoints(float xCenter,float yCenter,float x,float y)
{
putpixel(xCenter+x,yCenter+y,1);
putpixel(xCenter-x,yCenter+y,1);
putpixel(xCenter+x,yCenter-y,1);
putpixel(xCenter-x,yCenter-y,1);
}
void ellipseMidPoint(float xCenter,float yCenter,float Rx,float Ry)
{
{
float Rx2=Rx*Rx;
float Ry2=Ry*Ry;
float twoRx2=2*Rx2;
float twoRy2=2*Ry2;
float p;
float x=0;
float y=Ry;
float px=0;
float py=twoRx2*y;
ellipsePlotPoints(xCenter,yCenter,x,y);
p = ROUND((Ry2-Rx2*Ry)+(0.25 * Rx2));
while(px<py)
{
x++;
px+=twoRy2;
if(p<0)
p+=Ry2+px;
else
{
y--;
py-=twoRx2;
p+=Ry2+px-py;
}
ellipsePlotPoints(xCenter,yCenter,x,y);
}
p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
while(y>0)
{
y--;
py-=twoRx2;
if(p>0)
p+=Rx2-py;
else
{
x++;
px+=twoRy2;
p+=Rx2-py+px;
}
ellipsePlotPoints(xCenter,yCenter,x,y);
}
}
}
void CirclePlotPoints(int xCenter,int yCenter,int x,int y)
{
putpixel(xCenter+x,yCenter+y,WHITE);
putpixel(xCenter-x,yCenter+y,WHITE);
putpixel(xCenter+x,yCenter-y,WHITE);
putpixel(xCenter-x,yCenter-y,WHITE);
putpixel(xCenter+y,yCenter+x,WHITE);
putpixel(xCenter-y,yCenter+x,WHITE);
putpixel(xCenter+y,yCenter-x,WHITE);
putpixel(xCenter-y,yCenter-x,WHITE);
}
void CircleMidpoint(int xCenter, int yCenter, int radius)
{
int x=0;
int y = radius;
int p=1-radius;
CirclePlotPoints(xCenter,yCenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
CirclePlotPoints(xCenter,yCenter,x,y);
// delay(1000);
}
}
void InterFace()
{
settextstyle(1, HORIZ_DIR , 2);
bar(90,1,100,getmaxy());
setcolor(1);
bar(10,120,80,150);
outtextxy(20,120,"Circle");
bar(10,160,80,190);
outtextxy(20,160,"Elipse");
bar(10,200,80,230);
outtextxy(20,200,"Line");
bar(10,240,80,270);
outtextxy(20,240,"Clear");
bar(10,280,80,310);
outtextxy(20,280,"Exit");
}
void main()
{
int a=DETECT,b;
int x=0,y=0,x1=10,y1=10,x2=0,y2=0,dif_x=0,dif_y=0;
int WhichShape=0;
initgraph(&a,&b,"c:\\tc\\bgi");
chk_mouse();
show_mouse();
InterFace();
while(!kbhit())
{
if(mouse_pressed(0))
{
get_mouse(&x1,&y1);
while(!mouse_released(0))
get_mouse(&x2,&y2);
}
if(x1<0)x1=2;
if(y1<0)y1=2;
// if(x1>101&&y1>1&&x2<101&&y2<1)
//{
if(MouseInbox(x2,y2,10,120,80,150))
{
WhichShape=CIRCLE;
Clear_Drawing_Screen();
}
else if(MouseInbox(x2,y2,10,160,80,190))
{
Clear_Drawing_Screen();
WhichShape=ELIPSE;
}
else if(MouseInbox(x2,y2,10,200,80,230))
{
WhichShape=LINE;
Clear_Drawing_Screen();
}
else if(MouseInbox(x2,y2,10,240,80,270))
{
Clear_Drawing_Screen();
}
else if(MouseInbox(x2,y2,10,280,80,310))
exit(0);
else if(!(MouseInbox(x2,y2,0,0,101,479))&&
!(MouseInbox(x1,y1,0,0,101,479)))
{
x=(x1+x2)/2;
y=(y1+y2)/2;
dif_x=abs(x2-x);
dif_y=abs(y2-y);
switch (WhichShape)
{
case CIRCLE:
{
CircleMidpoint(x,y,dif_x);
break;
}
case ELIPSE:
//if(x1>100&&y1>1&&x2<101&&y2<1)
{
ellipseMidPoint(x,y,dif_x,dif_y);
break;
}
case LINE:
//if(x1>101&&y1>1&&x2<101&&y2<1)
lineBres(x1,y1,x2,y2);
break;
}//end switch
}//end if
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -