裁剪算法

简介: 直线裁剪 program clipline;uses crt,graph;var   markseg:word;   gd,gm:integer;   startx,starty,endx,endy:integer;   x1,y1,x2,y2,x,y:integer;   ch:char;   ...

直线裁剪

program clipline;
uses crt,graph;
var
   markseg:word;
   gd,gm:integer;
   startx,starty,endx,endy:integer;
   x1,y1,x2,y2,x,y:integer;
   ch:char;
   readflag,enterflag:boolean;

   procedure lineclip(x1,y1,x2,y2,left,top,right,bottom:integer);
   type codetype=array[1..4] of byte;
   var
     x11,y11:integer;
     a,b:codetype;

     procedure getcode(x,y:integer;var c:codetype);
     begin
       c[1]:=0; c[2]:=0; c[3]:=0; c[4]:=0;
       if x<left then c[1]:=1;
       if x>right then c[2]:=1;
       if y>bottom then c[3]:=1;
       if y<top then c[4]:=1;
       {writeln(c[1],c[2],c[3],c[4])}
     end;

     procedure swap(var x,y:integer);
     var
       temp:integer;
     begin
       temp :=x;
       x :=y;
       y :=temp;
     end;

  begin
    getcode(x2,y2,b);
    repeat
      getcode(x1,y1,a);
      if (a[1]+a[2]+a[3]+a[4]=0) and (b[1]+b[2]+b[3]+b[4]=0) then
        exit
      else if (a[1] and b[1]+a[2] and b[2]+a[3] and b[3]+a[4] and b[4]<>0) then
      begin
        setcolor(0);
        setwritemode(copyput);
        line(x1,y1,x2,y2);
        exit;
      end
      else
      begin
        if a[1]+a[2]+a[3]+a[4]=0 then
        begin
          getcode(x1,y1,b);
          getcode(x2,y2,a);
          swap(x1,x2);
          swap(y1,y2);
        end;

        if (a[1]=1) then
        begin
          x11 :=left;
          y11 :=round((y2-y1)*(left-x1)/(x2-x1)+y1);
        end
        else if (a[2]=1) then
        begin
          x11 :=right;
          y11 :=round((y2-y1)*(right-x1)/(x2-x1)+y1);
        end
        else if (a[3]=1) then
        begin
          x11 :=round((x2-x1)*(bottom-y1)/(y2-y1)+x1);
          y11 :=bottom;
        end
        else
        begin
          x11 :=round((x2-x1)*(top-y1)/(y2-y1)+x1);
          y11 :=top;
        end;
      end;
      setcolor(0);
      setwritemode(copyput);
      line(x1,y1,x11,y11);
      x1 :=x11;
      y1 :=y11;
      getcode(x1,y1,a);
    until false;
    setcolor(white);
  end;

begin
  gd :=detect;
  directvideo :=false;
  initgraph(gd,gm,'D:\TP\BGI');
  readflag :=false;
  x1 :=50;  y1:=100;
  x2 :=100; y2 :=50;
  startx :=70;  starty :=30;  endx :=90;  endy :=100;
  line(x1,y1,x2,y2);
  rectangle(startx,starty,endx,endy);
{  lineclip(x1,y1,x2,y2,startx,starty,endx,endy);}
  readln;
  closegraph;
end.

圆的裁剪

#include <graphics.h>
#include <math.h>
int xc=240,yc=195,n=60,rx=120,ry=80,i,j,xL=100,xr=299,yt=100,yb=241,x1,y1;
float x,y,p=30.0,th,cp,sp;
main()
{
  int y=0;
  int driver=DETECT,mode=0;
  initgraph(&driver,&mode,"");
  setbkcolor(1);
  setcolor(14);
  p=p/57.3;  cp=cos(p);  sp=sin(p);
  rectangle(100,100,299,241);
  for (i=0;i<=n;i++)
  {
    th=6.2832/n;
    x=rx*cos(i*th);
    y=ry*sin(i*th);
    x1=xc+x*cp-y*sp;
    y1=yc-x*sp-y*cp;
    if (x1<xL || x1>xr || y1<yt || y1>yb)
      continue;
    j++;
    if (j==1) moveto(x1,y1);
    lineto(x1,y1);
  }
  getch();
  closegraph();
}

多边形裁剪:

