private void RemoveNodeButton_Click(object sender, EventArgs e) { ResetEventHandler(); layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.Other; IPosition nearestPoint = new Position_Connected_Edit(0, 0); PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate(); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate); MouseMovedRealPosition = delegate(IPosition position) { positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint); nearestPoint = m2m_NN.NearestNeighbor(position); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); layer.SpringLayerRepresentationChangedEvent(layer); layersPaintedControl.Invalidate(); }; layer.VisibleInOptDlg = false; layer.Active = true; layer.Name = "Edited Figure"; layer.SquareFrameDrawer.RectangleRadiusWidth = 8; layer.SquareFrameDrawer.LineWidth = 1; layer.SquareFrameDrawer.LineColor = Color.Red; layer.CenterPointCoordinate.Visible = true; layer.CenterPointCoordinate.CoordinateFont = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold); if (currentLayer != null) { layers.Remove(currentLayer); } currentLayer = layer; layers.Add(layer); MouseLBottonCheckRealPosition = delegate(IPosition position) { //((IPositionSetEdit)selectedPositionSetEdit).RemovePosition(nearestPoint); if (selectedPositionSetEdit is IPositionSet_ConnectedEdit && nearestPoint is IPosition_Connected) { ((IPositionSet_ConnectedEdit)selectedPositionSetEdit).RemovePosition_Connected((IPosition_Connected)nearestPoint); } else if (selectedPositionSetEdit is IPositionSetEdit) { ((IPositionSetEdit)selectedPositionSetEdit).RemovePosition(nearestPoint); } else { throw new Exception(error_position); } selectedPositionSetLayer.SpringLayerRepresentationChangedEvent(selectedPositionSetLayer); m2m_NN.Remove(nearestPoint); layersPaintedControl.Invalidate(); }; }
private void SelectBotton_Click(object sender, EventArgs e) { layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle; ResetEventHandler(); //layersPainterForm.state_PainterForm_MouseDown = LayersPainterForm.State_PainterForm_MouseDown.Other; IPosition nearestPoint = new Position_Point(0, 0); PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate(); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate); MouseMovedRealPosition = delegate(IPosition position) { positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint); nearestPoint = m2m_NN.NearestNeighbor(position); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); layer.SpringLayerRepresentationChangedEvent(layer); layersPaintedControl.Invalidate(); }; layer.VisibleInOptDlg = false; layer.Active = true; layer.Name = "Edited Figure"; layer.SquareFrameDrawer.RectangleRadiusWidth = 8; layer.SquareFrameDrawer.LineWidth = 1; layer.SquareFrameDrawer.LineColor = Color.YellowGreen; layer.CenterPointCoordinate.Visible = true; layer.CenterPointCoordinate.CoordinateFont = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold); //layer.Point.IsDrawPointBorder = true; //layer.Point.PointColor = Color.Red; //layer.Point.PointRadius = 3; //layer.PointCoordinate.CoordinateFont = new Font(layer.PointCoordinate.CoordinateFont, FontStyle.Bold); //layer.PointCoordinate.Visible = true; if (currentLayer != null) { layers.Remove(currentLayer); } currentLayer = layer; layers.Add(layer); }
private void ShowLine() { IWriteLineInGridEngine writeLineInGridEngine = new Line2Block(); //IWriteLineInGridEngine writeLineInGridEngine = new Line2Block(); IPositionSet line = writeLineInGridEngine.WriteLineInGrid(configurationForWriteLineInGridAlgorithm.GridWidth, configurationForWriteLineInGridAlgorithm.GridHeight, configurationForWriteLineInGridAlgorithm.StartPosition, configurationForWriteLineInGridAlgorithm.EndPosition); painterDialog.Clear(); painterDialog.Layers.Add(new Layer_Grid(10, 10, configurationForWriteLineInGridAlgorithm.GridWidth, configurationForWriteLineInGridAlgorithm.GridHeight, 0, 0)); IPositionSetEdit startAndEnd = new PositionSetEdit_ImplementByICollectionTemplate(); startAndEnd.AddPosition(configurationForWriteLineInGridAlgorithm.StartPosition); startAndEnd.AddPosition(configurationForWriteLineInGridAlgorithm.EndPosition); painterDialog.HoldOnMode(); painterDialog.SetShowModeToForm(); if (line != null) { Layer_PositionSet_Point layer = new Layer_PositionSet_Point(line); layer.Point.PointRadius = 2; layer.Point.PointColor = Color.Blue; painterDialog.Layers.Add(layer); } Layer_PositionSet_Path layer2 = new Layer_PositionSet_Path((IPositionSet)startAndEnd); layer2.PathLine.LineWidth = 2; layer2.PathLine.LineColor = Color.Red; painterDialog.Layers.Add(layer2); painterDialog.Show(); }
/* * public SimplePositionSet ConvexHull(SimplePositionSet ps) * { * int n = ps.GetCount(); * Point[] pts = new Point[n]; * for (int i = 0; i < n; i++) * { * SimplePosition pos = ps.GetNextPosition(); * pts[i] = new Point(pos.GetX(), pos.GetY()); * } * Point[] resPts = _ConvexHull(pts); * SimplePositionSet result = new SimplePositionSet(); * for (int i = 0; i < resPts.Length; i++) * result.AddPosition(new SimplePosition(resPts[i].X, resPts[i].Y)); * return result; * } */ public IPositionSet ConvexHull(IPositionSet ps) { //转换成数组 IPosition[] posArr = (IPosition[])ps.ToArray(); int N = posArr.Length; //排序 Polysort.Quicksort(posArr); IPosition left = posArr[0]; IPosition right = posArr[N - 1]; // Partition into lower hull and upper hull CDLL lower = new CDLL(left), upper = new CDLL(left); for (int i = 0; i < N; i++) { double det = Area2(left, right, posArr[i]); if (det > 0) { upper = upper.Append(new CDLL(posArr[i])); } else if (det < 0) { lower = lower.Prepend(new CDLL(posArr[i])); } } lower = lower.Prepend(new CDLL(right)); upper = upper.Append(new CDLL(right)).Next; // Eliminate points not on the hull eliminate(lower); eliminate(upper); // Eliminate duplicate endpoints 消除重复点 /* * if (lower.Prev.val.Equals(upper.val)) * lower.Prev.Delete(); * if (upper.Prev.val.Equals(lower.val)) * upper.Prev.Delete(); * */ // Join the lower and upper hull IPosition[] res = new IPosition[lower.Size() + upper.Size()]; lower.CopyInto(res, 0); upper.CopyInto(res, lower.Size()); PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); for (int i = 0; i < res.Length - 1; i++) { result.AddPosition(res[i]); } return(result); }
private void OnGetQueryPart(ILevel level, int levelSequence, IPart part) { lock (layers) { PositionSetEdit_ImplementByICollectionTemplate partSet = new PositionSetEdit_ImplementByICollectionTemplate(); partSet.AddPosition(part); Layer_M2MPartSetInSpecificLevel layer = new Layer_M2MPartSetInSpecificLevel(level, partSet); layer.MainColor = Color.FromArgb(255, 0, 0); layer.Active = true; layers.Add(layer); } flowControlerForm.BeginInvoke(Update); flowControlerForm.SuspendAndRecordWorkerThread(); }
private PositionSetEdit_ImplementByICollectionTemplate getPoints2(Bitmap bmp) { PositionSetEdit_ImplementByICollectionTemplate ps = new PositionSetEdit_ImplementByICollectionTemplate(); for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { if (bmp.GetPixel(i, j).Name != "ffffffff") { ps.AddPosition(new Position_Point(i, j)); } } } return(ps); }
private void OnGetQueryPosition(IPosition position) { lock (layers) { IPositionSetEdit temp = new PositionSetEdit_ImplementByICollectionTemplate(); temp.AddPosition(position); Layer_PositionSet_Point queryPositionLayer = new Layer_PositionSet_Point(temp); queryPositionLayer.Point.PointRadius = 3; queryPositionLayer.Point.PointColor = Color.IndianRed; queryPositionLayer.Point.IsDrawPointBorder = true; queryPositionLayer.Active = true; layers.Add(queryPositionLayer); } flowControlerForm.BeginInvoke(Update); flowControlerForm.SuspendAndRecordWorkerThread(); }
public IPositionSet CollisionDetection(PositionSetEditSet bgPositionSet, IPositionSetEdit objPositionSet) { PreProcess(bgPositionSet); float delta = 0; while (true) { IPositionSetEdit obj = new PositionSetEdit_ImplementByICollectionTemplate(); objPositionSet.InitToTraverseSet(); while (objPositionSet.NextPosition()) { obj.AddPosition(new Position_Point(objPositionSet.GetPosition().GetX(), objPositionSet.GetPosition().GetY() - delta)); } Collide(obj); delta += 0.2f; } return(null); //return QueryConvexHull(); }
private PositionSetEdit_ImplementByICollectionTemplate testData() { int[] data = { 100, 100, 100, 200, 200, 100, 200, 200, 200, 200 }; int n = data.Length / 2; PositionSetEdit_ImplementByICollectionTemplate ps = new PositionSetEdit_ImplementByICollectionTemplate(); for (int i = 0; i < n; i++) { ps.AddPosition(new Position_Point(data[2 * i], data[2 * i + 1])); } return(ps); }
private void AddDoubleConnectionButton_Click(object sender, EventArgs e) { selectedNode = null; layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle; ResetEventHandler(); IPosition nearestPoint = new Position_Connected_Edit(0, 0); PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate(); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate); MouseMovedRealPosition = delegate(IPosition position) { positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint); nearestPoint = m2m_NN.NearestNeighbor(position); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); layer.SpringLayerRepresentationChangedEvent(layer); layersPaintedControl.Invalidate(); }; layer.VisibleInOptDlg = false; layer.Active = true; layer.Name = "Edited Figure"; layer.SquareFrameDrawer.RectangleRadiusWidth = 8; layer.SquareFrameDrawer.LineWidth = 1; layer.SquareFrameDrawer.LineColor = Color.YellowGreen; layer.CenterPointCoordinate.Visible = true; layer.CenterPointCoordinate.CoordinateFont = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold); if (currentLayer != null) { layers.Remove(currentLayer); } currentLayer = layer; layers.Add(layer); MouseLBottonCheckRealPosition = delegate(IPosition position) { if (selectedNode == null) { selectedNode = nearestPoint; positionSetEdit_ImplementByICollectionTemplate.AddPosition(selectedNode); } else { if (selectedNode != nearestPoint) { float x1 = selectedNode.GetX(), y1 = selectedNode.GetY(), x2 = nearestPoint.GetX(), y2 = nearestPoint.GetY(); float distance = (float)Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); if (selectedNode is IPosition_Connected_Edit) { ((IPosition_Connected_Edit)selectedNode).GetAdjacencyPositionSetEdit().AddAdjacency((IPosition_Connected)nearestPoint, distance); } else { throw new Exception(error_connection); } if (nearestPoint is IPosition_Connected_Edit) { ((IPosition_Connected_Edit)nearestPoint).GetAdjacencyPositionSetEdit().AddAdjacency((IPosition_Connected)selectedNode, distance); } else { throw new Exception(error_connection); } positionSetEdit_ImplementByICollectionTemplate.RemovePosition(selectedNode); selectedNode = null; } } selectedPositionSetLayer.SpringLayerRepresentationChangedEvent(selectedPositionSetLayer); layersPaintedControl.Invalidate(); }; }
public IPosition GetMouseDoubleChickedNearestPositionInCurrentPositionSet(IPositionSet currentPositionSet) { M2M_NN m2m_NN = new M2M_NN(); m2m_NN.PreProcess(currentPositionSet); layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle; ResetEventHandler(); //layersPainterForm.state_PainterForm_MouseDown = LayersPainterForm.State_PainterForm_MouseDown.Other; IPosition nearestPoint = new Position_Connected_Edit(0, 0); PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate(); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate); MouseMovedRealPosition = delegate(IPosition position) { positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint); nearestPoint = m2m_NN.NearestNeighbor(position); positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint); layer.SpringLayerRepresentationChangedEvent(layer); layersPaintedControl.Invalidate(); }; layer.VisibleInOptDlg = false; layer.Active = true; layer.Name = "Edited Figure"; layer.SquareFrameDrawer.RectangleRadiusWidth = 8; layer.SquareFrameDrawer.LineWidth = 1; layer.SquareFrameDrawer.LineColor = Color.Red; layer.CenterPointCoordinate.Visible = true; layer.CenterPointCoordinate.CoordinateFont = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold); System.Threading.Thread currentThread = System.Threading.Thread.CurrentThread; Position_Connected_Edit mouseChickedRealPosition = null; IPosition nearestPositionInCurrentPositionSet = null; this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "please double chick on the screen." }); MouseDoubleChickOnLayersPaintedControl += delegate(object sender, MouseEventArgs e) { mouseChickedRealPosition = new Position_Connected_Edit(); mouseChickedRealPosition.SetX(layersPaintedControl.ConvertMouseXToRealX(e.X)); mouseChickedRealPosition.SetY(layersPaintedControl.ConvertMouseYToRealY(e.Y)); nearestPositionInCurrentPositionSet = m2m_NN.NearestNeighbor(mouseChickedRealPosition); if ((currentThread.ThreadState & System.Threading.ThreadState.Suspended) == System.Threading.ThreadState.Suspended) { currentThread.Resume(); } }; if (currentLayer != null) { layers.Remove(currentLayer); } currentLayer = layer; layers.Add(layer); currentThread.Suspend(); MouseDoubleChickOnLayersPaintedControl = null; return(nearestPositionInCurrentPositionSet); }
public static IPositionSet getBlockLine3(IPosition refPos, float itv0, IPosition startPos, IPosition endPos) { rX = refPos.GetX(); rY = refPos.GetY(); itv = itv0; float sX = startPos.GetX(); float sY = startPos.GetY(); float eX = endPos.GetX(); float eY = endPos.GetY(); PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); if ((sX == eX) && (sY == eY)) { return(result); } /* * if (getM(sX) == getM(eX) && getN(sY) == getN(eY)) * { * x = getM(sX) * itv + itv / 2 + rX; * y = getN(sY) * itv + itv / 2 + rY; * result.AddPosition(new SimplePosition(x, y)); * return result; * } * * //斜率为0或无限大的情况 * else if (xDir == 0 || yDir == 0) * { * eX = getM(eX) * itv + itv / 2 + rX; * eY = getN(eY) * itv + itv / 2 + rY; * while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0) * { * x = getM(x) * itv + itv / 2 + rX; * y = getN(y) * itv + itv / 2 + rY; * result.AddPosition(new SimplePosition(x, y)); * x += itv * xDir; * y += itv * yDir; * } * return result; * } */ int xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1; int yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1; //最简单的情况:一条线 if (getM(sX) == getM(eX) && getN(sY) == getN(eY)) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } else if (getM(sX) == getM(eX)) { for (int i = getN(sY); (getN(eY) - i) * yDir >= 0; i += yDir) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } else if (getN(sY) == getN(eY)) { for (int i = getM(sX); (getM(eX) - i) * xDir >= 0; i += xDir) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } //一般情况:斜线 float x = sX; float y = sY; float k = (eY - sY) / (eX - sX); float nextX, nextY; int offsetX = 0, offsetY = 0; if (xDir > 0) { nextX = (getM(sX) + 1) * itv + rX; } else { nextX = getM(sX) * itv + rX; } if (yDir > 0) { nextY = (getN(sY) + 1) * itv + rY; } else { nextY = getN(sY) * itv + rY; } while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0) { float px = (getM(x) + offsetX) * itv + itv / 2 + rX; float py = (getN(y) + offsetY) * itv + itv / 2 + rY; offsetX = offsetY = 0; result.AddPosition(new Position_Point(px, py)); float dx = Math.Abs(nextX - x); float dy = Math.Abs(nextY - y); double dk = dy / dx; if (dk > Math.Abs(k)) { x = nextX; if (xDir < 0) { offsetX = -1; } nextX += itv * xDir; y = sY + (x - sX) * k; } else if (dk < Math.Abs(k)) { y = nextY; if (yDir < 0) { offsetY = -1; } nextY += itv * yDir; x = sX + (y - sY) / k; } else { x = nextX; y = nextY; if (xDir < 0) { offsetX = -1; } if (yDir < 0) { offsetY = -1; } nextX += itv * xDir; nextY += itv * yDir; } } return(result); }
private void AddNodeButton_Click(object sender, EventArgs e) { layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.Other; IPosition currentPoint = new Position_Connected_Edit(0, 0); PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate(); positionSetEdit_ImplementByICollectionTemplate.AddPosition(currentPoint); Layer_PositionSet_Point layer = new Layer_PositionSet_Point(positionSetEdit_ImplementByICollectionTemplate); MouseMovedRealPosition = delegate(IPosition position) { positionSetEdit_ImplementByICollectionTemplate.RemovePosition(currentPoint); currentPoint = position; positionSetEdit_ImplementByICollectionTemplate.AddPosition(currentPoint); layer.SpringLayerRepresentationChangedEvent(layer); layersPaintedControl.Invalidate(); }; layer.VisibleInOptDlg = false; layer.Active = true; layer.Name = "Edited Figure"; layer.Point.IsDrawPointBorder = true; layer.Point.PointColor = Color.Red; layer.Point.PointRadius = 3; layer.PointCoordinate.CoordinateFont = new Font(layer.PointCoordinate.CoordinateFont, FontStyle.Bold); layer.PointCoordinate.Visible = true; if (currentLayer != null) { layers.Remove(currentLayer); } currentLayer = layer; layers.Add(layer); MouseLBottonCheckRealPosition = delegate(IPosition position) { //((IPositionSetEdit)selectedPositionSetEdit).AddPosition(position); if (selectedPositionSetEdit is IPositionSet_ConnectedEdit && position is IPosition_Connected) { ((IPositionSet_ConnectedEdit)selectedPositionSetEdit).AddPosition_Connected((IPosition_Connected)position); } else if (selectedPositionSetEdit is IPositionSetEdit) { ((IPositionSetEdit)selectedPositionSetEdit).AddPosition(position); } else { throw new Exception(error_position); } selectedPositionSetLayer.SpringLayerRepresentationChangedEvent(selectedPositionSetLayer); if (m2m_NN.CanInsert(position)) { m2m_NN.Insert(position); } else { m2m_NN.PreProcess(selectedPositionSetEdit); selectedPositionSetLayer.SpringLayerMaxRectChangedEvent(); } layersPaintedControl.Invalidate(); }; }
//求散点子集的凸包 private IPositionSet solveHelp(IPosition p1, IPosition p2, IPositionSet ps) { //求离p1,p2最远的点 ps.InitToTraverseSet(); if (!ps.NextPosition()) { return(ps); } IPosition p3 = ps.GetPosition(); double p3_h = getH(p1, p2, p3); PositionSetEdit_ImplementByICollectionTemplate rest = new PositionSetEdit_ImplementByICollectionTemplate(); while (ps.NextPosition()) { IPosition pt = ps.GetPosition(); double pt_h = getH(p1, p2, pt); if (pt_h > p3_h) { rest.AddPosition(p3); p3 = pt; p3_h = pt_h; } else { rest.AddPosition(pt); } } //把散点分成几部分 PositionSetEdit_ImplementByICollectionTemplate leftpart = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate rightpart = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate p1set = new PositionSetEdit_ImplementByICollectionTemplate(); PositionSetEdit_ImplementByICollectionTemplate p2set = new PositionSetEdit_ImplementByICollectionTemplate(); ps = rest; IPosition p11 = new Position_Point(2 * p2.GetX() - p1.GetX(), 2 * p2.GetY() - p1.GetY()); //p1关于p2的对称点 double leftangle = getAngle(p1, p2, p3); double rightangle = getAngle(p2, p11, p3); ps.InitToTraverseSet(); while (ps.NextPosition()) { IPosition p = ps.GetPosition(); if (p.GetX() == p1.GetX() && p.GetY() == p1.GetY()) { p1set.AddPosition(p); } else if (p.GetX() == p2.GetX() && p.GetY() == p2.GetY()) { p2set.AddPosition(p); } else if (p.GetX() == p3.GetX() && p.GetY() == p3.GetY()) { leftpart.AddPosition(p); } else { double a = getAngle(p1, p2, p); if (a > leftangle) { leftpart.AddPosition(p); } a = getAngle(p2, p11, p); if (a < rightangle) { rightpart.AddPosition(p); } } } //对子集求解 IPositionSet subResult1 = solveHelp(p1, p3, leftpart); IPositionSet subResult2 = solveHelp(p3, p2, rightpart); //合并各部分解 PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); p1set.InitToTraverseSet(); while (p1set.NextPosition()) { result.AddPosition(p1set.GetPosition()); } subResult1.InitToTraverseSet(); while (subResult1.NextPosition()) { result.AddPosition(subResult1.GetPosition()); } result.AddPosition(p3); subResult2.InitToTraverseSet(); while (subResult2.NextPosition()) { result.AddPosition(subResult2.GetPosition()); } p2set.InitToTraverseSet(); while (p2set.NextPosition()) { result.AddPosition(p2set.GetPosition()); } return(result); }
//求散点集的凸包 public IPositionSet ConvexHull(IPositionSet ps) { IPosition p1 = null; IPosition p2 = null; PositionSetEdit_ImplementByICollectionTemplate rest = new PositionSetEdit_ImplementByICollectionTemplate(); //找到最低点 /* * p1 = pts[0]; * for (int i = 1; i < pts.Length; i++) * { * if (pts[i].Y < p1.Y || ( pts[i].Y == p1.Y && pts[i].X < p1.X) ) * { * rest.Add(p1); * p1 = pts[i]; * } * else * rest.Add(pts[i]); * } * */ ps.InitToTraverseSet(); if (ps.NextPosition()) { p1 = ps.GetPosition(); } while (ps.NextPosition()) { p2 = ps.GetPosition(); if (p2.GetY() < p1.GetY() || (p2.GetY() == p1.GetY() && p2.GetX() < p1.GetX())) { rest.AddPosition(p1); p1 = p2; } else { rest.AddPosition(p2); } } rest.InitToTraverseSet(); //找到与最低点成角最小的点 /* * int p2_i = 0; * double p2_angle = getAngle(rest[0], p1); * for (int i = 1; i < rest.Count; i++) * { * double angle_t = getAngle(rest[i], p1); * if (angle_t < p2_angle || (angle_t == p2_angle && getDistance(p1, rest[i]) > getDistance(p1, rest[p2_i])) ) * { * p2_i = i; * p2_angle = angle_t; * } * } * p2 = rest[p2_i]; * rest.RemoveAt(p2_i); * */ ps = rest; rest = new PositionSetEdit_ImplementByICollectionTemplate(); ps.InitToTraverseSet(); if (ps.NextPosition()) { p2 = ps.GetPosition(); } double p2_angle = getAngle(p2, p1); while (ps.NextPosition()) { IPosition pt = ps.GetPosition(); double angle_t = getAngle(pt, p1); if (angle_t < p2_angle || (angle_t == p2_angle && getDistance(p1, pt) > getDistance(p1, p2))) { rest.AddPosition(p2); p2 = pt; p2_angle = angle_t; } else { rest.AddPosition(pt); } } //求解剩余点 IPositionSet subResult = solveHelp(p1, p2, rest); //组合成凸包 PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); result.AddPosition(p1); /* * foreach (Point p in subResult) * result.Add(p); * */ subResult.InitToTraverseSet(); while (subResult.NextPosition()) { result.AddPosition(subResult.GetPosition()); } result.AddPosition(p2); return(result); }
/// <summary> /// 二维Line2Block实现 /// </summary> /// <param name="gridWidth">格的宽度</param> /// <param name="gridHeight">格的高度</param> /// <param name="startPosition">线段开始点</param> /// <param name="endPosition">线段终止点</param> /// <returns>线段所经过的方块的中点坐标集合(不包括开始点和终止点所在的方块),如果是空集直接返回NULL</returns> public IPositionSet WriteLineInGrid(float gridWidth, float gridHeight, IPosition startPosition, IPosition endPosition) { //归一化处理 float temp; float endx = endPosition.GetX() / gridWidth; float startx = startPosition.GetX() / gridWidth; float endy = endPosition.GetY() / gridHeight; float starty = startPosition.GetY() / gridHeight; float dx = endx - startx; float dy = endy - starty; //如果两点在同一个方块里就返回空 if ((int)startx == (int)endx && (int)starty == (int)endy) { //positionSet.AddPosition(new Position_Point(((int)startx+0.5f)*gridWidth,((int)starty+0.5f)*gridHeight)); return(null); } PositionSetEdit_ImplementByICollectionTemplate positionSet = new PositionSetEdit_ImplementByICollectionTemplate(); if (Math.Abs(dx) > Math.Abs(dy)) { //保证起点x坐标小于终点x坐标 if (startx > endx) { temp = startx; startx = endx; endx = temp; temp = starty; starty = endy; endy = temp; dx = -dx; dy = -dy; } //添加起点所在的方块 //positionSet.AddPosition(new Position_Point(((int)startx + 0.5f) * gridWidth, ((int)starty + 0.5f) * gridHeight)); float slope = dy / dx; float x = (int)startx + 1; float y = (x - startx) * slope + starty;//算出第一个边界值 float oldy = y; //添加可能出现的与起点所在分块同一列的方块 if ((int)y != (int)starty) { positionSet.AddPosition(new Position_Point(((int)x - 1 + 0.5f) * gridWidth, ((int)y + 0.5f) * gridHeight)); } //迭代所有边界值 for (int i = 0; i < (int)endx - (int)startx - 1; i++) { oldy = y; x += 1; y += slope; positionSet.AddPosition(new Position_Point(((int)x - 1 + 0.5f) * gridWidth, ((int)oldy + 0.5f) * gridHeight)); if ((int)oldy != (int)y) { positionSet.AddPosition(new Position_Point(((int)x - 1 + 0.5f) * gridWidth, ((int)y + 0.5f) * gridHeight)); } } //添加可能出现的与终点所在分块同一列的方块 if ((int)y != (int)endy) { positionSet.AddPosition(new Position_Point(((int)x + 0.5f) * gridWidth, ((int)y + 0.5f) * gridHeight)); } } else { //保证起点y坐标小于终点y坐标 if (starty > endy) { temp = startx; startx = endx; endx = temp; temp = starty; starty = endy; endy = temp; dx = -dx; dy = -dy; } //添加起点所在的方块 //positionSet.AddPosition(new Position_Point(((int)startx + 0.5f) * gridWidth, ((int)starty + 0.5f) * gridHeight)); float slope = dx / dy; float y = (int)starty + 1; float x = (y - starty) * slope + startx;//算出第一个边界值 float oldx = x; //添加可能出现的与终点所在分块同一行的方块 if ((int)x != (int)startx) { positionSet.AddPosition(new Position_Point(((int)x + 0.5f) * gridWidth, ((int)y - 1 + 0.5f) * gridHeight)); } //迭代所有边界值 for (int i = 0; i < (int)endy - (int)starty - 1; i++) { oldx = x; x += slope; y += 1; positionSet.AddPosition(new Position_Point(((int)oldx + 0.5f) * gridWidth, ((int)y - 1 + 0.5f) * gridHeight)); if ((int)oldx != (int)x) { positionSet.AddPosition(new Position_Point(((int)x + 0.5f) * gridWidth, ((int)y - 1 + 0.5f) * gridHeight)); } } //添加可能出现的与终点所在分块同一行的方块 if ((int)x != (int)endx) { positionSet.AddPosition(new Position_Point(((int)x + 0.5f) * gridWidth, ((int)y + 0.5f) * gridHeight)); } } return(positionSet.GetNum() == 0?null:positionSet); }
public static IPositionSet getBlockLine2(IPosition refPos, float itv0, IPosition startPos, IPosition endPos) { rX = refPos.GetX(); rY = refPos.GetY(); itv = itv0; float sX = startPos.GetX(); float sY = startPos.GetY(); float eX = endPos.GetX(); float eY = endPos.GetY(); PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); int xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1; int yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1; float x = sX; float y = sY; //结果只包含一个格 if (getM(sX) == getM(eX) && getN(sY) == getN(eY)) { x = getM(sX) * itv + itv / 2 + rX; y = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(x, y)); return(result); } //斜率为0或无限大的情况 else if (xDir == 0 || yDir == 0) { //eX = getM(eX) * itv + itv / 2 + rX; //eY = getN(eY) * itv + itv / 2 + rY; //while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0) //{ // x = getM(x) * itv + itv / 2 + rX; // y = getN(y) * itv + itv / 2 + rY; // result.AddPosition(new SimplePosition(x, y)); // x += itv * xDir; // y += itv * yDir; //} while ((eX - x) * xDir >= 0 && (eY - y) * yDir >= 0) { result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY)); if (eX % itv == 0 && xDir == 0) { result.AddPosition(new Position_Point(getM(x - itv / 2) * itv + itv / 2 + rX, getN(y) * itv + itv / 2 + rY)); } if (eY % itv == 0 && yDir == 0) { result.AddPosition(new Position_Point(getM(x) * itv + itv / 2 + rX, getN(y - itv / 2) * itv + itv / 2 + rY)); } x += itv * xDir; y += itv * yDir; } return(result); } //斜率不为0且不为无限大的情况 float k = (eY - sY) / (eX - sX); //float nextX, nextY; //if (xDir > 0) // nextX = (getM(sX) + 1) * itv + rX; //else // nextX = getM(sX) * itv + rX - MIN_ITV; //if (yDir > 0) // nextY = (getN(sY) + 1) * itv + rY; //else // nextY = getN(sY) * itv + rY - MIN_ITV; //while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0) //{ // float px = getM(x) * itv + itv / 2 + rX; // float py = getN(y) * itv + itv / 2 + rY; // result.AddPosition(new SimplePosition(px, py)); // float dx = Math.Abs(nextX - x); // float dy = Math.Abs(nextY - y); // double dk = dy / dx; // if (dk > Math.Abs(k)) // { // x = nextX; // nextX += itv * xDir; // y = sY + (x - sX) * k; // } // else if (dk < Math.Abs(k)) // { // y = nextY; // nextY += itv * yDir; // x = sX + (y - sY) / k; // } // else // { // x = nextX; // y = nextY; // nextX += itv * xDir; // nextY += itv * yDir; // } //} //xDir>0 yDir>0 ///找出所有边境交点 int sxx = getM(sX); int exx = getM(eX); int syy = getN(sY); int eyy = getN(eY); List <Position_Point> pointList = new List <Position_Point>(); if (xDir > 0) { for (int i = 0; i + sxx < exx + 1; i++) { float xx = (i + sxx) * itv + rX; float yy = k * xx + (eY - k * eX); pointList.Add(new Position_Point(xx, yy)); } } else { for (int i = 0; i + exx < sxx + 1; i++) { float xx = (i + exx) * itv + rX; float yy = k * xx + (eY - k * eX); pointList.Add(new Position_Point(xx, yy)); } } if (yDir > 0) { for (int i = 0; i + syy < eyy + 1; i++) { float yy = (i + syy) * itv + rY; float xx = (yy - (eY - k * eX)) / k; pointList.Add(new Position_Point(xx, yy)); } } else { for (int i = 0; i + eyy < syy + 1; i++) { float yy = (i + eyy) * itv + rY; float xx = (yy - (eY - k * eX)) / k; pointList.Add(new Position_Point(xx, yy)); } } pointList.Sort(new pointXcomparer()); for (int i = 1; i < pointList.Count; i++) { Position_Point p1 = pointList[i - 1]; Position_Point p2 = pointList[i]; //把两个交点的中点添加进去 float px = getM((p1.GetX() + p2.GetX()) / 2) * itv + itv / 2 + rX; float py = getN((p1.GetY() + p2.GetY()) / 2) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); if (i == pointList.Count - 1) { if (eX % itv == 0) { px = getM(eX + (itv / 2)) * itv + itv / 2 + rX; py = getN(eY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); px = getM(eX - (itv / 2)) * itv + itv / 2 + rX; result.AddPosition(new Position_Point(px, py)); } if (eY % itv == 0) { px = getM(eX) * itv + itv / 2 + rX; py = getN(eY + (itv / 2)) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); py = getN(eY - (itv / 2)) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } else if (i == 0) { if (sX % itv == 0) { px = getM(sX + (itv / 2)) * itv + itv / 2 + rX; py = getN(sY) * itv + itv / 2 + rY;; result.AddPosition(new Position_Point(px, py)); px = getM(sX - (itv / 2)) * itv + itv / 2 + rX; result.AddPosition(new Position_Point(px, py)); } if (sY % itv == 0) { px = getM(sX) * itv + itv / 2 + rX; py = getN(sY + (itv / 2)) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); py = getN(sY - (itv / 2)) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } if (p1.GetX() == p2.GetX() && p1.GetY() == p2.GetY()) { px = p1.GetX(); py = p2.GetY(); result.AddPosition(new Position_Point(px - itv / 2, py + itv / 2)); result.AddPosition(new Position_Point(px - itv / 2, py - itv / 2)); result.AddPosition(new Position_Point(px + itv / 2, py - itv / 2)); result.AddPosition(new Position_Point(px + itv / 2, py + itv / 2)); } } return(result); }
public void PreProcess(PositionSetEditSet pointList) { #region code for algorithm demo if (GetPositionSetToGetConvexHull != null) { GetPositionSetToGetConvexHull(pointList); } #endregion //请在这里改变不同的m2mStructure实现。 M2MSCreater_ForGeneralM2MStruture m2m_Creater_ForGeneralM2MStruture = new M2MSCreater_ForGeneralM2MStruture(); m2mStructure = m2m_Creater_ForGeneralM2MStruture.CreateAutomatically(pointList, levelNum); PositionSetEdit_ImplementByICollectionTemplate positionSet = new PositionSetEdit_ImplementByICollectionTemplate(); ILevel bottomLevel = m2mStructure.GetLevel(m2mStructure.GetLevelNum() - 1); for (int i = 0; i < pointList.GetPositionSetNum(); i++) { IPosition start = new Position_Point(); IPosition end = new Position_Point(); IPosition first = new Position_Point(); IPositionSet positionListTemp = pointList.GetNthPositionSet(i); positionListTemp.InitToTraverseSet(); if (positionListTemp.NextPosition()) { start = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY())); first = start; positionSet.AddPosition(start); } while (positionListTemp.NextPosition()) { end = new Position_Point(bottomLevel.ConvertRealValueToRelativeValueX(positionListTemp.GetPosition().GetX()), bottomLevel.ConvertRealValueToRelativeValueY(positionListTemp.GetPosition().GetY())); IPositionSet positionSetTemp = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), start, end); if (positionSetTemp != null) { positionSetTemp.InitToTraverseSet(); while (positionSetTemp.NextPosition()) { positionSet.AddPosition(positionSetTemp.GetPosition()); } } start = end; positionSet.AddPosition(start); } IPositionSet positionSetTemp2 = writeLineInGridEngine.WriteLineInGrid(bottomLevel.GetGridWidth(), bottomLevel.GetGridHeight(), end, first); if (positionSetTemp2 != null) { positionSetTemp2.InitToTraverseSet(); while (positionSetTemp2.NextPosition()) { positionSet.AddPosition(positionSetTemp2.GetPosition()); } } } m2mStructure.Preprocessing(positionSet); #region code for algorithm demo if (GetM2MStructure != null) { GetM2MStructure(m2mStructure); } #endregion }
private void button1_Click(object sender, EventArgs e) { IConvexHullEngine convexhull; int n; switch (comboBox1.SelectedIndex) { case 0: convexhull = new GrahamScan(); break; case 1: //convexhull = new QuickHull(); convexhull = new M2M_CH(); break; default: return; } try { n = Int32.Parse(textBox1.Text); } catch (Exception) { return; } //Point[] ps = new Point[n]; PositionSetEdit_ImplementByICollectionTemplate ps = new PositionSetEdit_ImplementByICollectionTemplate(); Random r = new Random(DateTime.Now.Millisecond); //if (true) { int minx = (int)(pb.Width * 0.05); int miny = (int)(pb.Height * 0.05); for (int i = 0; i < n; i++) { int x = r.Next(minx, pb.Width - minx); int y = r.Next(miny, pb.Height - miny); //ps[i] = new Point(x, y); ps.AddPosition(new Position_Point(x, y)); System.Console.Write(x); System.Console.Write(","); System.Console.Write(y); System.Console.WriteLine(","); } System.Console.WriteLine(); } //else //{ // ps = testData(); //} IPositionSet convexPoints = convexhull.ConvexHull(ps); pb.Image = new Bitmap(pb.Width, pb.Height); Graphics g = Graphics.FromImage(pb.Image); convexPoints.InitToTraverseSet(); g.Clear(Color.White); /* * for (int i = 0; i < ps.Length; i++) * { * g.FillEllipse(Brushes.Black, ps[i].X - 2, pb.Height - ps[i].Y - 2, 4, 4); * g.DrawString("(" + ps[i].X + "," + ps[i].Y + ")", Font, Brushes.Blue, ps[i].X, pb.Height - ps[i].Y); * } * for (int i = 0; i < convexPoints.Length; i++) * { * if (i != convexPoints.Length - 1) * g.DrawLine(new Pen(Color.Green), new PointF(convexPoints[i].X, pb.Height - convexPoints[i].Y), new PointF(convexPoints[i + 1].X, pb.Height - convexPoints[i + 1].Y)); * else * { * g.DrawLine(new Pen(Color.Green), new PointF(convexPoints[i].X, pb.Height - convexPoints[i].Y), new PointF(convexPoints[0].X, pb.Height - convexPoints[0].Y)); * } * } * */ ps.InitToTraverseSet(); IPosition p = ps.GetPosition(); while (p != null) { g.FillEllipse(Brushes.Black, p.GetX() - 2, pb.Height - p.GetY() - 2, 4, 4); g.DrawString("(" + p.GetX() + "," + p.GetY() + ")", Font, Brushes.Blue, p.GetX(), pb.Height - p.GetY()); p = ps.GetPosition(); } IPosition p1 = convexPoints.GetPosition(); IPosition p2 = convexPoints.GetPosition(); while (p2 != null) { g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), pb.Height - p1.GetY()), new PointF(p2.GetX(), pb.Height - p2.GetY())); p1 = p2; p2 = convexPoints.GetPosition(); } convexPoints.InitToTraverseSet(); p2 = convexPoints.GetPosition(); g.DrawLine(new Pen(Color.Green), new PointF(p1.GetX(), pb.Height - p1.GetY()), new PointF(p2.GetX(), pb.Height - p2.GetY())); }
private void button1_Click(object sender, EventArgs e) { M2M_CD M2M_CD = new M2M_CD(); new ConfiguratedByForm(M2M_CD); //以下代码必须在UI线程中调用,即不能在另开的线程中调用 LayersExOptDlg layers = new LayersExOptDlg(); LayersPainterForm layersPainterForm = new LayersPainterForm(layers); LayersPaintedControl layersPaintedControl = layersPainterForm.LayersPaintedControl; LayersEditedControl layersEditedControl = new LayersEditedControl(); layersEditedControl.Dock = DockStyle.Top; layersEditedControl.LayersPaintedControl = layersPaintedControl; layersPainterForm.Controls.Add(layersEditedControl); FlowControlerForm flowControlerForm = new FlowControlerForm(); layersPainterForm.Show(); flowControlerForm.Show(layersPainterForm); //新建或外部传入待演示对象 //打开一个Worker线程来进行算法流程的演示(否则会阻塞UI线程以至于演示不能进行) IAsyncResult result = new dDemoProcess(delegate { //产生随机点集: /* * RandomPositionSet_InFixedDistribution randomPositionSet_InFixedDistribution = new RandomPositionSet_InFixedDistribution(); * randomPositionSet_InFixedDistribution.PointNum = 1000; * randomPositionSet_InFixedDistribution.DistributionStyle = distributionStyle.ClusterGaussianDistribution; * new ConfiguratedByForm(randomPositionSet_InFixedDistribution); * randomPositionSet_InFixedDistribution.Produce(); * * //编辑点集 * Layer_PositionSet_Point layer = new Layer_PositionSet_Point(randomPositionSet_InFixedDistribution); * layer.EditAble = true; * layer.Point.PointColor = Color.Yellow; * layer.Point.PointRadius = 2; * layer.Point.IsDrawPointBorder = true; * layers.Add(layer); * layersPainterForm.Invalidate(); * flowControlerForm.SuspendAndRecordWorkerThread(); * layers.Remove(layer); * */ /////////////////////////////////////// //GetRandomPositionFromPositionSetRectangle getRandomPositionFromPositionSetRectangle //= new GetRandomPositionFromPositionSetRectangle(randomPositionSet_InFixedDistribution); ////对m2m_NN算法进行演示: ////为对象的事件添加事件响应,以进行算法演示 //AlgorithmDemo_M2M_NN algorithmDemo_M2M_NN = new AlgorithmDemo_M2M_NN(m2m_NN, layers, flowControlerForm, layersPaintedControl.Invalidate); ////以下代码与非演示状态一样。 //m2m_NN.PreProcess(randomPositionSet_InFixedDistribution); //m2m_NN.NearestNeighbor(layersPaintedControl.GetMouseDoubleChickedRealPosition()); //while (true) //{ // for (int i = layers.Count - 1; i >= 0; i--) // { // if ((layers[i].Name != "M2MStructure") && (layers[i].Name != "PositionSetOfComparedPoint")) // { // layers.Remove(layers[i]); // } // } // m2m_NN.NearestNeighbor(getRandomPositionFromPositionSetRectangle.Get()); //} ////结束演示(解除事件响应的绑定) //algorithmDemo_M2M_NN.EndDemo(); /////////////////////////////////////// //对M2M_CD算法进行演示: //为对象的事件添加事件响应,以进行算法演示 PositionSetEditSet polygonSet = new PositionSetEditSet(); IPositionSetEdit polygon = new PositionSetEdit_ImplementByICollectionTemplate(); polygon.AddPosition(new Position_Point(40, 40)); polygon.AddPosition(new Position_Point(50, 10)); polygon.AddPosition(new Position_Point(100, 70)); IPositionSetEdit polygon2 = new PositionSetEdit_ImplementByICollectionTemplate(); polygon2.AddPosition(new Position_Point(0, 0)); polygon2.AddPosition(new Position_Point(20, 20)); polygon2.AddPosition(new Position_Point(21, 12)); IPositionSetEdit polygon3 = new PositionSetEdit_ImplementByICollectionTemplate(); polygon3.AddPosition(new Position_Point(60, 60)); polygon3.AddPosition(new Position_Point(62, 60)); polygon3.AddPosition(new Position_Point(62, 56)); polygon3.AddPosition(new Position_Point(64, 56)); polygon3.AddPosition(new Position_Point(61, 52)); polygon3.AddPosition(new Position_Point(58, 56)); polygon3.AddPosition(new Position_Point(60, 56)); polygonSet.AddPositionSet(polygon); polygonSet.AddPositionSet(polygon2); //polygonSet.AddPositionSet(polygon3); AlgorithmDemo_M2M_CD algorithmDemo_M2M_CD = new AlgorithmDemo_M2M_CD(M2M_CD, layers, flowControlerForm, layersPaintedControl.Invalidate); while (true) { layers.Clear(); //以下代码与非演示状态一样。 //M2M_CD.ConvexHull(randomPositionSet_InFixedDistribution.Produce()); Layer_PositionSet_Polygon layer = new Layer_PositionSet_Polygon(polygon); Layer_PositionSet_Polygon layer2 = new Layer_PositionSet_Polygon(polygon2); layers.Add(layer); layers.Add(layer2); M2M_CD.CollisionDetection(polygonSet, polygon3); } //结束演示(解除事件响应的绑定) algorithmDemo_M2M_CD.EndDemo(); }).BeginInvoke(null, null); }
public static IPositionSet getBlockLine(IPosition refPos, float itv0, IPosition startPos, IPosition endPos) { rX = refPos.GetX(); rY = refPos.GetY(); itv = itv0; float sX = startPos.GetX(); float sY = startPos.GetY(); float eX = endPos.GetX(); float eY = endPos.GetY(); PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate(); //判断起点终点是否相同 if ((sX == eX) && (sY == eY)) { return(result); } //判断起点终点横坐标是否相同 //判断起点终点纵坐标是否相同 int xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1; int yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1; //最简单的情况:一条线 if (getM(sX) == getM(eX) && getN(sY) == getN(eY)) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } else if (getM(sX) == getM(eX)) { for (int i = getN(sY); (getN(eY) - i) * yDir >= 0; i += yDir) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } else if (getN(sY) == getN(eY)) { for (int i = getM(sX); (getM(eX) - i) * xDir >= 0; i += xDir) { float px = getM(sX) * itv + itv / 2 + rX; float py = getN(sY) * itv + itv / 2 + rY; result.AddPosition(new Position_Point(px, py)); } } //一般情况:斜线 float x = sX; float y = sY; float k = (eY - sY) / (eX - sX); float nextX, nextY; int offsetX = 0, offsetY = 0; if (xDir > 0) { nextX = (getM(sX) + 1) * itv + rX; } else { nextX = getM(sX) * itv + rX; } if (yDir > 0) { nextY = (getN(sY) + 1) * itv + rY; } else { nextY = getN(sY) * itv + rY; } while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0) { float px = (getM(x) + offsetX) * itv + itv / 2 + rX; float py = (getN(y) + offsetY) * itv + itv / 2 + rY; offsetX = offsetY = 0; result.AddPosition(new Position_Point(px, py)); float dx = Math.Abs(nextX - x); float dy = Math.Abs(nextY - y); double dk = dy / dx; if (dk > Math.Abs(k)) { x = nextX; if (xDir < 0) { offsetX = -1; } nextX += itv * xDir; y = sY + (x - sX) * k; } else if (dk < Math.Abs(k)) { y = nextY; if (yDir < 0) { offsetY = -1; } nextY += itv * yDir; x = sX + (y - sY) / k; } else { x = nextX; y = nextY; if (xDir < 0) { offsetX = -1; } if (yDir < 0) { offsetY = -1; } nextX += itv * xDir; nextY += itv * yDir; } } IList <IPosition> poslist = result.ToIlist(); //去掉重复点 for (int i = 0; i < poslist.Count - 1;) { if (poslist[i].GetX() == poslist[i + 1].GetX() && poslist[i].GetY() == poslist[i + 1].GetY()) { poslist.RemoveAt(i + 1); } else { i++; } } //去掉两个端点 poslist.RemoveAt(0); poslist.RemoveAt(poslist.Count - 1); return(result); }