📄 selipse.cpp
字号:
#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
//////////////mouse
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 fillarea(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;
void SetPixel(int x, int y, int c){putpixel(x,y,c);}
int GetPixel(int x, int y){return getpixel(x,y);}
void fillpixel(int x, int y)
{
st.push(x,y);
}
bool popSeed(int *x, int *y)
{
return st.pop(x,y);
}
void stackpixel(Color bound, Color fill)
{
Color col1, col2;
int x, y;
int xLeft, xRight;
int i;
while (popSeed(&x, &y)) {
if (GetPixel(x, y) != bound) {
fillarea(x, y, bound, fill, &xLeft, &xRight);
if (xLeft != xRight) {
y++;
for(i=xLeft+1; i<=xRight; i++) {
col1 = GetPixel(i-1, y);
col2 = GetPixel(i, y);
if (col1 != bound && col1 != fill && col2 == bound)
fillpixel(i-1, y);
}
if (col2 != bound && col2 != fill)
fillpixel(xRight, y);
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)
fillpixel(i-1, y);
}
if (col2 != bound && col2 != fill)
fillpixel(xRight, y);
} else {
col1 = GetPixel(xLeft, y+1);
col2 = GetPixel(xLeft, y-1);
if (col1 != fill)
fillpixel(xLeft, y+1);
if (col2 != fill)
fillpixel(xLeft, y-1);
}
}
}
}
void fillarea(int x, int y, Color bound, Color fill, int *xLeft, int *xRight)
{
Color col;
int i;
i = x;
col = GetPixel(i, y);
while(col != bound) {
SetPixel(i, y, fill);
i++;
col = GetPixel(i, y);
}
*xRight = i-1;
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)
{
fillpixel(x,y);
stackpixel(bound, fill);
}
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;
}
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,BLUE);
putpixel(xCenter-x,yCenter+y,BLUE);
putpixel(xCenter+x,yCenter-y,BLUE);
putpixel(xCenter-x,yCenter-y,BLUE);
putpixel(xCenter+y,yCenter+x,BLUE);
putpixel(xCenter-y,yCenter+x,BLUE);
putpixel(xCenter+y,yCenter-x,BLUE);
putpixel(xCenter-y,yCenter-x,BLUE);
}
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);
}
}
void InterFace()
{
settextstyle(1, 0 , 2);
setcolor(2);
fillellipse(100,100,50,25);
outtextxy(75,85,"Circle");
fillellipse(200,100,50,25);
outtextxy(175,85,"Elipse");
fillellipse(300,100,50,25);
outtextxy(275,85,"Fill");
fillellipse(400,100,50,25);
outtextxy(375,85,"Exit");
}
void Clear_Drawing_Screen()
{
setfillstyle(1, BLACK);
bar(25,25,getmaxx(),getmaxy());
setfillstyle(1, BLUE);
bar(0,0,80,23);
outtextxy(1,1,"BACK");
InterFace();
}
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>60&&y1>80&&x2<140&&y2<120)
{
Clear_Drawing_Screen();
WhichShape=CIRCLE;
}
else if(x1>160&&y1>80&&x2<240&&y2<120)
{
Clear_Drawing_Screen();
WhichShape=ELIPSE;
}
else if(x1>260&&y1>80&&x2<340&&y2<120)
{
WhichShape=FILL;
// Clear_Drawing_Screen();
}
else if(x1>360&&y1>80&&x2<440&&y2<120)
exit(0);
else if(x1>0&&y1>0&&x2<80&&y2<23)
Clear_Drawing_Screen();
else{
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:
ellipseMidPoint(x,y,dif_x,dif_y);
break;
case FILL:
FloodFill(x,y,BLUE,WHITE);
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -