1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 5 namespace CutLine 6 { 7 static class Program 8 { 9 [STAThread] 10 static void Main() 11 { 12 Application.EnableVisualStyles(); 13 Application.SetCompatibleTextRenderingDefault(false); 14 Application.Run(new Form1()); 15 } 16 } 17 static public class ai 18 { 19 static byte code(Point p, Rectangle rec) 20 { 21 byte ans=0; 22 if (p.Y < rec.Y) ans |= 1; 23 if (p.Y > rec.Y + rec.Height) ans |= 2; 24 if (p.X < rec.X) ans |= 4; 25 if (p.X > rec.X + rec.Width) ans |= 8; 26 return ans; 27 } 28 static public void run(Point from, Point to, Rectangle rec) 29 { 30 byte f = code(from, rec); 31 byte t = code(to, rec); 32 if ((f & t) != 0)//绝对无交集,两点在同一侧 33 { 34 nothing = true; 35 return; 36 } 37 if ((f | t)== 0)//两点都在内部 38 { 39 ai.from = from; 40 ai.to = to; 41 nothing = false; 42 return; 43 } 44 if (f == 0) 45 { 46 change(ref to,ref from,ref rec,ref t); 47 } 48 else{ 49 change(ref from,ref to, ref rec,ref f); 50 } 51 run(from, to, rec); 52 } 53 static void change(ref Point move, ref Point stay, ref Rectangle rec,ref byte c) 54 { 55 if ((c & 1) != 0) 56 { 57 move.X =(int)( (move.X - stay.X) *(double) (rec.Y - stay.Y) / (move.Y - stay.Y) + stay.X); 58 move.Y = rec.Y; 59 } 60 else if ((c & 2) != 0) 61 { 62 move.X = (int)((double)(move.X - stay.X) / (move.Y - stay.Y) * (rec.Y + rec.Height - stay.Y) + stay.X); 63 move.Y = rec.Y + rec.Height; 64 } 65 else if ((c & 4) != 0) 66 { 67 move.Y = (int )((double)(move.Y - stay.Y) / (move.X - stay.X) * (rec.X - stay.X) + stay.Y); 68 move.X = rec.X; 69 } 70 else if ((c & 8) != 0) 71 { 72 move.Y = (int)((double)(move.Y - stay.Y) / (move.X - stay.X) * (rec.X +rec.Width- stay.X) + stay.Y); 73 move.X = rec.X+rec.Width; 74 } 75 } 76 public static Point from = new Point(10,10); 77 public static Point to = new Point(40,40); 78 public static bool nothing = true; 79 } 80 public partial class Form1 : Form 81 { 82 public Form1() 83 { 84 InitializeComponent(); 85 Text = "直线裁剪--made by weidiao.neu"; 86 } 87 88 int times = 0; 89 Point from = new Point(); 90 Point to = new Point(); 91 Point temp = new Point(); 92 Rectangle rec = new Rectangle(); 93 94 private void button1_Click(object sender, EventArgs e) 95 { 96 CreateGraphics().Clear(Color.AliceBlue); 97 times = 0; 98 } 99 bool bigger(Point a, Point b) 100 { 101 return a.X >= b.X && b.X >= b.Y; 102 } 103 int min(int a, int b) 104 { 105 if(a<b)return a; 106 return b; 107 } 108 Pen linePen = new Pen(Color.Red, 5); 109 Pen recPen = new Pen(Color.Purple, 2); 110 override protected void OnMouseMove(MouseEventArgs e) 111 { 112 if (times == 0 || times == 2) return; 113 Bitmap bit = new Bitmap(ClientSize.Width, ClientSize.Height); 114 Graphics g = Graphics.FromImage(bit); 115 g.Clear(Color.AliceBlue); 116 switch (times) 117 { 118 case 1: g.DrawLine(linePen, from, e.Location); 119 break; 120 case 3: g.DrawLine(linePen, from, to); 121 rec.X = min(temp.X, e.X); 122 rec.Y = min(temp.Y, e.Y); 123 rec.Width = Math.Abs(temp.X - e.X); 124 rec.Height = Math.Abs(temp.Y - e.Y); 125 g.DrawRectangle(recPen, rec); 126 break; 127 } 128 CreateGraphics().DrawImage(bit, 0, 0); 129 } 130 override protected void OnMouseDown(MouseEventArgs e) 131 { 132 switch (times) 133 { 134 case 0: 135 button1_Click(null, null); 136 from.X = e.X; 137 from.Y = e.Y; 138 break; 139 case 1: to.X = e.X; 140 to.Y = e.Y; 141 CreateGraphics().DrawLine(linePen, from, to); 142 break; 143 case 2: temp.X = e.X; 144 temp.Y = e.Y; 145 break; 146 case 3: 147 rec.X = min(temp.X, e.X); 148 rec.Y = min(temp.Y, e.Y); 149 rec.Width = Math.Abs(temp.X - e.X); 150 rec.Height = Math.Abs(temp.Y - e.Y); 151 CreateGraphics().DrawRectangle(recPen, rec); 152 ai.run(from, to, rec); 153 if(ai.nothing==false) 154 CreateGraphics().DrawLine(new Pen(Color.Green, 8), ai.from, ai.to); 155 break; 156 } 157 times++; 158 times %= 4; 159 } 160 } 161 }
时间: 2024-10-27 05:57:48