/* Sutherland-Hodgman 算法 */
#define LEN sizeof(struct node)
#include <math.h>
#include<stdio.h>
#include<graphics.h>

struct node
{
int dx,dy;
struct node *next;
};
struct node *h,*q,*r;
struct node *creat()
{
int p[8][2]={100,120,160,50,180,100,200,80,240,160,210,220,170,160,140,190};
int i;
setcolor(12);
for (i=0;i<7;i++) line(p[i][0],p[i][1],p[i+1][0],p[i+1][1]);
line(p[0][0],p[0][1],p[7][0],p[7][1]);
rectangle(120,200,230,70);
h=NULL;
for (i=0;i<8;i++)
{
q=(struct node *)malloc(LEN);
q->dx=p[i][0];
q->dy=p[i][1];
if (h==NULL)
h=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(h);
}


int x;

struct node *builx(h,x)
struct node *h;

{
int s[2],j[2];
struct node *hh,*p,*q;

int max,min;
p=h; hh=NULL;
s[0]=p->dx; s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx)
{
max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{
q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL)
hh=q;
else
r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx; s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x; j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]);
max=s[0]; min=p->dx;
if (s[0]<p->dx) { max=p->dx; min=s[0]; }
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0]; q->dy=j[1];
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
if (p->dx>=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx; q->dy=p->dy;
if (hh==NULL) hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builxx(h,x)
struct node *h;
int x;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[0]=x;
j[1]=s[1]+(p->dy-s[1])*(x-s[0])/(p->dx-s[0]+0.1);
max=s[0];
min=p->dx;
if (s[0]<p->dx) { max=p->dx;
min=s[0];
}
if ((j[0]>=min)&&(j[0]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dx<=x)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *buily(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy>=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}

struct node *builyy(h,y)
struct node *h;
int y;
{int s[2],j[2];
struct node *hh,*p,*q;
int max,min;
p=h;
hh=NULL;
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
while (p!=NULL)
{
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
s[0]=p->dx;
s[1]=p->dy;
p=p->next;
}
p=h;
j[1]=y;
j[0]=s[0]+(p->dx-s[0])*(y-s[1])/(p->dy-s[1]+0.1);
max=s[1];
min=p->dy;
if (s[1]<p->dy) { max=p->dy;
min=s[1];
}
if ((j[1]>=min)&&(j[1]<=max))
{ q=(struct node *)malloc(LEN);
q->dx=j[0];
q->dy=j[1];
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
if (p->dy<=y)
{ q=(struct node *)malloc(LEN);
q->dx=p->dx;
q->dy=p->dy;
if (hh==NULL)
hh=q;
else r->next=q;
r=q;
}
r->next=NULL;
return(hh);
}
void Initialize(void)
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode," ");
setbkcolor(WHITE);
}
main()
{ int max,min;
struct node *head,*r,*q;
int i;
int s[2];
Initialize();
head=creat();
q=head;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,14);
q=q->next;
}
putpixel(q->dx,q->dy,14);
q=builx(head,120);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,15);
q=q->next;
}
putpixel(q->dx,q->dy,15);
q=buily(head,70);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,2);
q=q->next;
}
putpixel(q->dx,q->dy,2);
q=builxx(head,230);
head=q;
while (q->next!=NULL)
{ putpixel(q->dx,q->dy,1);
q=q->next;
}
putpixel(q->dx,q->dy,1);
q=builyy(head,200);
head=q;
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
setcolor(14);
while (q!=NULL)
{ line(s[0],s[1],q->dx,q->dy);
s[0]=q->dx;
s[1]=q->dy;
q=q->next;
}
q=head;
line(s[0],s[1],q->dx,q->dy);
getch();
}

