using System;

 using System.Collections.Generic;

 using System.ComponentModel;

 using System.Data;

 using System.Drawing;

 using System.Linq;

 using System.Text;

 using System.Windows.Forms;

 namespace cutLine

 {

     unsafe public partial class Form1 : Form

     {

         const int left = ;

         const int right = ;

         const int bottom = ;

         const int top = ;

         Point window1, window2;     //保存裁剪区矩形

         Point line1, line2;         //保存线段的两个端点坐标

         int start, x , y;           //标志点击次数,取值范围0,1,2

         Graphics g;                 //GDI 对象

         public Form1()

         {

             InitializeComponent();

             g = CreateGraphics();

             window1 = new Point(, );//裁剪区左下角

             window2 = new Point(, ); //裁剪区右上角

             start = ;

         }

         private void Form1_MouseClick(object sender, MouseEventArgs e)

         {

             int x = e.X, y = e.Y;    //记录鼠标点击位置:

             Pen p = new Pen(Brushes.Black, );

             switch (start)

             {

                 case :

                     {

                         g.Clear(Color.Silver);

                         Background();

                         start = ;

                         line1 = new Point(e.X, e.Y);

                         g.DrawRectangle(Pens.Black, x - , y - , , ); //画一个小方块,显示点击位置

                         break;

                     }

                 case :

                     {

                         start = ;

                         line2 = new Point(e.X, e.Y);

                         g.DrawRectangle(Pens.Black, x - , y - , , ); //画一个小方块,显示点击位置

                         g.DrawLine(p, line1, line2);

                         //DrawString_1();

                         cutLine(line1, line2, window1.X, window2.X, window1.Y, window2.Y);

                         break;

                     }

             }

         }

         private void Background()

         {

             if (start == )

                 g.DrawRectangle(Pens.Black, window1.X, window1.Y,window2.X - window1.X, window2.Y - window1.Y);

             DrawString();

         }

         private void DrawString()

         {

             String str;

             str = "                              黑色矩形为裁剪窗口\n                              点鼠标输入线段的两个点,自动执行裁剪\n                              红色为线段裁剪后的部分";     //临时字体对象              //画刷枚举     //位置

             g.DrawString(str, new Font("宋体", , FontStyle.Regular), Brushes.Black, , );

         }

         private void DrawString_1(int code1, int code2)

         {

             String str;

             g.FillRectangle (Brushes.White, , , , );

             str = "\n编码: "   Convert.ToString(code1, ).PadLeft(, ’’’’)   "  "   Convert.ToString(code2, ).PadLeft(, ’’’’);

             g.DrawString(str, new Font("宋体", , FontStyle.Regular), Brushes.Black, , );

         }

         private void Form1_Load(object sender, EventArgs e)

         {  

         }

         private void Form1_Paint(object sender, PaintEventArgs e)

         {

             Background();

         }

        private void Encode(int x, int y,int *code,int xl, int xr, int yb, int yt)

         {

             int c = ;

             if (x < xl)

                 c = c | left;

             else if (x > xr)

                 c = c | right;

             if (y < yb)

                 c = c | bottom;

             else if (y > yt)

                 c = c | top;

             (*code) = c;

         }

         public void cutLine(Point p1, Point p2, int xl, int xr, int yb, int yt)

         {

             Pen p = new Pen(Brushes.Red, );

             int x1, x2, y1, y2,code1, code2, code;

             x1 = p1.X; x2 = p2.X; y1 = p1.Y; y2 = p2.Y;

             Encode(x1,y1, &code1, xl,xr,yb,yt);

             Encode(x2,y2, &code2, xl,xr,yb,yt);

             DrawString_1(code1, code2);

             while (code1 !=  || code2 != )

             {

                 if ((code1 & code2) != ) return;

                 code = code1;

                 if (code1 == )

                     code = code2;

                 if ((left & code) != )//线段与左边界相交

                 {

                     x = xl;

                     y = y1   (y2 - y1) * (xl - x1) / (x2 - x1);

                 }

                 else if ((right & code) != )//线段与右边界相交

                 {

                     x = xr;

                     y = y1   (y2 - y1) * (xr - x1) / (x2 - x1);

                 }

                 else if ((bottom & code) != )//线段与下边界相交

                 {

                     y = yb;

                     x = x1   (x2 - x1) * (yb - y1) / (y2 - y1);

                 }

                 else if ((top & code) != )//线段与上边界相交

                 {

                     y = yt;

                     x = x1   (x2 - x1) * (yt - y1) / (y2 - y1);

                 }

                 if (code == code1)

                 {

                     x1 = x;

                     y1 = y;

                     Encode(x, y, &code1, xl, xr, yb, yt);

                 }

                 else

                 {

                     x2 = x;

                     y2 = y;

                     Encode(x, y, &code2, xl, xr, yb, yt);

                 }

             }

             p1.X = x1; p1.Y = y1;

             p2.X = x2; p2.Y = y2;

             g.DrawLine(p, p1, p2);

         }

     }

 }

