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. error launching installer-最新版Win 10 解决方案

    error 提示 error launching installer遇到 error的背景 楼主最近重新装了Windows 10 pro 64 bit 版,安装的时候选的地区是United State ...

  2. 软件工程_1st weeks

    本周为软件工程课的第一周,本周主要完成了三个工作:了解了github并使用.拜读了<构建之法>并开通了博客以及完成了四则运算的代码实现. 对于第一项工作github的安装和使用,花费了5个 ...

  3. oracle小知识点

    一 . procedure和function: procedure和function在语法上几乎完全一样,使用上却有小小的差别, procedure可以单独的调用 在命令行直接exec pro_xxx ...

  4. Mysql 悲观锁

    转载:http://chenzhou123520.iteye.com/blog/1860954 悲观锁介绍: 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处 ...

  5. caffe实现多任务学习

    Github: https://github.com/Haiyang21/Caffe_MultiLabel_Classification Blogs  1. 采用多label的lmdb+Slice L ...

  6. mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

    mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

  7. codeforces439B

    Devu, the Dumb Guy CodeForces - 439B Devu is a dumb guy, his learning curve is very slow. You are su ...

  8. 训练题(代码未检验)(序列前k大和问题)

    大厦 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission ...

  9. Maven概述(一)

    Maven是什么? Apache Maven is a software project management and comprehension tool. Based on the concept ...

  10. Linux定时器crontab的使用

    参数 Usage: crontab [参数] 文件 crontab [参数] crontab -n [主机名] Options: -u <user> 定义用户 -e 编辑工作表 -l 列出 ...