//gets NFP locations private List <Pair> NFPOptions(MyShapes orbiting, MyShapes statinary) { List <Pair> pairs = new List <Pair>(); for (int i = 0; i < statinary.Points.Length; i++) { for (int j = 0; j < orbiting.Points.Length; j++) { pairs.Add(new Pair(i, j)); } } for (int i = 0; i < pairs.Count; i++) { Points Trim = TrimTranslation(orbiting, statinary, pairs[i].shape, statinary.PlacedPoints[pairs[i].shape], pairs[i].Orbit); pairs[i].PointOnA = statinary.PlacedPoints[pairs[i].shape] - Trim; Points[] points = GetPoints(orbiting, pairs[i].PointOnA, pairs[i].Orbit); pairs[i].PointOnA = points[0]; foreach (var point in points) { Points p = new Points(Math.Round(point.X, 1), Math.Round(point.Y, 1)); if (!LinesDoNotIntersect(statinary.PlacedPoints, points)) { pairs.Remove(pairs[i--]); break; } } } return(pairs); }
private Points NTFFunctions(MyShapes orbiting, MyShapes stationary, List <MyShapes> myShapes, MyShapes area) { List <Pair> pairs = NFPOptions(orbiting, stationary); EliminateOptions(pairs, myShapes, area, orbiting, stationary); if (pairs.Count == 0) { return(null); } Points pointOnA; do { int optimal = GetOptimalLocation(pairs); pointOnA = pairs[optimal].PointOnA; LeftBottom(myShapes, orbiting, 0, ref pointOnA); Points[] placedPoints = GetPoints(orbiting, pointOnA, 0); if (placedPoints[GetMaxY(orbiting.Points)].Y > area.Height) { pointOnA = new Points(-1, -1); } if (pointOnA.X == -1) { pairs.RemoveAt(optimal); if (pairs.Count == 0) { return(null); } } }while (pointOnA.X == -1); return(pointOnA); }
private void RemoveShape(object obj) { if (Shape != null) { MyShapes.Remove(Shape); Shape = null; } }
//moves every vertex of shape to location on which will be placed public Vector[] GetVectors(MyShapes s, Vector indexOnA, int indexOfF) { double difx = indexOnA.X - s.Vectors[indexOfF].X, diffy = indexOnA.Y - s.Vectors[indexOfF].Y; Vector[] vector = new Vector[s.Vectors.Length]; for (int i = 0; i < vector.Length; i++) { vector[i] = new Vector(s.Vectors[i].X + difx, s.Vectors[i].Y + diffy); } return(vector); }
//moves shape to left-bottom most location private void LeftBottom(List <MyShapes> myShapes, MyShapes s, int indexOfS, ref Vector vector) { Vector[] vectors = GetVectors(s, vector, indexOfS); bool b = true; double cx = .1, cy = .1; while (b && vector.X - cx >= 0) { for (int i = 0; i < vectors.Length; i++) { vectors[i].X -= .1; } if (!CanMove(myShapes, vectors)) { b = false; cx -= .1; for (int j = 0; j < vectors.Length; j++) { vectors[j].X += .1; } break; } cx += .1; } if (b) { cx -= .1; } b = true; while (b && vector.Y - cy >= 0) { for (int i = 0; i < vectors.Length; i++) { vectors[i].Y -= .1; } if (!CanMove(myShapes, vectors)) { b = false; cy -= .1; for (int j = 0; j < vectors.Length; j++) { vectors[j].Y += .1; } break; } cy += .1; } if (b) { cy -= .1; } vector -= new Vector(Math.Round(cx, 2), Math.Round(cy, 2)); }
} //length of all shapes //constructer public Algorithm(List <MyShapes> myShapes, MyShapes area) { shapes = myShapes; this.area = area; Succeeded = true; foreach (var item in shapes) { Length += item.Length; } OrderShapes(); Succeeded = SetShapes(); }
//places shapes public bool PlaceShape(MyShapes orbiting, ref MyShapes stationary, List <MyShapes> myShapes) { //step 1-place first shape if (stationary == null) { int indexOfX = 0; int indexOfY = 0; for (int p = 1; p < orbiting.Points.Length; p++) { if (orbiting.Points[p].X < orbiting.Points[indexOfX].X || (orbiting.Points[p].X == orbiting.Points[indexOfX].X && orbiting.Points[p].Y < orbiting.Points[indexOfX].Y)) { indexOfX = p; } if (orbiting.Points[p].Y < orbiting.Points[indexOfY].Y || (orbiting.Points[p].Y == orbiting.Points[indexOfY].Y && orbiting.Points[p].X < orbiting.Points[indexOfY].X)) { indexOfY = p; } } Points indexOnA = new Points(orbiting.Points[0].X - orbiting.Points[indexOfX].X, 0); PlaceOnArea(orbiting, 0, indexOnA); stationary = orbiting; return(true); } //step 2-place next shape int i = myShapes.Count - 1; Points OptimalPoint = new Points(-1, -1); while (i >= 0) { Points pointOnA = NTFFunctions(orbiting, myShapes[i], myShapes, area); if (pointOnA != null) { if (OptimalPoint.X == -1) { OptimalPoint = new Points(pointOnA.X, pointOnA.Y); } else if (pointOnA.X < OptimalPoint.X || pointOnA.X == OptimalPoint.X && pointOnA.Y < OptimalPoint.Y) { OptimalPoint = new Points(pointOnA.X, pointOnA.Y); } } i--; } if (OptimalPoint.X == -1) { return(false); } PlaceOnArea(orbiting, 0, OptimalPoint); //stationary = orbiting; return(true); }
private bool CheckBasicVector(Vector[] vectors, MyShapes s, MyShapes shap, int v, int indexOfS, int si, int indexOfO, int oi) { double x = 0; double y = 0; List <MyShapes> shapes = new List <MyShapes>(); shapes.Add(s); if (v == 1) { if (vectors[oi].X > vectors[indexOfO].X) { x = .5; } else if (vectors[oi].X < vectors[indexOfO].X) { x = -.5; } y = (vectors[oi].Y - vectors[indexOfO].Y) / (vectors[oi].X - vectors[indexOfO].X) * (vectors[indexOfO].X + x - vectors[oi].X) + vectors[oi].Y; y = vectors[indexOfO].Y - y; } else { if (s.PlacedVectors[si].X > s.PlacedVectors[indexOfS].X) { x = -.5; } else if (s.PlacedVectors[si].X < s.PlacedVectors[indexOfS].X) { x = .5; } y = (s.PlacedVectors[si].Y - s.PlacedVectors[indexOfS].Y) / (s.PlacedVectors[si].X - s.PlacedVectors[indexOfS].X) * (s.PlacedVectors[indexOfS].X + x - s.PlacedVectors[si].X) + s.PlacedVectors[si].Y; y = s.PlacedVectors[indexOfS].Y - y; } Vector vector = new Vector(x, y * -1); for (int i = 0; i < vectors.Length; i++) { vectors[i] -= vector; } if (!CanMove(shapes, vectors)) { return(true); } for (int i = 0; i < vectors.Length; i++) { vectors[i] += vector; } return(false); }
//moves every vertex of shape to location on which will be placed public Points[] GetPoints(MyShapes s, Points indexOnA, int indexOfF, Points trim = null) { if (trim == null) { trim = new Points(0, 0); } double difx = indexOnA.X - (s.Points[indexOfF].X + trim.X), diffy = indexOnA.Y - (s.Points[indexOfF].Y + trim.Y); Points[] vector = new Points[s.Points.Length]; for (int i = 0; i < vector.Length; i++) { vector[i] = new Points(s.Points[i].X + difx, s.Points[i].Y + diffy); } return(vector); }
public void addToList(int x, int y, Color c, SHAPE_TYPE type) { if (type == SHAPE_TYPE.Circle) { MyShapes.Add(new Circle(x, y, c)); } if (type == SHAPE_TYPE.Square) { MyShapes.Add(new Square(x, y, c)); } if (type == SHAPE_TYPE.Triangle) { MyShapes.Add(new Triangle(x - 10, y - 50, c, new Point(x + 50, y), new Point(x, y + 70))); } }
//shrinks length of area private void ShrinkingAlgorithm(List <MyShapes> MyShapes, MyShapes area) { //start from rightmost shape and try to place shape more in left CompareShapes r = new CompareShapes();//sorts shapes by location MyShapes.Sort(r); int c = MyShapes.Count - 1; int i = MyShapes.Count - 1; List <MyShapes> done = new List <MyShapes>(); List <MyShapes> myShapes = new List <MyShapes>(); bool d = false; while (i >= 0 && c > 0) { for (int k = 0; k < i; k++) { myShapes.Add(MyShapes[k]); } int OptimalO = -1; Vector OptimalA = new Vector(Length, 0); foreach (var item in done) { d = (item == MyShapes[i]) ? true : false; } if (!d) { for (int j = myShapes.Count - 1; j > i; j--) { MyShapes t = shapes[j]; GetLocation(MyShapes[i], myShapes[j], ref OptimalO, ref OptimalA, myShapes); if (OptimalO != -1) { PlaceShape(MyShapes[i], ref t, myShapes); MyShapes.Sort(r); c--; done.Add(MyShapes[i]); break; } c--; i++; } } } }
//places shapes on area public bool SetShapes() { double sum = 0; bool success = true; foreach (var item in shapes)//checks that every shape fits { if (item.Height > area.Height || item.Width > area.Width) { return(false); } sum += item.AreaOfS; } if (sum > area.AreaOfS) { return(false); //checks that total area of shapes is less than area of area } MyShapes stationary = null; List <MyShapes> myShapes = new List <MyShapes>(); foreach (var item in shapes) //places every shape on area { success = PlaceShape(item, ref stationary, myShapes); if (!success) { return(false); } myShapes.Add(item); //adds shape to placed shapes } ShrinkingAlgorithm(shapes); //shrinks area foreach (var item in shapes) //checks if shapes fit in area { foreach (var p in item.PlacedPoints) { if (p.X > area.Width) { return(false); } } } return(success); }
private void _mapEntitiesHub_OnAreaReceived(AreaDto areaDto) { MyShape myShape = new() { Id = areaDto.Id, Name = areaDto.Name, IsClosed = areaDto.IsClosed }; MyShape firstShape = MyShapes.FirstOrDefault(s => s.Id == myShape.Id); if (firstShape is null) { MyShapes.Add(myShape); } else { firstShape.IsClosed = myShape.IsClosed; } }
//eliminates NFP locations private void EliminateOptions(List <Pair> pairs, List <MyShapes> myShapes, MyShapes area, MyShapes orbiting, MyShapes s) { for (int i = 0; i < pairs.Count; i++) { Points[] points = GetPoints(orbiting, pairs[i].PointOnA, 0); bool removed = false; foreach (var item in points) { if (item.Y < 0 || item.X < 0) { pairs.RemoveAt(i--); removed = true; break; } } if (!removed && !CanPlace(myShapes, points, -1, Length)) { pairs.RemoveAt(i--); } } }
public async Task <bool> Run(int id) { Projects p = await repository.GetProjectR(id); if (p.Result.Count > 0) { return(true); } List <MyShapes> myShapes = new List <MyShapes>(); MyShapes area = new MyShapes(); foreach (var item in p.ProjectShapeConn) { Shapes s = await repository.GetShapeR(item.ShapeId); List <PointDTO> pointDTOs = new List <PointDTO>(); foreach (var pnt in s.Point) { pointDTOs.Add(mapper.Map <PointDTO>(pnt)); } if (item.Shape.Area == true) { area = new MyShapes(s.Id, s.Unit, pointDTOs); } else { myShapes.Add(new MyShapes(s.Id, s.Unit, pointDTOs)); } } Algorithm algorithm = new Algorithm(myShapes, area); if (algorithm.Succeeded == true) { foreach (var item in myShapes) { AddResult(item.IndexOfPoint.X, item.IndexOfPoint.Y, item.PointOnArea.X, item.PointOnArea.Y, item.Id, p.Id); } } return(algorithm.Succeeded); }
//places shapes on area public bool SetShapes() { double sum = 0; bool success = true; foreach (var item in shapes)//checks that every shape fits { if (item.Width > area.Width || item.Length > area.Length) { return(false); } sum += item.AreaOfS; } if (sum > area.AreaOfS) { return(false); //checks that total area of shapes is less than area of area } MyShapes t = null; List <MyShapes> myShapes = new List <MyShapes>(); foreach (var item in shapes) //places every shape on area { success = PlaceShape(item, ref t, myShapes); if (!success) { return(false); } myShapes.Add(item); } // ShrinkingAlgorithm(shapes, area); foreach (var item in shapes)//checks if shapes fit in area { if (item.PointOnArea.X + (item.Length - item.indexOfPoint.X) > area.Length) { return(false); } } return(success); }
private async void Initialize() { using AreaApi areaApi = new(); IEnumerable <AreaDto> areas = await areaApi.ReadAsync(); MyShapes.Clear(); foreach (AreaDto area in areas) { MyShape myShape = new() { Id = area.Id, Name = area.Name, IsClosed = area.IsClosed }; using AreaPointApi areaPointApi = new(); IEnumerable <AreaPointDto> areaPoints = await areaPointApi.ReadAsync(area.Id); if (areaPoints is null) { continue; } foreach (AreaPointDto areaPoint in areaPoints) { myShape.ShapePoints.Add(new ShapePoint() { Id = areaPoint.Id, AreaId = areaPoint.AreaId, Position = new Point(areaPoint.X, areaPoint.Y) }); } MyShapes.Add(myShape); } }
//trims translation vector private Points TrimTranslation(MyShapes orbiting, MyShapes stationary, int inds, Points indexOnA, int indexOfP) { //moves shape to next point Points[] Points = GetPoints(orbiting, indexOnA, indexOfP); Points intersectPoint = new Points(0, 0); for (int i = 0; i < Points.Length; i++) //checks if at any point orbiting shape overlaps with stationary shape { Points p1 = Points[i]; Points p2 = i == Points.Length - 1 ? Points[0] : Points[i + 1]; for (int j = 0; j < stationary.Points.Length; j++) { Points q1 = stationary.PlacedPoints[j]; Points q2 = j == stationary.Points.Length - 1 ? stationary.PlacedPoints[0] : stationary.PlacedPoints[j + 1]; Points vector; if (LineSegementsIntersect(q1, q2, p1, p2, out vector, false)) { if (!(Math.Round(vector.X) == Math.Round(q1.X) && Math.Round(vector.Y) == Math.Round(q2.Y)) && !(Math.Round(vector.X) == Math.Round(q2.X) && Math.Round(vector.Y) == Math.Round(q2.Y)) && !(Math.Round(vector.X) == Math.Round(p1.X) && Math.Round(vector.Y) == Math.Round(p1.Y)) && !(Math.Round(vector.X) == Math.Round(p2.X) && Math.Round(vector.Y) == Math.Round(p2.Y))) { Points realVector = IsInside(stationary.Points, stationary.Points.Length, p2, Length) ? new Points(p2.X - vector.X, p2.Y - vector.Y) : new Points(q2.X - vector.X, q2.Y - vector.Y); if (intersectPoint.X == 0 && intersectPoint.Y == 0) { intersectPoint = realVector; continue; } intersectPoint = Math.Sqrt(Math.Pow(indexOnA.X - (indexOnA.X - realVector.X), 2) + Math.Pow(indexOnA.Y - (indexOnA.Y - realVector.Y), 2)) > Math.Sqrt(Math.Pow(indexOnA.X - (indexOnA.X - intersectPoint.X), 2) + Math.Pow(indexOnA.Y - (indexOnA.Y - intersectPoint.X), 2)) ? intersectPoint : realVector; } } } } return(intersectPoint); }
private static Shape determineRandomShape(int x, int y) { Shape randomShape = null; Array values = Enum.GetValues(typeof(MyShapes)); theShape = (MyShapes)values.GetValue(RandomizerFactory.RandomVariable.Next(0, values.Length)); switch (theShape) { case MyShapes.Circle: randomShape = new Circle(x, y); break; case MyShapes.Square: randomShape = new Square(x, y); break; case MyShapes.SpottedCircle: randomShape = new SpottedCircle(x, y); break; } return(randomShape); }
//moves shape to left-bottom most location private void LeftBottom(List <MyShapes> myShapes, MyShapes shape, int indexOfS, ref Points vector) { Points helperPoint = new Points(vector.X, vector.Y); //places shape in current optimal location bool changed = true; while (changed) { Points[] Points = GetPoints(shape, vector, indexOfS); int MaxY = GetMaxY(Points); double CountX = 0, CountY = 0; //saves distance that can move shape Points HelperVector = new Points(Points[0].X, Points[0].Y); for (int i = 1; i < Points.Length; i++) //gets smallest points of the shape { if (Points[i].X < HelperVector.X) { HelperVector.X = Points[i].X; } if (Points[i].Y < HelperVector.Y) { HelperVector.Y = Points[i].Y; } } MoveLeft(HelperVector, ref CountX, Points, myShapes); changed = MoveDown(HelperVector, ref CountY, Points, myShapes); helperPoint -= new Points(Math.Round(CountX, 1), Math.Round(CountY, 1)); Points = GetPoints(shape, helperPoint, indexOfS); if (Points[MaxY].Y > area.Height) { CountY = CountX = 0; Points = GetPoints(shape, vector, indexOfS); changed = MoveDown(HelperVector, ref CountY, Points, myShapes); } vector -= new Points(Math.Round(CountX, 1), Math.Round(CountY, 1)); vector = new Points(Math.Round(vector.X, 1), Math.Round(vector.Y, 1)); } }
//places shapes public bool PlaceShape(MyShapes shap, ref MyShapes s, List <MyShapes> myShapes) { //step 1-place first shape if (s == null) { int indexOfF = 0; for (int p = 1; p < shap.Vectors.Length; p++) { if (shap.Vectors[p].X < shap.Vectors[indexOfF].X) { indexOfF = p; } else if (shap.Vectors[p].X == shap.Vectors[indexOfF].X && shap.Vectors[p].Y < shap.Vectors[indexOfF].Y) { indexOfF = p; } } Vector indexOnA = new Vector(0, area.Width - (area.Width - shap.Vectors[indexOfF].Y)); PlaceOnArea(shap, indexOfF, indexOnA); s = shap; return(true); } //step 2-place next shape int OptimalO = -1; Vector OptimalA = new Vector(Length, 0); GetLocation(shap, s, ref OptimalO, ref OptimalA, myShapes); if (OptimalO == -1) { return(false); } LeftBottom(myShapes, shap, OptimalO, ref OptimalA); PlaceOnArea(shap, OptimalO, OptimalA); s = shap; return(true); }
private void _mapEntitiesHub_OnAreaDeleted(AreaDto areaDto) { MyShape shape = MyShapes.FirstOrDefault(s => s.Id == areaDto.Id); MyShapes.Remove(shape); }
//NTF-get location on which to place shape private void GetLocation(MyShapes shap, MyShapes s, ref int OptimalO, ref Vector OptimalA, List <MyShapes> myShapes) { int i = 1, SIndexOfS = 0; for (; i < s.Vectors.Length; i++) { if (s.Vectors[i].Y < s.Vectors[SIndexOfS].Y) { SIndexOfS = i; } } int indexOfS = SIndexOfS, SindexOfO = 0; for (i = 1; i < shap.Vectors.Length; i++) { if (shap.Vectors[i].Y > shap.Vectors[SindexOfO].Y) { SindexOfO = i; } } int indexOfO = SindexOfO; bool firstMove = true; //condition to stop-returns to original location Vector OE = new Vector(-1, -1); //if sliding stopped before vertex of orbiting shape Vector SE = new Vector(-1, -1); //if sliding stopped before vertex of stationary shape int PCorrectVector = -1; int POCorrectVector = -1; int[][] PossibleMoves = new int[4][]; //saves all translations for (i = 0; i < 4; i++) { PossibleMoves[i] = new int[9]; } //0-statinary direction, 1-orbiting direction, 2-orbiting position, 3-translation derived vector //4-orbiting vertex change, 5-stationary vertex change, 6-translation vector, 7-stationary line, 8-orbiting line while (firstMove == true || !(indexOfS == SIndexOfS && indexOfO == SindexOfO)) { //save optimal location Vector staticp = SE.X == -1? (s.PointOnArea - s.indexOfPoint) + s.Vectors[indexOfS]: new Vector((s.PointOnArea - s.indexOfPoint).X + SE.X, (s.PointOnArea - s.indexOfPoint).Y + SE.Y); double below, above, left; if (OE.X == -1) { below = shap.Vectors[indexOfO].Y; above = shap.Width - shap.Vectors[indexOfO].Y; left = shap.Vectors[indexOfO].X; } else { below = OE.Y; above = shap.Width - OE.Y; left = OE.X; } if (CanPlace(myShapes, GetVectors(shap, staticp, indexOfO), s.Id, Length) && staticp.Y - below >= 0 && staticp.Y + above < area.Width && staticp.X - left >= 0) { if (staticp.X < OptimalA.X) { OptimalA = staticp; OptimalO = indexOfO; } else if (staticp.X == OptimalA.X && staticp.Y < OptimalA.Y) { OptimalA = staticp; OptimalO = indexOfO; } } //start=0, end=1, right=0, left=1, stationary=0, orbiting=1 BuildTable(ref PossibleMoves, shap, indexOfO, s, indexOfS, SE, OE); int correctTVector; if (SE.X != -1 || OE.X != -1) { correctTVector = 0; SE = OE = new Vector(-1, -1); } else { EliminatesTransVectors(ref PossibleMoves, s, shap, indexOfO, indexOfS); correctTVector = GetCorrectVector(PossibleMoves, PCorrectVector, POCorrectVector, s, shap); if (correctTVector == -1) { break; //no translation possible } PCorrectVector = PossibleMoves[correctTVector][7]; POCorrectVector = PossibleMoves[correctTVector][8]; Vector Trim = TrimTranslation(PossibleMoves[correctTVector], shap, s, indexOfS, indexOfO); if (Trim.X != -1) { if (PossibleMoves[correctTVector][3] == 1) { OE = new Vector(shap.Vectors[PossibleMoves[correctTVector][4]].X - Trim.X, shap.Vectors[PossibleMoves[correctTVector][4]].Y - Trim.Y); } else { SE = new Vector(s.Vectors[PossibleMoves[correctTVector][5]].X - Trim.X, s.Vectors[PossibleMoves[correctTVector][5]].Y - Trim.Y); } } } indexOfO = PossibleMoves[correctTVector][4]; indexOfS = PossibleMoves[correctTVector][5]; firstMove = false; } }
private void AddShape(object obj) { shape = new MyShapeModel(); MyShapes.Add(shape); }
private void BuildTable(ref int[][] PossibleMoves, MyShapes shap, int indexOfO, MyShapes s, int indexOfS, Vector SE, Vector OE) { int i = 0; Vector[] ShapMovedVector = GetVectors(shap, s.PlacedVectors[indexOfS], indexOfO); int previousO, nextO, previousS, nextS; previousS = indexOfS == 0 ? s.Vectors.Length - 1 : indexOfS - 1; nextS = indexOfS == s.Vectors.Length - 1 ? 0 : indexOfS + 1; previousO = indexOfO == 0 ? shap.Vectors.Length - 1 : indexOfO - 1; nextO = indexOfO == shap.Vectors.Length - 1 ? 0 : indexOfO + 1; if (SE.X == -1 && OE.X == -1) //get translation options { PossibleMoves[0][0] = PossibleMoves[0][1] = 1; //e, e PossibleMoves[0][2] = ShapMovedVector[previousO].X <= s.PlacedVectors[indexOfS].X ? 1 : 0; PossibleMoves[1][0] = 1; //e PossibleMoves[1][1] = 0; //s PossibleMoves[1][2] = ShapMovedVector[nextO].X <= s.PlacedVectors[indexOfS].X ? 1 : 0; PossibleMoves[2][0] = PossibleMoves[2][1] = 0; //s, s PossibleMoves[2][2] = ShapMovedVector[nextO].X <= s.PlacedVectors[indexOfS].X ? 1 : 0; PossibleMoves[3][0] = 0; //s PossibleMoves[3][1] = 1; //e PossibleMoves[3][2] = ShapMovedVector[previousO].X <= s.PlacedVectors[indexOfS].X ? 1 : 0; for (i = 0; i < 4; i++) //get vector derivision and eliminate vectors { if ((PossibleMoves[i][0] == 1 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 0) || (PossibleMoves[i][0] == 1 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 1)) { PossibleMoves[i][3] = 1; PossibleMoves[i][7] = indexOfS == 0 ? s.LinearEquation.Length - 1 : indexOfS - 1; PossibleMoves[i][8] = indexOfO == shap.LinearEquation.Length - 1 ? 0 : indexOfO + 1; } else if (PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 1) { PossibleMoves[i][3] = 1; PossibleMoves[i][7] = indexOfS == s.LinearEquation.Length - 1 ? 0 : indexOfS + 1; PossibleMoves[i][8] = indexOfO == shap.LinearEquation.Length - 1 ? 0 : indexOfO + 1; } else if (PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 0) { PossibleMoves[i][3] = 0; PossibleMoves[i][7] = indexOfS == s.LinearEquation.Length - 1 ? 0 : indexOfS + 1; PossibleMoves[i][8] = indexOfO == shap.LinearEquation.Length - 1 ? 0 : indexOfO + 1; } else if ((PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 1 && PossibleMoves[i][2] == 0) || (PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 1 && PossibleMoves[i][2] == 1)) { PossibleMoves[i][3] = 0; PossibleMoves[i][7] = indexOfS == s.LinearEquation.Length - 1 ? 0 : indexOfS + 1; PossibleMoves[i][8] = indexOfO == 0 ? shap.LinearEquation.Length - 1 : indexOfO - 1; } else { PossibleMoves[i][3] = PossibleMoves[i][7] = PossibleMoves[i][8] = -1; } } for (i = 0; i < 4; i++)//eliminate incorrect translation vector { if (PossibleMoves[i][3] == -1) { PossibleMoves[i][4] = PossibleMoves[i][5] = -1; } else if ((PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 1) || (PossibleMoves[i][0] == 1 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 0) || (PossibleMoves[i][0] == 1 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 1)) { PossibleMoves[i][4] = nextO; PossibleMoves[i][5] = indexOfS; PossibleMoves[i][6] = indexOfO; } else if ((PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 0 && PossibleMoves[i][2] == 0) || (PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 1 && PossibleMoves[i][2] == 0) || (PossibleMoves[i][0] == 0 && PossibleMoves[i][1] == 1 && PossibleMoves[i][2] == 1)) { PossibleMoves[i][4] = indexOfO; PossibleMoves[i][5] = nextS; PossibleMoves[i][6] = indexOfS; } } } else if (SE.X != -1) { for (int j = 0; j < 4; j++) { for (int j2 = 0; j2 < 7; j2++) { PossibleMoves[j][j2] = -1; } } PossibleMoves[0][4] = indexOfO; PossibleMoves[0][5] = nextS; } else { for (int j = 0; j < 4; j++) { for (int j2 = 0; j2 < 6; j2++) { PossibleMoves[j][j2] = -1; } } PossibleMoves[0][4] = nextO; PossibleMoves[0][5] = indexOfS; } }
//gets correct translation vector private int GetCorrectVector(int[][] PossibleMoves, int PCorrectVector, int POCorrectVector, MyShapes s, MyShapes shap) { int c = 0, i, corr = 0; for (i = 0; i < 4; i++) //checks if there are possibilities { if (PossibleMoves[i][3] != -1) { c++; corr = i; } } if (c == 0) { return(-1); } else if (c > 1) { c = -1; for (i = 0; i < 4; i++) { if (PossibleMoves[i][3] != -1 && c == -1) //compares all vectors to get optimal one { c = i; if (PCorrectVector == -1) { break; } continue; } int dp = 0; //distance from previous vector int dn = 0; //distance from current vector int comp = 0; //helper variable if (PossibleMoves[i][3] == 0) //stationary translation { int j = PossibleMoves[i][7]; while (j != PCorrectVector) { if (j == s.LinearEquation.Length) { j = 0; continue; } dn++; j++; } j = PossibleMoves[c][7]; while (j != PCorrectVector) { if (j == 0) { j = s.LinearEquation.Length - 1; continue; } comp++; j--; } dn = comp > dn ? dn : comp; j = PossibleMoves[i][7]; while (j != POCorrectVector) { if (j == shap.LinearEquation.Length) { j = 0; continue; } dp++; j++; } j = PossibleMoves[c][7]; while (j != POCorrectVector) { if (j == 0) { j = shap.LinearEquation.Length - 1; continue; } comp++; j--; } dp = comp > dn ? dn : comp; } if (PossibleMoves[i][3] == 1) //orbiting translation { int j = PossibleMoves[i][8]; while (j != PCorrectVector) { if (j == s.LinearEquation.Length) { j = 0; continue; } dn++; j++; } j = PossibleMoves[c][8]; while (j != PCorrectVector) { if (j == 0) { j = s.LinearEquation.Length - 1; continue; } comp++; j--; } dn = comp > dn ? dn : comp; j = PossibleMoves[i][8]; while (j != POCorrectVector) { if (j == shap.LinearEquation.Length) { j = 0; break; } dp++; j++; } j = PossibleMoves[c][8]; while (j != POCorrectVector) { if (j == 0) { j = shap.LinearEquation.Length - 1; continue; } comp++; j--; } dp = comp > dn ? dn : comp; c = dp > dn ? dp : dn; dp = dn = 0; } } corr = c; } return(corr); }
//trims translation vector private Vector TrimTranslation(int[] v, MyShapes o, MyShapes s, int inds, int indo) { double difx = s.Vectors[inds].X - o.Vectors[indo].X, dify = s.Vectors[inds].Y - o.Vectors[indo].Y; bool sd = v[3] == 0 ? true : false; Vector q1 = new Vector(); Vector q2 = new Vector(); Vector p1 = new Vector(); Vector p2 = new Vector(); Vector intersectPoint = new Vector(-1, -1); Vector vector = new Vector(); Vector realVector = new Vector(); Vector sPoint = new Vector(s.Vectors[inds].X, s.Vectors[inds].Y); Vector ePoint = new Vector(); Vector[] oVectors; if (sd) //if translated on stationary shape { if (inds == s.Vectors.Length - 1) { ePoint = new Vector(s.Vectors[0].X, s.Vectors[0].Y); } else { ePoint = new Vector(s.Vectors[inds + 1].X, s.Vectors[inds + 1].Y); } oVectors = GetVectors(o, ePoint, v[4]); } else { if (indo == o.Vectors.Length - 1) { ePoint = new Vector(o.Vectors[0].X + difx, o.Vectors[0].Y + dify); } else { ePoint = new Vector(o.Vectors[indo + 1].X + difx, o.Vectors[indo + 1].Y + dify); } oVectors = GetVectors(o, sPoint, v[4]); } for (int i = 0; i < o.LinearEquation.Length; i++) { p1 = oVectors[i]; if (i == o.LinearEquation.Length - 1) { p2 = oVectors[0]; } else { p2 = oVectors[i + 1]; } for (int j = 0; j < s.LinearEquation.Length; j++) { q1 = s.Vectors[j]; q2 = j == s.LinearEquation.Length - 1 ? s.Vectors[0] : s.Vectors[j + 1]; if (LineSegementsIntersect(q1, q2, p1, p2, out vector, false) && ((!sd && (vector.X != s.Vectors[inds].X && vector.Y != s.Vectors[inds].Y)) || (sd && (vector.X != s.Vectors[v[5]].X && vector.Y != s.Vectors[v[5]].Y)))) { if (IsInside(s.Vectors, s.Vectors.Length, p2, Length)) { realVector = new Vector(p2.X - vector.X, p2.Y - vector.Y); } else { realVector = new Vector(q2.X - vector.X, q2.Y - vector.Y); } if (intersectPoint.X == -1) { intersectPoint = realVector; continue; } intersectPoint = Math.Sqrt(Math.Pow(ePoint.X - (ePoint.X - realVector.X), 2) + Math.Pow(ePoint.Y - (ePoint.Y - realVector.Y), 2)) > Math.Sqrt(Math.Pow(ePoint.X - (ePoint.X - intersectPoint.X), 2) + Math.Pow(ePoint.Y - (ePoint.Y - intersectPoint.X), 2)) ? intersectPoint : realVector; } } } return(intersectPoint); }
public void EliminatesTransVectors(ref int[][] PossibleMoves, MyShapes s, MyShapes shap, int indexOfO, int indexOfS) { for (int i = 0; i < 4; i++) { if (PossibleMoves[i][3] == -1) { continue; } else { Vector[] vectors = GetVectors(shap, s.PlacedVectors[indexOfS], indexOfO); Vector distanceTrans = vectors[PossibleMoves[i][4]] - vectors[indexOfO]; Vector vector = new Vector(); int si; if (PossibleMoves[i][0] == 0) { si = indexOfS == s.Vectors.Length - 1 ? 0 : indexOfS + 1; } else { si = indexOfS == 0 ? s.Vectors.Length - 1 : indexOfS - 1; } int oi; if (PossibleMoves[i][1] == 0) { oi = indexOfO == shap.Vectors.Length - 1 ? 0 : indexOfO + 1; } else { oi = indexOfO == 0 ? shap.Vectors.Length - 1 : indexOfO - 1; } if (CheckBasicVector(vectors, s, shap, PossibleMoves[i][3], indexOfS, si, indexOfO, oi)) { PossibleMoves[i][3] = -1; continue; } vectors = GetVectors(shap, s.PlacedVectors[PossibleMoves[i][5]], indexOfO); for (int j = 0; j < vectors.Length; j++) { vectors[j] -= distanceTrans; } if (PossibleMoves[i][3] == 1) { if ((PossibleMoves[i][4] < indexOfO && indexOfO != vectors.Length - 1) || (PossibleMoves[i][4] == vectors.Length - 1 && indexOfO == 0)) { PossibleMoves[i][3] = -1; } else if (IsInside(s.PlacedVectors, s.PlacedVectors.Length, vectors[indexOfO], Length + area.Width, vectors[PossibleMoves[i][4]])) { PossibleMoves[i][3] = -1; } else { for (int j = 0; j < vectors.Length; j++) { int k = j == vectors.Length - 1 ? 0 : j + 1; if (LineSegementsIntersect(vectors[j], vectors[k], s.PlacedVectors[indexOfS], s.PlacedVectors[si], out vector, false) && vector.X != s.PlacedVectors[indexOfS].X && vector.Y != s.PlacedVectors[indexOfS].Y && vector.X != s.PlacedVectors[si].X && vector.Y != s.PlacedVectors[si].Y) { PossibleMoves[i][3] = -1; break; } } for (int j = 0; j < s.PlacedVectors.Length; j++) { int k = j == s.PlacedVectors.Length - 1 ? 0 : j + 1; if (LineSegementsIntersect(s.PlacedVectors[j], s.PlacedVectors[k], vectors[indexOfO], vectors[oi], out vector, false) && vector.X != vectors[indexOfO].X && vector.Y != vectors[indexOfO].Y && vector.X != vectors[oi].X && vector.Y != vectors[oi].Y) { PossibleMoves[i][3] = -1; break; } } } } else if (PossibleMoves[i][3] == 0) { if ((PossibleMoves[i][5] < indexOfS && indexOfS != s.Vectors.Length - 1) || (PossibleMoves[i][5] == s.Vectors.Length - 1 && indexOfS == 0)) { PossibleMoves[i][3] = -1; } if (IsInside(vectors, vectors.Length, s.PlacedVectors[indexOfS], Length, s.PlacedVectors[PossibleMoves[i][5]])) { PossibleMoves[i][3] = -1; } else { for (int j = 0; j < s.PlacedVectors.Length; j++) { int k = j == s.PlacedVectors.Length - 1 ? 0 : j + 1; if (LineSegementsIntersect(s.PlacedVectors[j], s.PlacedVectors[k], vectors[indexOfO], vectors[oi], out vector, false) && vector.X != vectors[indexOfO].X && vector.Y != vectors[indexOfO].Y && vector.X != vectors[oi].X && vector.Y != vectors[oi].Y) { PossibleMoves[i][3] = -1; break; } } for (int j = 0; j < vectors.Length; j++) { int k = j == vectors.Length - 1 ? 0 : j + 1; if (LineSegementsIntersect(vectors[j], vectors[k], s.PlacedVectors[indexOfS], s.PlacedVectors[si], out vector, false) && vector.X != s.PlacedVectors[indexOfS].X && vector.Y != s.PlacedVectors[indexOfS].Y && vector.X != s.PlacedVectors[si].X && vector.Y != s.PlacedVectors[si].Y) { PossibleMoves[i][3] = -1; break; } } } } if (PossibleMoves[i][3] != -1) { int c = 0; for (int j = 0; j < vectors.Length; j++) { if (IsInside(s.PlacedVectors, s.PlacedVectors.Length, vectors[j], Length + area.Width)) { c++; } } if (c == vectors.Length) { PossibleMoves[i][3] = -1; } } } } }
//places shape on area private void PlaceOnArea(MyShapes myShape, int indexOfF, Points indexOnA) { myShape.PlacedPoints = GetPoints(myShape, indexOnA, indexOfF); myShape.PointOnArea = indexOnA; myShape.IndexOfPoint = myShape.Points[indexOfF]; }
//places shape on area private void PlaceOnArea(MyShapes myShape, int indexOfF, Vector indexOnA) { myShape.PlacedVectors = GetVectors(myShape, indexOnA, indexOfF); myShape.PointOnArea = indexOnA; myShape.indexOfPoint = myShape.Vectors[indexOfF]; }