private void GenerateSamplePolygon() { //TODO: //generate sample polygon var pointsList = new List <MyPoint> { new MyPoint(626d, 57d), new MyPoint(856d, 191d), new MyPoint(744d, 301d), new MyPoint(851d, 532d), new MyPoint(460d, 502d), new MyPoint(367d, 408d), new MyPoint(427d, 149d) }; MyPolygon polygon = new MyPolygon(pointsList.Last(), PolygonCanvas); foreach (var point in pointsList) { polygon.AddVerticleAndDraw(point); } Polygons.Add(PolygonNumber, polygon); polygon.Edges[0].relationEdge = polygon.Edges[3]; polygon.Edges[3].relationEdge = polygon.Edges[0]; polygon.Edges[0].relationType = RelationType.Perpendicular; polygon.Edges[3].relationType = RelationType.Perpendicular; polygon.ApplyRelationChanges(polygon.Edges[0]); polygon.Edges[0].relationIcon = new RelationIcon(polygon.Edges[0], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas); polygon.Edges[3].relationIcon = new RelationIcon(polygon.Edges[3], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas); polygon.Edges[1].relationEdge = polygon.Edges[6]; polygon.Edges[6].relationEdge = polygon.Edges[1]; polygon.Edges[1].relationType = RelationType.Equal; polygon.Edges[6].relationType = RelationType.Equal; polygon.ApplyRelationChanges(polygon.Edges[1]); polygon.Edges[1].relationIcon = new RelationIcon(polygon.Edges[1], RelationType.Equal, PolygonNumber, polygon, PolygonCanvas); polygon.Edges[6].relationIcon = new RelationIcon(polygon.Edges[6], RelationType.Equal, PolygonNumber, polygon, PolygonCanvas); polygon.Edges[2].relationEdge = polygon.Edges[5]; polygon.Edges[5].relationEdge = polygon.Edges[2]; polygon.Edges[2].relationType = RelationType.Perpendicular; polygon.Edges[5].relationType = RelationType.Perpendicular; polygon.ApplyRelationChanges(polygon.Edges[2]); polygon.Edges[2].relationIcon = new RelationIcon(polygon.Edges[2], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas); polygon.Edges[5].relationIcon = new RelationIcon(polygon.Edges[5], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas); PolygonNumber++; }
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Canvas currentCanvas = sender as Canvas; Point CurrentMousePosition = e.GetPosition(currentCanvas); MyPoint p = new MyPoint(CurrentMousePosition.X, CurrentMousePosition.Y); switch (ProgramMode) { case Mode.Pointer: { foreach (var pol in Polygons) { foreach (var edge in pol.Value.Edges) { //check if point hit if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true) { CurrentDragObjectType = DragObjectType.Verticle; IsDraggingOn = true; DragPolygonId = pol.Key; DragObject = edge.first; return; } } foreach (var edge in pol.Value.Edges) { //check if edge hit if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true) { CurrentDragObjectType = DragObjectType.Edge; IsDraggingOn = true; DragPolygonId = pol.Key; DragObject = edge; DragStartingPoint = CurrentMousePosition; return; } } //check if inside Polygon if (pol.Value.IsPointInside(CurrentMousePosition) == true) { CurrentDragObjectType = DragObjectType.Polygon; IsDraggingOn = true; DragPolygonId = pol.Key; DragObject = pol.Value; DragStartingPoint = CurrentMousePosition; return; } } CurrentDragObjectType = DragObjectType.Nothing; } break; case Mode.Draw: { if (PolygonDrawing == false) { MyPolygon polygon = new MyPolygon(p, currentCanvas); CurrentlyDrawingPolygon = polygon; PolygonDrawing = true; CurrentLine?.DeleteDrawing(); CurrentLine = Draw.SimpleEdge(CurrentMousePosition, CurrentMousePosition, currentCanvas); } else { var DrawResult = CurrentlyDrawingPolygon.AddVerticleAndDraw(p); switch (DrawResult) { case PolygonDrawResult.DrawFinished: CurrentLine.SetFirstPoint(CurrentMousePosition); Polygons.Add(PolygonNumber, CurrentlyDrawingPolygon); CurrentLine.DeleteDrawing(); PolygonDrawing = false; CurrentlyDrawingPolygon = null; PolygonNumber++; break; case PolygonDrawResult.NotEnoughEdges: MessageBox.Show("Not enough edges to finish polygon", Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Warning); break; case PolygonDrawResult.DrawInProgress: CurrentLine.SetFirstPoint(CurrentMousePosition); break; default: break; } } } break; case Mode.AddMiddleVerticle: { foreach (var pol in Polygons) { foreach (var edge in pol.Value.Edges) { //check if edge hit if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true) { pol.Value.AddMiddleVerticleOnEdge(edge); return; } } } } break; case Mode.AddEqualRelation: case Mode.AddPerpendicularRelation: { RelationType relationType; if (ProgramMode == Mode.AddEqualRelation) { relationType = RelationType.Equal; } else { relationType = RelationType.Perpendicular; } if (RelationPolygonId == -1) { foreach (var pol in Polygons) { foreach (var edge in pol.Value.Edges) { //check if edge hit if (edge.relationType == RelationType.None) { if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true) { edge.SelectEdge(); RelationSelectedEdge = edge; RelationPolygonId = pol.Key; return; } } } } } else { foreach (var edge in Polygons[RelationPolygonId].Edges) { //check if edge hit if (edge.relationType == RelationType.None) { if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true) { if (Object.ReferenceEquals(edge, RelationSelectedEdge) == false) { edge.relationEdge = RelationSelectedEdge; RelationSelectedEdge.relationEdge = edge; edge.relationType = relationType; RelationSelectedEdge.relationType = relationType; var result = Polygons[RelationPolygonId].ApplyRelationChanges(edge); if (result == true) { edge.relationIcon = new RelationIcon(edge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas); RelationSelectedEdge.relationIcon = new RelationIcon(RelationSelectedEdge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas); } else { edge.relationEdge = null; RelationSelectedEdge.relationEdge = null; edge.relationType = RelationType.None; RelationSelectedEdge.relationType = RelationType.None; } } RelationSelectedEdge.UnselectEdge(); RelationSelectedEdge = null; RelationPolygonId = -1; return; } } } } } break; case Mode.DeleteVerticleOrRelation: { foreach (var pol in Polygons) { foreach (var edge in pol.Value.Edges) { //check if point hit if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true) { if (Polygons[pol.Key].DeleteVerticle(edge.first) == true) { Polygons[pol.Key] = null; Polygons.Remove(pol.Key); } return; } } foreach (var edge in pol.Value.Edges) { //check if edge hit if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true) { edge.DeleteRelation(); return; } } } } break; default: break; } return; }