private void GetDirection(ref MyPointDouble v, double xb, double yb, double xe, double ye) { v.x = xe - xb; v.y = ye - yb; }
private double ScalarPro(MyPointDouble u, MyPointDouble v) { return(u.x * v.x + u.y * v.y); }
private void Execute_Click(object sender, EventArgs e) { if (ConfigList.Items.Count < 3) { MessageBox.Show("Configuration does not have enough points!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (isNotConvex()) { if (MessageBox.Show("Please input the convex configuration!", "Warning", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Retry) { clear_Click(sender, e); } else { Close(); } return; } int n = ConfigList.Items.Count; foreach (MyLineSeg LineSeg in LineBox.Items) { MyPointDouble d = new MyPointDouble(); GetDirection(ref d, LineSeg.xb, LineSeg.yb, LineSeg.xe, LineSeg.ye); bool q = true; double tn = 0, tv = 1, t; for (int i = 0; i < n; i++) { int i1 = (i + 1) % n, i2 = (i + 2) % n; MyPoint _pi = (MyPoint)ConfigList.Items[i], _p1 = (MyPoint)ConfigList.Items[i1], _p2 = (MyPoint)ConfigList.Items[i2]; MyPointDouble P0 = new MyPointDouble(_pi.x, _pi.y), P1 = new MyPointDouble(_p1.x, _p1.y), P2 = new MyPointDouble(_p2.x, _p2.y); MyPointDouble _top = new MyPointDouble(), _top_1 = new MyPointDouble(); GetDirection(ref _top, P0.x, P0.y, P1.x, P1.y); GetDirection(ref _top_1, P0.x, P0.y, P2.x, P2.y); MyPointDouble normal = new MyPointDouble(); normal.y = 1; if (_top.x != 0) { normal.x = -_top.y / _top.x; } else { normal.y = 0; normal.x = 1; } if (ScalarPro(normal, _top_1) < 0) { normal.y *= -1; normal.x *= -1; } MyPointDouble Wi = new MyPointDouble(); GetDirection(ref Wi, P0.x, P0.y, LineSeg.xb, LineSeg.yb); double DS = ScalarPro(d, normal), WS = ScalarPro(Wi, normal); if (DS == 0) { if (WS < 0) { q = false; break; } } t = -WS / DS; if (DS > 0) { if (t <= 1) { tn = Math.Max(tn, t); } else { q = false; break; } } else { if (t >= 0) { tv = Math.Min(tv, t); } else { q = false; break; } } } if (q && tn <= tv) { MyLineSeg LSeg = new MyLineSeg(LineSeg.xb, LineSeg.yb, LineSeg.xe, LineSeg.ye); LSeg.xb = (int)Math.Round(LineSeg.xb + tn * (LineSeg.xe - LineSeg.xb)); LSeg.yb = (int)Math.Round(LineSeg.yb + tn * (LineSeg.ye - LineSeg.yb)); LSeg.xe = (int)Math.Round(LineSeg.xb + tv * (LineSeg.xe - LineSeg.xb)); LSeg.ye = (int)Math.Round(LineSeg.yb + tv * (LineSeg.ye - LineSeg.yb)); Pen penB0 = new Pen(Color.Red, 2), penB1 = new Pen(Color.White, 2), penB2 = new Pen(Color.Black, 2); Graphics g = Graphics.FromImage(view.Image); g.DrawLine(penB1, LineSeg.xb, LineSeg.yb, LineSeg.xe, LineSeg.ye); g.DrawLine(penB2, LineSeg.xb, LineSeg.yb, LSeg.xb, LSeg.yb); g.DrawLine(penB0, LSeg.xb, LSeg.yb, LSeg.xe, LSeg.ye); g.DrawLine(penB2, LSeg.xe, LSeg.ye, LineSeg.xe, LineSeg.ye); } } view.Refresh(); }