荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: playboy (为了钱,努力!), 信区: Program
标  题: 直线裁剪
发信站: BBS 荔园晨风站 (Wed Jan 12 08:49:43 2000), 转信


#include<graphics.h>
#include<stdlib.h>
#include <stdio.h>
#include <conio.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
#define Startx1 380
#define Startx2 250
#define Starty1 250
#define Starty2 150
#define ML 200
#define MR 400
#define MB 100
#define MT 400
#define Esc 27
#define Speed 8

int c_s_line(int ,int ,int ,int );

main()
{
  int driver=DETECT,mode;
  char ch=0;
  int x1,y1,x2,y2;

  x1=x2=y1=y2=0;
  initgraph(&driver,&mode,"F:\\tURBOc3");
  setcolor(WHITE);
  rectangle(ML-1,MT+1,MR+1,MB-1);
  setcolor(BLUE);
  gotoxy(1,1);
  printf("Press Esc to Exit!");
  gotoxy(1,2);
  printf("Press Direction to Move");
  c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
  while(ch!=Esc)
  {
switch(ch)
    {
      case 0x48:setcolor(BLACK);
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                setcolor(BLUE);
                y1-=Speed;
                y2-=Speed;
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                break;
      case 0x50:setcolor(BLACK);
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                setcolor(BLUE);
                y1+=Speed;
                y2+=Speed;
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                break;
      case 0x4b:setcolor(BLACK);
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                setcolor(BLUE);
                x1-=Speed;
                x2-=Speed;
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                break;
      case 0x4d:setcolor(BLACK);
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                setcolor(BLUE);
                x1+=Speed;
                x2+=Speed;
                c_s_line(Startx1+x1,Starty1+y1,Startx2+x2,Starty2+y2);
                break;
    }
    ch=getch();
  }
  closegraph();
}
int encode(int x,int y)
{
  int c=0;

  if(x<ML)c|=LEFT;
  else if(x>MR)c|=RIGHT;
  if(y<MB)c|=BOTTOM;
  else if(y>MT)c|=TOP;
  return c;
}

int c_s_line(int x1,int y1,int x2,int y2)
{
  int c1,c2,c;
  float x,y,dx,dy;
  c1=encode(x1,y1);
  c2=encode(x2,y2);
  if(!(c1&c2))
    if(c1==c2)
      line(x1,y1,x2,y2);
    else
    {
      c=c1;
      if(c==0)c=c2;
      x=(c==c1)?x1:x2;
      y=(c==c1)?y1:y2;
      if(c&LEFT)
      {
        x=ML;
        y=y1+(float)(y2-y1)*(ML-x1)/(x2-x1);
      }
      else if(c&RIGHT)
      {
        x=MR;
        y=y1+(float)(y2-y1)*(MR-x1)/(x2-x1);
      }
      else if(c&BOTTOM)
      {
        x=x1+(float)(x2-x1)*(MB-y1)/(y2-y1);
        y=MB;
      }
      else if(c&TOP)
      {
        x=x1+(float)(x2-x1)*(MT-y1)/(y2-y1);
        y=MT;
      }
      if(c==c1){
        x1=(double)x;
        y1=(double)y;
      }
      else
      {
        x2=(double)x;
        y2=(double)y;
      }
      c_s_line(x1,y1,x2,y2);
    }
}

--
※ 来源:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 192.168.1.90]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店