/// <summary> /// Обрабатывает клик мыши по редактируемой карте /// </summary> private void canvasClick(object sender, MouseButtonEventArgs e) { System.Windows.Point pos = Mouse.GetPosition(sender as ContentControl); Geometry.Figures.Point point = UserInterface.Transformer.ConvertToModel(new Geometry.Figures.Point(pos.X, pos.Y)); if (flag) { addModel(point); } else { if (selectShapes.IsChecked == true && !(e.OriginalSource is Rectangle))// выбран режим выделения { int i = 0; for (i = 0; i < map.Lands.Count; i++) // находим индекс нашей фигуры в списке map.Lands { if (Geometry.Figures.Intersect.IsIntersected(point, map.Lands[i].Polygon)) { break; } } Shape shape = e.OriginalSource as Shape; if (Drawer.SelectedShapes.Contains(shape)) // если фигура уже выделена, то снимаем выделение { shape.StrokeThickness = 0; Drawer.SelectedShapes.Remove(shape); Drawer.SelectedLands.Remove(map.Lands[i]); } else // иначе выделяем её { Drawer.SelectedShapes.Add(shape); shape.Stroke = new SolidColorBrush(Colors.Red); shape.StrokeThickness = 3; Drawer.SelectedLands.Add(map.Lands[i]); //!! } } else { Drawer.ClickedPoints.Add(point); var radius = UserInterface.Transformer.ConvertToModelLength(6); Geometry.Figures.Circle circle = new Geometry.Figures.Circle(point, radius); UserInterface.Presenters.FigurePresenter FP = new UserInterface.Presenters.FigurePresenter(circle, null); UI.GamePresenter.Map.Figures.Add(FP); // после этих строк точки можно ставить не везде Geometry.Figures.Polyline polyline = new Geometry.Figures.Polyline(Drawer.ClickedPoints); Drawer.Line = new UserInterface.Presenters.FigurePresenter(polyline, null); UI.GamePresenter.Map.Figures.Add(Drawer.Line); reDrawMap(); } } }
///<summary> /// Получение позиции на ломанной в зависимости от времени на ее отрезках и пройденном времени + остаток ломанной /// </summary> public LastPolyAndPosition PositionAndLastPolyline(double time, List<double> durations) { var curTime = 0; var _time = 0.0; foreach (var duration in durations) { _time += duration; if (_time < time) { curTime++; } else { var subTime = 0.0; for (int i = 0; i <= curTime; i++) { subTime += durations[i]; } subTime = time - subTime; var lastPoint = PointsToSegments(points)[curTime].Position(subTime, durations[curTime]); var lastPoints = new List<Point> {lastPoint}; for (int index = curTime+1; index < points.Count; index++) { var point = points[index]; lastPoints.Add(point); } var lastPoly = new Polyline(lastPoints); return new LastPolyAndPosition(lastPoly, lastPoint); } } return null; }
/// <summary> /// Обработка нажатия кнопки "Нарисовать", отрисовывает нужную местность /// </summary> private void drawArea(object sender, RoutedEventArgs e) { if (Drawer.ClickedPoints.Count == 0) { MessageBox.Show(Messages.EmptyClickedPoints); } else { try { List <Geometry.Figure> po = new List <Geometry.Figure>(); if (Drawer.LandName == "Road" || Drawer.LandName == "Water") { #region Check Self Intersection var listSeg = Geometry.Figures.Polyline.PointsToSegments(Drawer.ClickedPoints); listSeg.Remove(listSeg.Last()); if (Geometry.Figures.Polyline.CheckSelfIntersection(listSeg)) { throw new Exception(Messages.SelfIntersectionPolyline); } #endregion Geometry.Figures.Polyline poly = new Geometry.Figures.Polyline(Drawer.ClickedPoints); po.Add(poly); } else { ConvexPolygon CV = new ConvexPolygon(); //?? List <Geometry.Figures.Point> mypoints = new List <Geometry.Figures.Point>(); List <List <Geometry.Figures.Point> > mypolpoints = new List <List <Geometry.Figures.Point> >(); foreach (Geometry.Figures.Point p in Drawer.ClickedPoints) { mypoints.Add(new Geometry.Figures.Point(p.X, p.Y)); } #region Check Self Intersection var listSeg = Geometry.Figures.Polyline.PointsToSegments(mypoints); if (Geometry.Figures.ConvexPolygon.CheckSelfIntersection(listSeg)) { throw new Exception(Messages.SelfIntersection); } #endregion mypolpoints = CV.ConvertToConvexList(mypoints); Drawer.Line.Points.Clear(); List <Geometry.Figures.Point> polPoints = new List <Geometry.Figures.Point>(); Geometry.Figures.ConvexPolygon poly = new Geometry.Figures.ConvexPolygon(Drawer.ClickedPoints); foreach (List <Geometry.Figures.Point> pointList in mypolpoints) //points { po.Add(new Geometry.Figures.ConvexPolygon(pointList)); } } switch (Drawer.LandName) { case "Sand": { GameEngine.Lands.Sand sand = new GameEngine.Lands.Sand(po); map.Lands.Add(sand); break; } case "Forest": { GameEngine.Lands.Forest forest = new GameEngine.Lands.Forest(po); map.Lands.Add(forest); break; } case "Water": { GameEngine.Lands.Water water = new GameEngine.Lands.Water(po); map.Lands.Add(water); break; } case "Mountain": { GameEngine.Lands.Mountains mountain = new GameEngine.Lands.Mountains(po); map.Lands.Add(mountain); break; } case "City": { GameEngine.Lands.City city = new GameEngine.Lands.City(po); map.Lands.Add(city); break; } case "LowLand": { GameEngine.Lands.Lowland lowLand = new GameEngine.Lands.Lowland(po); map.Lands.Add(lowLand); break; } case "Field": { GameEngine.Lands.Field field = new GameEngine.Lands.Field(po); map.Lands.Add(field); break; } case "Road": { GameEngine.Lands.Road road = new GameEngine.Lands.Road(po); map.Lands.Add(road); break; } } resetPoints(null, null); reDrawMap(); Drawer.ClickedPoints = new List <Geometry.Figures.Point>(); } catch (Exception a) { MessageBox.Show(a.Message); resetPoints(null, null); reDrawMap(); Drawer.ClickedPoints = new List <Geometry.Figures.Point>(); } } }
// Установление пересечения Полигона и Ломанной public static bool IsIntersected(ConvexPolygon polygon, Polyline polyline) { return GetIntersection(polygon, polyline).Count > 0; }
public LastPolyAndPosition(Polyline polyline, Point pos) { Polyline = polyline; Position = pos; }
//public static List<Point> GetIntersection(Segment first, Segment second) //{ // Point vectorFirst = new Point(first.End.X - first.Begin.X, first.End.Y - first.Begin.Y); // Point vectorSecond = new Point(second.End.X - second.Begin.X, second.End.Y - second.Begin.Y); // var poList = new List<Point>(); // if (vectorFirst.X / vectorSecond.X == vectorFirst.Y / vectorSecond.Y) // { // if (Point.Length(first.Begin, first.End) > Point.Length(second.Begin, second.End)) // { // if (IsIntersected(second.Begin, first)) poList.Add(second.Begin); // if (IsIntersected(second.End, first)) poList.Add(second.End); // } // else // { // if (IsIntersected(first.Begin, second)) poList.Add(first.Begin); // if (IsIntersected(first.End, second)) poList.Add(first.End); // } // } // double p = 0, t = 0; // double a1 = 0, a2 = 0, b1 = 0, b2 = 0, d1 = 0, d2 = 0; // d1 = second.Begin.X - first.Begin.X; // d2 = second.Begin.Y - first.Begin.Y; // a1 = vectorFirst.X; // a2 = vectorFirst.Y; // b1 = vectorSecond.X; // b2 = vectorSecond.Y; // if (a1 == 0) // { // p = d1 / b1; // t = (d2 - p * b2) / a2; // } // else // { // p = d2 - (d1 / a1) * a2; // p = p / (b2 - (b1 * a2) / a1); // t = (d1 - b1 * p) / a1; // } // p = -p; // if ((IsIntersected(new Point(first.Begin.X + vectorFirst.X * t, first.Begin.Y + vectorFirst.Y * t), first)) // && (IsIntersected(new Point(second.Begin.X + vectorSecond.X * p, second.Begin.Y + vectorSecond.Y * p), second))) // { // Point pointInter = new Point(first.Begin.X + vectorFirst.X * t, first.Begin.Y + vectorFirst.Y * t); // poList.Add(pointInter); // } // //if ((p <= 1) && (p >= 0) && (t <= 1) && (t >= 0)) // return poList; //} //public static List<Point> GetIntersection(Segment first, Segment second) //{ // //Point vectorFirst = new Point(first.End.X - first.Begin.X, first.End.Y - first.Begin.Y); // //Point vectorSecond = new Point(second.End.X - second.Begin.X, second.End.Y - second.Begin.Y); // Point firstNorm = LeftNormal(first); // Point secondNorm = LeftNormal(second); // List<Point> pointli = new List<Point>(); // if (((isLeft(first, firstNorm, second.Begin)) && (!(isLeft(first, firstNorm, second.End)))) // || (!(isLeft(first, firstNorm, second.Begin)) && (isLeft(first, firstNorm, second.End)))) // { // if (((isLeft(second, secondNorm, first.Begin)) && (!(isLeft(second, secondNorm, first.End)))) // || (!(isLeft(second, secondNorm, first.Begin)) && (isLeft(second, secondNorm, first.End)))) // { // Point vector = new Point(-firstNorm.X, -firstNorm.Y); // pointli.Add(); // } // } //} // Поиск пересечений Полигона и Ломанной public static List<Point> GetIntersection(ConvexPolygon polygon, Polyline polyline) { return Intersect.GetIntersection(polygon, polyline.Points.ToArray()); }
public static List<Point> GetIntersection(Polyline poly,ConvexPolygon polygon) { var poList = new List<Point>(); var polySegm = Polyline.PointsToSegments(poly.Points); polySegm = polySegm.GetRange(0, polySegm.Count - 1); foreach (var segment in polySegm) { //for (int index = 0; index < polySegments.Count; index++) //{ // if ((!isLeft(polySegments[index], normals[index], segment.Begin)) && // (!isLeft(polySegments[index], normals[index], segment.End))) // { // index = polySegments.Count; // } // else // { // if (GetIntersection(segment, polySegments[index]).Count > 0) // poList.Add((GetIntersection(segment, polySegments[index]))[0]); // } //} if (GetIntersection(segment,polygon).Count>0) poList.AddRange(GetIntersection(segment,polygon)); //if (poList.Count == 0) //{ // poList.Add(polySegm[0].Begin); // poList.Add(polySegm.Last().End); //} } return poList; }
///<summary> /// Установление пересечения Полигона и Ломанной /// </summary> public static bool IsIntersected(ConvexPolygon polygon, Polyline polyline) { return(GetIntersection(polygon, polyline).Count > 0); }
///<summary> /// Поиск пересечений Полигона и Ломаной /// </summary> public static List <Point> GetIntersection(ConvexPolygon polygon, Polyline polyline) { return(Intersect.GetIntersection(polygon, polyline.Points.ToArray())); }
// Поиск точек пересечений Полигона со списком точек (array) public static List <Point> GetIntersection(ConvexPolygon polygon, params Point[] arr) { int type = 0; // flag of ConvexPolygon type intersect (Polyline,Segment,Point) var points = new List <Point>(); var segments = Polyline.PointsToSegments(polygon.Points); var normals = polygon.Normals; switch (arr.Count()) { case 0: // Nothing return(new List <Point>()); case 1: // Point break; case 2: // Segment type = 1; break; default: // Polyline type = 2; var PolylineSegments = Polyline.PointsToSegments(arr.ToList()); foreach (var polylineSegment in PolylineSegments) { points.AddRange(Intersect.GetIntersection(polygon, polylineSegment.Begin, polylineSegment.End)); } break; } for (int index = 0; index < segments.Count; index++) { var t = segments[index]; if (type == 0) { if (!isLeft(t, normals[index], arr[0])) { return(new List <Point>()); } } else if (type == 1) { if (isLeft(t, normals[index], arr[0]) && !isLeft(t, normals[index], arr[1]) || !isLeft(t, normals[index], arr[0]) && isLeft(t, normals[index], arr[1])) { foreach (var segment in segments) { //if (Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment).Count > 0) points.AddRange(Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment)); var newSegment = new Segment(arr[0], arr[1]); if (GetIntersection(newSegment, segment).Count > 0) { points.AddRange(Intersect.GetIntersection(new Segment(arr[0], arr[1]), segment)); } } break; } else if (!isLeft(t, normals[index], arr[0]) && !isLeft(t, normals[index], arr[1])) { return(new List <Point>()); } } } if (type == 0) { points.Add(arr[0]); } if (type == 1) { points.AddRange(new List <Point> { arr[0], arr[1] }); } return(points); }
/// <summary> /// Проверка пересечения ломаной и многоугольника /// </summary> /// <returns>Возвращает список точек пересечения</returns> public static List<Point> GetIntersection(Polyline poly, ConvexPolygon polygon) { var poList = new List<Point>(); var polySegm = Polyline.PointsToSegments(poly.Points); foreach (var segment in polySegm) { if (GetIntersection(segment, polygon).Count > 0) poList.AddRange(GetIntersection(segment, polygon)); } return poList; }
IDrawable run(IDrawable input) { if (!(input is PointSet)) { throw new Exception("PolylinePoligonTest. Input is not PolylinePoligonTest."); } var inp = input as PointSet; Point a = inp[0]; Point c = inp[1]; Point d = inp[2]; Point e = inp[4]; Point polyPoint1 = inp[4]; Point aa = new Point(20, 20); e = new Point(30, 20); Point ee = new Point(50,30); Segment b = new Segment(aa, e); Segment eee = new Segment(e, ee); Segment eae = new Segment(new Point(50, 30), a); Polyline polyline = new Polyline(20, 20, 30, 20, 50, 30, a.X, a.Y); //Segment b = new Segment(a,e); //Segment eee = new Segment(e,new Point(3*(a.X+e.X)/2,4*(a.Y+e.Y)/2)); //var polyline = new Polyline(new List<Point>() { a, e, new Point((a.X + e.X) / 2, (a.Y + e.Y) / 2) }); //var f = new ConvexPolygon(new Point(c.X, c.Y), new Point(d.X, c.Y), new Point(d.X, d.Y), new Point(c.X, d.Y)); var f = new ConvexPolygon(new Point((c.X + d.X) / 2, c.Y), new Point(d.X, (c.Y + d.Y) / 3), new Point(d.X, 2 * ((d.Y + c.Y) / 3)), new Point((c.X + d.X) / 2, d.Y), new Point(c.X, 2 * ((d.Y + c.Y) / 3)), new Point(c.X, ((d.Y + c.Y) / 3))); var x = Intersect.GetIntersection(polyline,f); if (x.Count > 0) return new DrawableSet(new List<IDrawable>() { new PointSet(x.ToArray()), new SegmentSet(b,eee,eae),new PolygonSet(f), DrawableElement.Text(0, 0, "Vse horosho") }); else return new DrawableSet(new List<IDrawable>() { new PointSet(x.ToArray()),new SegmentSet(b,eee,eae), new PolygonSet(f), DrawableElement.Text(0, 0, "Vse ploho") }); }