相关文章
|
2月前
|
算法
多边形裁剪算法
多边形裁剪算法
|
2月前
|
算法 图形学
【计算机图形学】实验三 用Cohen-Sutherland裁剪算法实现直线段裁剪
【计算机图形学】实验三 用Cohen-Sutherland裁剪算法实现直线段裁剪
244 2
|
1天前
|
算法 索引
基于Prony算法的系统参数辨识matlab仿真
Prony算法在MATLAB2022a中用于信号分析,识别复指数信号成分。核心程序通过模拟信号X1,添加不同SNR的噪声,应用Prony方法处理并计算误差。算法基于离散序列的复指数叠加模型,通过构建矩阵并解线性方程组估计参数,实现LTI系统动态特性的辨识。
|
3天前
|
算法 安全 数据库
基于结点电压法的配电网状态估计算法matlab仿真
**摘要** 该程序实现了基于结点电压法的配电网状态估计算法,旨在提升数据的准确性和可靠性。在MATLAB2022a中运行,显示了状态估计过程中的电压和相位估计值,以及误差随迭代变化的图表。算法通过迭代计算雅可比矩阵,结合基尔霍夫定律解决线性方程组,估算网络节点电压。状态估计过程中应用了高斯-牛顿或莱文贝格-马夸尔特法,处理量测数据并考虑约束条件,以提高估计精度。程序结果以图形形式展示电压幅值和角度估计的比较,以及估计误差的演变,体现了算法在处理配电网状态估计问题的有效性。
|
3天前
|
算法
基于PSO粒子群优化的PID控制器参数整定算法matlab仿真
该文探讨了使用PSO(粒子群优化)算法优化PID控制器参数的方法。通过PSO迭代,不断调整PID控制器的Kp、Ki、Kd增益,以减小控制误差。文中提供了MATLAB2022a版本的核心代码,展示了参数优化过程及结果。系统仿真图像显示了参数随迭代优化的变化。PID控制器结合PSO算法能有效提升控制性能,适用于复杂系统的参数整定,未来研究可关注算法效率提升和应对不确定性。
|
3天前
|
算法
m基于GA遗传优化的高斯白噪声信道SNR估计算法matlab仿真
**MATLAB2022a模拟展示了遗传算法在AWGN信道中估计SNR的效能。该算法利用生物进化原理全局寻优,解决通信系统中复杂环境下的SNR估计问题。核心代码执行多代选择、重组和突变操作,逐步优化SNR估计。结果以图形形式对比了真实SNR与估计值,并显示了均方根误差(RMSE),体现了算法的准确性。**
10 0
|
5天前
|
机器学习/深度学习 存储 算法
基于SFLA算法的神经网络优化matlab仿真
**摘要:** 使用MATLAB2022a,基于SFLA算法优化神经网络,降低训练误差。程序创建12个神经元的前馈网络,训练后计算性能。SFLA算法寻找最优权重和偏置,更新网络并展示训练与测试集的预测效果,以及误差对比。SFLA融合蛙跳与遗传算法,通过迭代和局部全局搜索改善网络性能。通过调整算法参数和与其他优化算法结合,可进一步提升模型预测精度。
|
5天前
|
算法
基于仿射区间的分布式三相不对称配电网潮流算法matlab仿真
```markdown # 摘要 本课题聚焦于基于仿射区间的分布式三相配电网潮流算法在MATLAB2022a中的仿真。算法利用仿射运算处理三相不平衡情况及分布式电源注入,旨在提供比区间算法更精确的不确定区域。仿真结果展示了算法优势。核心程序设计考虑了PQ、PV及PI节点,将不同类型的节点转换统一处理,以适应含分布式电源的配电网潮流计算需求。 ``` 这个摘要以Markdown格式呈现,总字符数为233,满足了240字符以内的要求。
|
5天前
|
机器学习/深度学习 算法 数据可视化
基于googlenet深度学习网络的睁眼闭眼识别算法matlab仿真
**算法预览图展示睁眼闭眼识别效果;使用Matlab2022a,基于GoogLeNet的CNN模型,对图像进行分类预测并可视化。核心代码包括图像分类及随机样本显示。理论概述中,GoogLeNet以高效Inception模块实现眼部状态的深度学习识别,确保准确性与计算效率。附带三张相关图像。**
|
7天前
|
机器学习/深度学习 自然语言处理 算法
m基于深度学习的OFDM+QPSK链路信道估计和均衡算法误码率matlab仿真,对比LS,MMSE及LMMSE传统算法
**摘要:** 升级版MATLAB仿真对比了深度学习与LS、MMSE、LMMSE的OFDM信道估计算法,新增自动样本生成、复杂度分析及抗频偏性能评估。深度学习在无线通信中,尤其在OFDM的信道估计问题上展现潜力,解决了传统方法的局限。程序涉及信道估计器设计,深度学习模型通过学习导频信息估计信道响应,适应频域变化。核心代码展示了信号处理流程,包括编码、调制、信道模拟、降噪、信道估计和解调。
28 8