using System;
 using System.Drawing;
 using System.Windows.Forms;

 namespace CutLine
 {
     static class Program
     {
         [STAThread]
         static void Main()
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
         }
     }
     static public class ai
     {
         static byte code(Point p, Rectangle rec)
         {
             ;
             ;
             ;
             ;
             ;
             return ans;
         }
         static public void run(Point from, Point to, Rectangle rec)
         {
             byte f = code(from, rec);
             byte t = code(to, rec);
             )//绝对无交集,两点在同一侧
             {
                 nothing = true;
                 return;
             }
             )//两点都在内部
             {
                 ai.from = from;
                 ai.to = to;
                 nothing = false;
                 return;
             }
             )
             {
                 change(ref to,ref from,ref rec,ref t);
             }
             else{
                 change(ref from,ref  to, ref rec,ref f);
             }
             run(from, to, rec);
         }
         static void change(ref Point move, ref Point stay, ref Rectangle rec,ref byte c)
         {
             ) != )
             {
                 move.X =(int)( (move.X - stay.X) *(double) (rec.Y - stay.Y) / (move.Y - stay.Y) + stay.X);
                 move.Y = rec.Y;
             }
             ) != )
             {
                 move.X = (int)((double)(move.X - stay.X) / (move.Y - stay.Y) * (rec.Y + rec.Height - stay.Y) + stay.X);
                 move.Y = rec.Y + rec.Height;
             }
             ) != )
             {
                 move.Y = (int )((double)(move.Y - stay.Y) / (move.X - stay.X) * (rec.X - stay.X) + stay.Y);
                 move.X = rec.X;
             }
             ) != )
             {
                 move.Y = (int)((double)(move.Y - stay.Y) / (move.X - stay.X) * (rec.X +rec.Width- stay.X) + stay.Y);
                 move.X = rec.X+rec.Width;
             }
         }
         ,);
         ,);
         public static bool nothing = true;
     }
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
             Text = "直线裁剪--made by weidiao.neu";
         }

         ;
         Point from = new Point();
         Point to = new Point();
         Point temp = new Point();
         Rectangle rec = new Rectangle();

         private void button1_Click(object sender, EventArgs e)
         {
             CreateGraphics().Clear(Color.AliceBlue);
             times = ;
         }
         bool bigger(Point a, Point b)
         {
             return a.X >= b.X && b.X >= b.Y;
         }
         int min(int a, int b)
         {
             if(a<b)return a;
             return b;
         }
         Pen linePen = );
         Pen recPen = );
         override protected void OnMouseMove(MouseEventArgs e)
         {
              || times == ) return;
             Bitmap bit = new Bitmap(ClientSize.Width, ClientSize.Height);
             Graphics g = Graphics.FromImage(bit);
             g.Clear(Color.AliceBlue);
             switch (times)
             {
                 : g.DrawLine(linePen, from, e.Location);
                     break;
                 : g.DrawLine(linePen, from, to);
                     rec.X = min(temp.X, e.X);
                     rec.Y = min(temp.Y, e.Y);
                     rec.Width = Math.Abs(temp.X - e.X);
                     rec.Height = Math.Abs(temp.Y - e.Y);
                     g.DrawRectangle(recPen, rec);
                     break;
             }
             CreateGraphics().DrawImage(bit, , );
         }
         override  protected void OnMouseDown(MouseEventArgs e)
         {
             switch (times)
             {
                 :
                     button1_Click(null, null);
                     from.X = e.X;
                     from.Y = e.Y;
                     break;
                 : to.X = e.X;
                     to.Y = e.Y;
                     CreateGraphics().DrawLine(linePen, from, to);
                     break;
                 : temp.X = e.X;
                     temp.Y = e.Y;
                     break;
                 :
                     rec.X = min(temp.X, e.X);
                     rec.Y = min(temp.Y, e.Y);
                     rec.Width = Math.Abs(temp.X - e.X);
                     rec.Height = Math.Abs(temp.Y - e.Y);
                     CreateGraphics().DrawRectangle(recPen, rec);
                     ai.run(from, to, rec);
                     if(ai.nothing==false)
                     CreateGraphics().DrawLine(), ai.from, ai.to);
                     break;
             }
             times++;
             times %= ;
         }
     }
 }

