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. CentOS7.5下MYSQL8.0.11

    MYsql的安装 1. 下载bundles 包 https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-1.el7.x86_64.rpm-bun ...

  2. Centos7安装Splash

    前言 最近在用Scrapy抓取一个网站数据,其中有个页面需要执行js脚本才能加载,所以需要用到Splash. 官网地址:https://splash.readthedocs.io/en/stable/ ...

  3. codeforces604B

    More Cowbell CodeForces - 604B Kevin Sun wants to move his precious collection of n cowbells from Na ...

  4. pycharm同级目录导入问题

    在PyCharm2017中同目录下import其他模块,会出现No model named ...的报错,但实际可以运行 这是因为PyCharm不会将当前文件目录自动加入source_path. 在当 ...

  5. MT【210】四点共圆+角平分线

    (2018全国联赛解答最后一题)在平面直角坐标系$xOy$中,设$AB$是抛物线$y^2=4x$的过点$F(1,0)$的弦,$\Delta{AOB}$的外接圆交抛物线于点$P$(不同于点$A,O,B$ ...

  6. MT【204】离散型最值

    (联赛一试2006,14).将2006表示成5个正整数$x_1,x_2,x_3,x_4,x_5$之和.记$S=\sum\limits_{1\le i<j\le5}{x_ix_j}$问:(1) 当 ...

  7. 一种BCD码转换的算法

    #include "stdio.h" typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef ...

  8. 洛谷 P2491消防 解题报告

    P2491 消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个 ...

  9. pandas 从入门到遗忘

    读取大文件(内存有限): import pandas as pd reader = pd.read_csv("tap_fun_test.csv", sep=',', iterato ...

  10. scp 的用法

    scp用于在linux下远程拷贝文件, 与rsync相比,scp不占资源,不会提高多少系统负荷,虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本 ...