求解决计算机图形学中多边形的生成(任意及多边形)的生成代码
求解决计算机图形学中多边形的生成(任意及多边形)的生成代码最好是在TC中实现的,谢谢各位大师了回答的好还会加分的!!(我等级在那,不用担心!)...
求解决计算机图形学中多边形的生成(任意及多边形)的生成代码
最好是在TC中实现的,谢谢各位大师了
回答的好还会加分的!!
(我等级在那,不用担心!) 展开
最好是在TC中实现的,谢谢各位大师了
回答的好还会加分的!!
(我等级在那,不用担心!) 展开
1个回答
展开全部
struct Polygon{ /*多边形存储结构*/
int color;
int n; /*顶点个数*/
int *v; /*存储顶点各顶点坐标的数组指针*/
struct Polygon * next;
};
struct Polygon *pol;//以链表形式存储所有画出的多边形
void DPoly() /*点击鼠标左键画多边形,点击右键自动完成多边形最后一条边的封闭*/
{
int x,y,headx,heady,oldx,oldy,preX,preY;
/*(headx,heady)表示多边形第一顶点,(oldx,oldy)表示画边时鼠标所经过的前一个点,
(preX,preY)表示当前所画边的第一顶点,(x,y)是当前鼠标位置,count是点击次数*/
int button,count=0,flag; /* flag值用于返回当前点击区域在绘图区(1值)或调色板区(2值)还是按钮区(0值)*/
struct Polygon * last=NULL,* current=NULL;
if(pol==NULL)
{
pol=current=(struct Polygon *)malloc(sizeof(struct Polygon)); /*申请头节点*/
pol->next=NULL;
}
else
{
current=pol->next;
while(current!=NULL)
current=current->next;
}
setwritemode(XOR_PUT);
getmousexy(&oldx,&oldy);
Draw_c_mouse(oldx,oldy);
while(1)
{
getmousebutton(&button);
getmousexy(&x,&y);
switch(button)
{
case LEFT_PRESSED:
do{
getmousebutton(&button);
}while(button!=NO_PRESSED); /*由于按键一次将被读取不止一次,做此循环直至当点击完毕时开始画线*/
if(count==1)
{
last=(struct Polygon*)malloc(sizeof(struct Polygon));
last->color=getcolor();
last->n=1;
last->v=(int *)malloc(3*sizeof(int));
preX=headx=last->v[1]=x; /*从v[1]开始记录顶点*/
preY=heady=last->v[2]=y;
line(headx,heady,preX,preY);
}
else
{
++last->n;
last->v=(int *)realloc(last->v,(2*last->n+1)*sizeof(int));
preX=last->v[2*last->n-1]=x; preY=last->v[2*last->n]=y;
}
oldx=x; oldy=y;
break;
case RIGHT_PRESSED:
do{
getmousebutton(&button);
}while(button!=NO_PRESSED); /*由于按键一次将被读取不止一次,做此循环直至当点击完毕时开始画线*/
if(count>1)
{
line(preX,preY,x,y);
line(headx,heady,preX,preY);
}
count=0;
current->next=last;
current=last;
current->next=NULL;
break;
case NO_PRESSED:
if(oldx!=x || oldy!=y)
{
if(count>=1)/*若成立,表示已开始画多边形*/
{
line(preX,preY,oldx,oldy); /*覆盖*/
line(preX,preY,x,y);
}
Draw_c_mouse(oldx,oldy);
Draw_c_mouse(x,y);
DisplayXY(x,y);
oldx=x;oldy=y;
}
break;
}
}
}
//*************************************************
//***********TC下调用鼠标中断的函数****************
//*************************************************
#include "graphics.h"
#include<dos.h>
#include<stdlib.h>
#define ENTER 23
#define ESC 27
#define NO_PRESSED 0
#define LEFT_PRESSED 1
#define RIGHT_PRESSED 2
void Draw_c_mouse(int x,int y) /* 画十字鼠标 */
{
int color;
color=getcolor();
setcolor(YELLOW);
line(x-8,y,x+8,y);
line(x,y-8,x,y+8);
line(x,y,x,y);
setcolor(color);
}
void mouse(int *m1,int *m2,int *m3,int *m4)
{
union REGS inregs,outregs;
inregs.x.ax=*m1;
inregs.x.bx=*m2;
inregs.x.cx=*m3;
inregs.x.dx=*m4;
int86(0x33,&inregs,&outregs);
*m1=outregs.x.ax;
*m2=outregs.x.bx;
*m3=outregs.x.cx;
*m4=outregs.x.dx;
}
void resetmouse(void)
{
int m1,m2,m3,m4;
m1=0;
mouse(&m1,&m2,&m3,&m4);
}
void showmouse(void)
{
int m1,m2,m3,m4;
m1=1;
mouse(&m1,&m2,&m3,&m4);
}
void hidemouse(void)
{
int m1,m2,m3,m4;
m1=2;
mouse(&m1,&m2,&m3,&m4);
}
void set_mouse_limit(int x1,int x2,int xy)
{
int m1,m2,m3,m4;
if (xy==0) m1=7; else m1=8;
m3=x1;m4=x2;
mouse(&m1,&m2,&m3,&m4);
}
void getmousexy(int *x,int *y)
{
int m1,m2;
m1=3;
mouse(&m1,&m2,x,y);
if(getmaxx()==319) (*x)/=2;
}
void getmousebutton(int *button)
{
int m1,m3,m4;
m1=3;
*button=0;
mouse(&m1,button,&m3,&m4);
}
void set_mouse_xy(int x,int y)
{
int m1,m2,m3,m4;
m1=4;m3=x;m4=y;
if(getmaxx()==319) x*=2;
mouse(&m1,&m2,&m3,&m4);
}
void DisplayXY(int x,int y)//显示鼠标当前坐标
{
char buffer[20];
int color=getcolor();
settextstyle(0,HORIZ_DIR,0);
setfillstyle(0,0);
bar(93,getmaxy()-15,180,getmaxy()-8);
setcolor(YELLOW);
sprintf(buffer,"X=%d,Y=%d",x,y);
outtextxy(93,getmaxy()-15,buffer);
setcolor(color);
}
int color;
int n; /*顶点个数*/
int *v; /*存储顶点各顶点坐标的数组指针*/
struct Polygon * next;
};
struct Polygon *pol;//以链表形式存储所有画出的多边形
void DPoly() /*点击鼠标左键画多边形,点击右键自动完成多边形最后一条边的封闭*/
{
int x,y,headx,heady,oldx,oldy,preX,preY;
/*(headx,heady)表示多边形第一顶点,(oldx,oldy)表示画边时鼠标所经过的前一个点,
(preX,preY)表示当前所画边的第一顶点,(x,y)是当前鼠标位置,count是点击次数*/
int button,count=0,flag; /* flag值用于返回当前点击区域在绘图区(1值)或调色板区(2值)还是按钮区(0值)*/
struct Polygon * last=NULL,* current=NULL;
if(pol==NULL)
{
pol=current=(struct Polygon *)malloc(sizeof(struct Polygon)); /*申请头节点*/
pol->next=NULL;
}
else
{
current=pol->next;
while(current!=NULL)
current=current->next;
}
setwritemode(XOR_PUT);
getmousexy(&oldx,&oldy);
Draw_c_mouse(oldx,oldy);
while(1)
{
getmousebutton(&button);
getmousexy(&x,&y);
switch(button)
{
case LEFT_PRESSED:
do{
getmousebutton(&button);
}while(button!=NO_PRESSED); /*由于按键一次将被读取不止一次,做此循环直至当点击完毕时开始画线*/
if(count==1)
{
last=(struct Polygon*)malloc(sizeof(struct Polygon));
last->color=getcolor();
last->n=1;
last->v=(int *)malloc(3*sizeof(int));
preX=headx=last->v[1]=x; /*从v[1]开始记录顶点*/
preY=heady=last->v[2]=y;
line(headx,heady,preX,preY);
}
else
{
++last->n;
last->v=(int *)realloc(last->v,(2*last->n+1)*sizeof(int));
preX=last->v[2*last->n-1]=x; preY=last->v[2*last->n]=y;
}
oldx=x; oldy=y;
break;
case RIGHT_PRESSED:
do{
getmousebutton(&button);
}while(button!=NO_PRESSED); /*由于按键一次将被读取不止一次,做此循环直至当点击完毕时开始画线*/
if(count>1)
{
line(preX,preY,x,y);
line(headx,heady,preX,preY);
}
count=0;
current->next=last;
current=last;
current->next=NULL;
break;
case NO_PRESSED:
if(oldx!=x || oldy!=y)
{
if(count>=1)/*若成立,表示已开始画多边形*/
{
line(preX,preY,oldx,oldy); /*覆盖*/
line(preX,preY,x,y);
}
Draw_c_mouse(oldx,oldy);
Draw_c_mouse(x,y);
DisplayXY(x,y);
oldx=x;oldy=y;
}
break;
}
}
}
//*************************************************
//***********TC下调用鼠标中断的函数****************
//*************************************************
#include "graphics.h"
#include<dos.h>
#include<stdlib.h>
#define ENTER 23
#define ESC 27
#define NO_PRESSED 0
#define LEFT_PRESSED 1
#define RIGHT_PRESSED 2
void Draw_c_mouse(int x,int y) /* 画十字鼠标 */
{
int color;
color=getcolor();
setcolor(YELLOW);
line(x-8,y,x+8,y);
line(x,y-8,x,y+8);
line(x,y,x,y);
setcolor(color);
}
void mouse(int *m1,int *m2,int *m3,int *m4)
{
union REGS inregs,outregs;
inregs.x.ax=*m1;
inregs.x.bx=*m2;
inregs.x.cx=*m3;
inregs.x.dx=*m4;
int86(0x33,&inregs,&outregs);
*m1=outregs.x.ax;
*m2=outregs.x.bx;
*m3=outregs.x.cx;
*m4=outregs.x.dx;
}
void resetmouse(void)
{
int m1,m2,m3,m4;
m1=0;
mouse(&m1,&m2,&m3,&m4);
}
void showmouse(void)
{
int m1,m2,m3,m4;
m1=1;
mouse(&m1,&m2,&m3,&m4);
}
void hidemouse(void)
{
int m1,m2,m3,m4;
m1=2;
mouse(&m1,&m2,&m3,&m4);
}
void set_mouse_limit(int x1,int x2,int xy)
{
int m1,m2,m3,m4;
if (xy==0) m1=7; else m1=8;
m3=x1;m4=x2;
mouse(&m1,&m2,&m3,&m4);
}
void getmousexy(int *x,int *y)
{
int m1,m2;
m1=3;
mouse(&m1,&m2,x,y);
if(getmaxx()==319) (*x)/=2;
}
void getmousebutton(int *button)
{
int m1,m3,m4;
m1=3;
*button=0;
mouse(&m1,button,&m3,&m4);
}
void set_mouse_xy(int x,int y)
{
int m1,m2,m3,m4;
m1=4;m3=x;m4=y;
if(getmaxx()==319) x*=2;
mouse(&m1,&m2,&m3,&m4);
}
void DisplayXY(int x,int y)//显示鼠标当前坐标
{
char buffer[20];
int color=getcolor();
settextstyle(0,HORIZ_DIR,0);
setfillstyle(0,0);
bar(93,getmaxy()-15,180,getmaxy()-8);
setcolor(YELLOW);
sprintf(buffer,"X=%d,Y=%d",x,y);
outtextxy(93,getmaxy()-15,buffer);
setcolor(color);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询