C# 实现线段的编码裁剪算法(vs2010)的更多相关文章

  1. CGA裁剪算法之线段裁剪算法

    CGA裁剪算法之线段裁剪算法 常用的线段裁剪算法有三种:[1]Cohen_SutherLand裁剪算法,[2]中点分割裁剪算法,[3]参数化方法. 1. Cohen_SutherLand裁剪算法 为了 ...

  2. [图形学] 习题8.12 NLN二维线段裁剪算法实现

    Nicholl-Lee-Nicholl二维线段裁剪算法相对于Cohen-Sutherland和Liang-Barsky算法来说,在求交点之前进行了线段端点相对于几个区域的判断,可以确切的知道要求交点的 ...

  3. [图形学] Chp8.7.2 梁友栋-Barsky线段裁剪算法

    这节简单介绍了梁友栋-Barsky裁剪算法的原理,只有结论并没有过程,看过http://blog.csdn.net/daisy__ben/article/details/51941608这篇文章后,大 ...

  4. 理解Liang-Barsky裁剪算法的算法原理

    0.补充知识向量点积:结果等于0, 两向量垂直; 结果大于0, 两向量夹角小于90度; 结果小于0, 两向量夹角大于90度.直线的参数方程:(x1, y1)和(x2, y2)两点确定的直线, 其参数方 ...

  5. Liang-Barsky直线段裁剪算法

    Liang-Barsky直线段裁剪算法 梁友栋与Barsky提出的裁剪算法以直线的参数方程为基础,把判断直线段与窗口边界求交的 二维裁剪问题转化为求解一组不等式,确定直线段参数的一维裁剪问题.设起点为 ...

  6. DDos攻击,使用深度学习中 栈式自编码的算法

    转自:http://www.airghc.top/2016/11/10/Dection-DDos/ 最近研究了一篇论文,关于检测DDos攻击,使用了深度学习中 栈式自编码的算法,现在简要介绍一下内容论 ...

  7. [C++]哈夫曼树(最优满二叉树) / 哈夫曼编码(贪心算法)

    一 哈夫曼树 1.1 基本概念 算法思想 贪心算法(以局部最优,谋求全局最优) 适用范围 1 [(约束)可行]:它必须满足问题的约束 2 [局部最优]它是当前步骤中所有可行选择中最佳的局部选择 3 [ ...

  8. RMQ问题(线段树算法,ST算法优化)

    RMQ (Range Minimum/Maximum Query)问题是指: 对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在[i,j]里的最小(大)值 ...

  9. WebGIS裁剪算法-线裁剪多边形

    在gis系统中 经常会用到一些裁剪的方法,首先推荐一个非常好用的空间分析JavaScript库--Turf.js,不仅功能强大.使用简单,同时处理速度也很快. Turf.js中提供了一中多边形的裁剪方 ...

随机推荐

  1. LinkedList 实现 Queue

    package cn.com.example; import java.util.LinkedList; /** * Created by Jack on 2017/3/8. */ public cl ...

  2. Struts2 Intercepter 笔记

    以前一直对Struts2的自定义拦截器(intercepter)不是很清楚,今天仔细研究了下,终于搞懂了,现在记录对拦截器的总结如下: 1:自定义拦截器的作用一般就是用来实现系统权限控制比较多: 2: ...

  3. cordic——sincos

    phase format :scaled radians,归化到多少pi roundmode :nearest even 近似值 coarse rotation: selected-pi——pi.no ...

  4. python 抽象类与接口类

    几个类 实现的方法都一致的话 就继承同一个父类 在父类写一个公共方法 给子类使用

  5. MT【6】等比数列和的极限

    评:利用等比数列求和公式给出所求数列,再利用反证法证明唯一性.这种反证方法印象中本科高等代数里讲初等对称多项式时出现过.

  6. json序列化 & 反序列化

    json序列化: json的dumps方法可以将json格式数据序列为python的相关数据类型,比如str,常用于打印,另外,在序列化时,中文汉字被转换为unicode编码,在dumps函数中添加参 ...

  7. 面试 -- Http协议相关(转载)

    http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...

  8. bracketed-paste-magic:zle:41: not enough arguments for -U

    原因是zsh的插件出问题了,解法方法如下: 把 ~/.oh-my-zsh/lib/misc.zsh 文件中的第一段 if 注释掉 OK 啦 # ]]; then # for d in $fpath; ...

  9. SpaceVim的基本安装和常见问题

    SpaceVim官网:https://spacevim.org/ SpaceVim中文官网:http://spacevim.org/cn/ SpaceVim的Github页面:https://gith ...

  10. 【loj3054】【hnoi2019】鱼

    题目 描述 ​ 难以描述.......慢慢看..: ​ https://loj.ac/problem/3054 范围 ​ $6 \le n \le 1000  ,  1 \le |x| , |y| \ ...