java

 package mySecondPackage;

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.BufferedImage;

 import javax.swing.*;

 /*
  * 上下左右
  * 0000
  * 用4位二进制表示
  * 上边之外为1,也就是上边之上为1
  * 下边之外为1,也就是下边之下为1
  * 左边之外为1,也就是左边之左为1
  * 右边之外为1,也就是右边之右为1
  * */
 /**
  * @author weidiao
  * @see hahaha
  * @version 2.0
  */

 class ai {
     /**
      * 按照上下左右的顺序进行编码
      * */
     static byte code(Point p, Rectangle rec) {
         byte ans = 0;
         if (p.y < rec.y)// 如果点p在长方形的上边
             ans |= 1;
         else if (p.y > rec.y + rec.height)// 如果点p在长方形的下边
             ans |= 2;
         if (p.x < rec.x)// 如果点p在长方形左边
             ans |= 4;
         else if (p.x > rec.x + rec.width)// 如果点p在长方形右边
             ans |= 8;
         return ans;
     }

     /**
      * @算法的主体,进行判断和循环
      * @param 输入参数
      *            :Point from,to描述直线 Rectangle rec描述矩形
      * @return输出参数:返回为空,因为Point from和to是传引用,所以就直接改变了
      *                          类的静态变量nothing反映了是否产生了结果,若无交点,nothing=true
      */
     static public void run(Point from, Point to, Rectangle rec) {
         while (true) {
             byte f = code(from, rec);
             byte t = code(to, rec);// 对两个点进行编码

             if ((f & t) != 0)// 绝对无交集,两点在矩形某条边同一侧
             {
                 nothing = true;//
                 return;
             }
             if ((f | t) == 0)// 两点都在矩形内部
             {
                 nothing = false;
                 return;
             }
             if (f == 0)
                 change(to, from, rec, t);
             else
                 change(from, to, rec, f);

         }
     }

     /**
      * @本函数用于求直线与矩形边所在直线的交点
      * @param Point
      *            move表示准备移动的那个点,Point stay表示不移动的点 Rectangle rec表示矩形 Byte c
      *            表示准备移动的那个点的编码
      * @return 被移动的点move值将会发生变化
      * @难点在于:计算时要用浮点数进行计算,直接用int误差太大
      */
     static void change(Point move, Point stay, Rectangle rec, byte c) {
         if ((c & 1) != 0) {
             move.x = (int) ((move.x - stay.x) * (double) (rec.y - stay.y)
                     / (move.getY() - stay.y) + stay.x);
             move.y = rec.y;
         } else if ((c & 2) != 0) {
             move.x = (int) ((double) (move.x - stay.x) / (move.y - stay.y)
                     * (rec.y + rec.height - stay.y) + stay.x);
             move.y = rec.y + rec.height;
         } else if ((c & 4) != 0) {
             move.y = (int) ((double) (move.y - stay.y) / (move.x - stay.x)
                     * (rec.x - stay.x) + stay.y);
             move.x = rec.x;
         } else if ((c & 8) != 0) {
             move.y = (int) ((double) (move.y - stay.y) / (move.x - stay.x)
                     * (rec.x + rec.width - stay.x) + stay.y);
             move.x = rec.x + rec.width;
         }
     }

     public static boolean nothing = true;
 }

 /**
  * @CutLine类,界面部分
  * */
 class CutLine extends JFrame {
     public static void main(String[] args) {
         new CutLine();
     }

     public CutLine() {
         setTitle("直线裁剪--made by weidiao.neu");
         setSize(700, 700);
         setVisible(true);
         setLayout(null);
         setDefaultCloseOperation(EXIT_ON_CLOSE);

         JButton clearButton = new JButton("清屏");
         clearButton.setFont(new Font("楷体", Font.BOLD, 50));
         clearButton.addActionListener(clearButtonClick);
         clearButton.setLocation(500, 500);
         clearButton.setSize(200, 200);
         add(clearButton); 

         addMouseListener(mouse);
         addMouseMotionListener(mouseMotion);

     }

     int times = 0;
     Point from = new Point();
     Point to = new Point();
     Point temp = new Point();// 用于存储矩形左上角的位置
     Rectangle rec = new Rectangle();

     ActionListener clearButtonClick = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             getGraphics().clearRect(0, 0, getWidth(), getHeight());
             times = 0;
         }
     };

     MouseListener mouse = new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
             switch (times) {
             case 0:
                 clearButtonClick.actionPerformed(null);
                 from = e.getPoint();
                 break;
             case 1:
                 to = e.getPoint();
                 getGraphics().drawLine(from.x, from.y, to.x, to.y);
                 break;
             case 2:
                 temp = e.getPoint();
                 break;
             case 3:
                 rec.x = Integer.min(temp.x, e.getX());
                 rec.y = Integer.min(temp.y, e.getY());
                 rec.width = Math.abs(temp.x - e.getX());
                 rec.height = Math.abs(temp.y - e.getY());
                 getGraphics().drawRect(rec.x, rec.y, rec.width, rec.height);
                 ai.run(from, to, rec);
                 if (ai.nothing == false) {
                     Graphics2D g = (Graphics2D) getGraphics();
                     g.setStroke(new BasicStroke(4));
                     g.setColor(Color.red);
                     g.drawLine(from.x, from.y, to.x, to.y);
                 }
                 break;
             }
             times++;
             if (times == 4)
                 times = 0;
         }
     };
     MouseMotionListener mouseMotion = new MouseMotionAdapter() {
         public void mouseMoved(MouseEvent e) {
             if (times == 0 || times == 2)
                 return;
             getGraphics().clearRect(0, 0, getWidth(), getHeight());

             BufferedImage bit = new BufferedImage(getWidth(), getHeight(),
                     BufferedImage.TYPE_INT_ARGB_PRE);
             Graphics2D g = (Graphics2D) bit.getGraphics();
             switch (times) {
             case 1:
                 g.setColor(Color.blue);
                 g.setStroke(new BasicStroke(3));
                 g.drawLine(from.x, from.y, e.getX(), e.getY());
                 break;
             case 3:
                 g.setColor(Color.blue);
                 g.setStroke(new BasicStroke(3));
                 g.drawLine(from.x, from.y, to.x, to.y);
                 rec.x = Integer.min(temp.x, e.getX());
                 rec.y = Integer.min(temp.y, e.getY());
                 rec.width = Math.abs(temp.x - e.getX());
                 rec.height = Math.abs(temp.y - e.getY());
                 g.drawRect(rec.x, rec.y, rec.width, rec.height);
                 break;
             }
             getGraphics().drawImage(bit, 0, 0, null);
         }
     };

     boolean bigger(Point a, Point b) {
         return a.x >= b.x && b.x >= b.y;
     }
 }

C# Cut Line Bressenham Algorithm的更多相关文章

  1. DDA, Bresenham line's algorithm and Voxel Traversal used in the Grid-Accelerator in PBRT

        - DDA(Digital Differential Analyzer, 数值微分法) -    计算机图形学中,经常会遇到一些计算机中”经典“的问题.例如,如何利用计算机”离散“的特质,模拟 ...

  2. 【A Global Line Matching Algorithm for 2D Laser Scan Matching in Regular Environment】

    只看了前面的部分,灭有看实验,觉得整体风格比较傻白甜,与我的想法不谋而合.简单明了,用起来应该比较方便. 初步探测:如果有直线,就给线性插值一下. 分级聚类:利用简单的阈值给聚类了一下,分成了段段. ...

  3. POJ 1921 Paper Cut(计算几何の折纸问题)

    Description Still remember those games we played in our childhood? Folding and cutting paper must be ...

  4. Line Search and Quasi-Newton Methods 线性搜索与拟牛顿法

    Gradient Descent 机器学习中很多模型的参数估计都要用到优化算法,梯度下降是其中最简单也用得最多的优化算法之一.梯度下降(Gradient Descent)[3]也被称之为最快梯度(St ...

  5. Line Search and Quasi-Newton Methods

    Gradient Descent 机器学习中很多模型的参数估计都要用到优化算法,梯度下降是其中最简单也用得最多的优化算法之一.梯度下降(Gradient Descent)[3]也被称之为最快梯度(St ...

  6. hdu3982 直线切多边形 【WA中...】

    题意:有一块蛋糕,上面有一颗cherry.用刀子切n次,求切完之后有cherry的那部分的面积 My solution: 先做一个大矩形,使cake内切于这个大矩形.如图: 然后不断切这个大矩形,每次 ...

  7. Hough Transform

    Hough Transform Introduction: The Hough transform is an algorithm that will take a collection of poi ...

  8. [位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15

    NOIP2014 提高组模拟试题 第一试试题 题目概况: 中文题目名称 合理种植 排队 科技节 源程序文件名 plant.pas/.c/.cpp lineup.pas/.c/.cpp scifest. ...

  9. RichEdit 各个版本介绍

    RichEdit是开发中经常使用到的控件,其版本自1.0起,历经好几年,好几次的更新,在此引用一篇介绍RichEdit版本的博文(http://blogs.msdn.com/b/murrays/arc ...

随机推荐

  1. proteus 运行出错,用户名不可使用中文!

    仿真的时候提示如图提示 cannot open ’c\user\小名\AppData\local\temp\LISA0089.sdf’ 系统用户名不能是中文! 解决办法:重新建立个账户,记得用英文命名 ...

  2. WinMain与wWinMain,win32的字符集问题

    刚接触win32开发的会发现,用vs新建一个win32项目默认代码里面的入口函数并不是WinMain而是_tWinMain或者wWinMain,这些都有什么区别呢? WinMain对应的是多字节字符集 ...

  3. Android中ListView错位布局实现(无聊向)

    由于某些原因,需要个错位的页面,在网上找不到好的例子,试着动手写了写. 不考虑配色的完成图如下: 首先考虑的是,listview每一行左右都有可能缩进. 先假设一行的布局就是ImageView,Tex ...

  4. ViewPager+Fragment再探:和TAB滑动条一起三者结合

    Fragment前篇: <Android Fragment初探:静态Fragment组成Activity> ViewPager前篇: <Android ViewPager初探:让页面 ...

  5. 【4412嵌入式开发板学习笔记】认识uboot

    转自迅为讨论群:http://www.topeetboard.com 重要说明:这份笔记不是4412开发配套的,是我在网上看视频的时候下载上课老师的笔记后修改的.所以我试了一下笔记上的uboot命令, ...

  6. MMORPG大型游戏设计与开发(客户端架构 part15 of vegine)

    一个接口需要统一的派生接口,这样做的好处在于能够统一的进行管理.我所知的脚本语言中,接口有多重接口,也还有所谓的虚基类,这些都是方便类的管理.在vengine(微引擎)中,统一的的接口管理为kerne ...

  7. 怎样用ZBrush中的shadowbox创建物体

    ZBrush一直以方便著称业内各领域,模型师不需要在多个软件中编辑塑造模型,而在ZBrush用shadowbox创建物体的流程,在Tool面板上的Geometry子面板中,4.0版本引入了shadow ...

  8. 三维网格分割算法(Random Walks)

    首先以一维随机游走(1D Random Walks)为例来介绍下随机游走(Random Walks)算法,如下图所示,从某点出发,随机向左右移动,向左和向右的概率相同,都为1/2,并且到达0点或N点则 ...

  9. php一句话后门过狗姿势万千之传输层加工(1)

    写在前面:过狗相关的资料网上也是有很多,所以在我接下来的文章中,可能观点或者举例可能会与网上部分雷同,或者表述不够全面.但是我只能说,我所传达给大家的信息,是我目前所掌握或者了解的,不能保证所有人都会 ...

  10. 后缀自动机&序列自动机综合

    好像序列自动机还没有写过- 串长为n的串共有n+1个节点,除了串中的n个节点,还有一个空的根节点放在串首.每个节点至多有26条出边,每条边连向它之后的第一个字符. 串中的任意一个子序列对应了一条根到某 ...