private List <StartPointClass> findNearestStartingPoints(Vector3 point, int num) { List <StartPointClass> list1 = startingPoints; List <StartPointClass> list2 = new List <StartPointClass>(); for (int i = 0; i < num; i++) { double min_value = Double.MaxValue; StartPointClass returnPoint = startingPoints[0]; foreach (var startPoint in list1) { if (startPoint.position == point) { continue; } bool isTrue = false; foreach (var startPoint2 in list2) { if (startPoint.position == startPoint2.position) { isTrue = true; break; } } if (isTrue) { continue; } if ((startPoint.position - point).magnitude < min_value) { min_value = (startPoint.position - point).magnitude; returnPoint = startPoint; } } list2.Add(returnPoint); } return(list2); }
private StartPointClass findNearestStartingPoint(Vector3 point, bool _withNoKind = false) { double min_value = quarterDistance; StartPointClass returnPoint = startingPoints[0]; foreach (var startPoint in startingPoints) { if (_withNoKind && startPoint.kind != 0) { continue; } if ((startPoint.position - point).magnitude * (1 / startPoint.sizeKoeff) < min_value) { min_value = (startPoint.position - point).magnitude; returnPoint = startPoint; } } return(returnPoint); }
//================================================= private void spawnRoad() { var rt = (RectTransform)Map.transform; var xMap = Map.transform.position.x; var yMap = Map.transform.position.y; var RoadQuat = Map.transform.rotation; var xPoints = 5f; var yPoints = 5f; var streetsFromPoint = 4; var l = 0; Vector3 RoadBeg; var neededTryes = 250; while (l < neededTryes) { l = 0; var xPlace = Random.Range(width * (1 / xPoints), width * ((xPoints - 1) / xPoints)); var yPlace = Random.Range(height * (1 / yPoints), height * ((yPoints - 1) / yPoints)); RoadBeg = new Vector3(xPlace, yPlace); while (checkStartingPointsToThis(RoadBeg)) { xPlace = Random.Range(Convert.ToSingle(quarterDistance1), Convert.ToSingle(width - quarterDistance1)); yPlace = Random.Range(Convert.ToSingle(quarterDistance1), Convert.ToSingle(height - quarterDistance1)); RoadBeg = new Vector3(xPlace, yPlace); l++; if (l >= neededTryes) { break; } } if (l < neededTryes) { startingPoints.Add(new StartPointClass(RoadBeg, 0)); } } ///Рассчитываем построения изначальных дорог foreach (var startPoint in startingPoints) { // if (startPoint.quat.Count == 0) // { List <StartPointClass> otherPoints = startingPoints; //выбираем ближайшие точки List <Vector3> posList = new List <Vector3>(); //posList.Add(startPoint.position); otherPoints.Sort((v1, v2) => (startPoint.position - v1.position).magnitude.CompareTo((startPoint.position - v2.position).magnitude)); int streetsFrom = Random.Range(streetsFromPoint - 1, streetsFromPoint + 1); //если две точки наодятся рядом друг с другом(например, 2 стороны реки), эта точка не тянется к более дальней for (int i = 1, j = 0; j < streetsFrom && i < otherPoints.Count; i++) { bool isNear = false; foreach (var point1 in posList) { if ((point1 - otherPoints[i].position).magnitude < quarterDistance) { isNear = true; } } if (isNear == true) { continue; } posList.Add(otherPoints[i].position); Quaternion q = new Quaternion(); Vector3 vectorToTarget = otherPoints[i].position - startPoint.position; float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg; q = Quaternion.AngleAxis(angle, Vector3.forward); if (startPoint.kind == 1) { Vector3 vectorToTarget2 = otherPoints[i].position - startPoint.position; float angle2 = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg; if (Math.Abs(Mathf.DeltaAngle(angle, angle2)) >= 90) { continue; } } ///я правда не знаю почему все дороги надо поворачивать на 90 градусов. startPoint.quat.Add(q * Quaternion.Euler(0, 0, -90)); j++; } if (startPoint.kind == 1 && startPoint.quat.Count > 1) { startPoint.quat.RemoveAt(0); } } StartPointClass startPoint1 = findNearestStartingPoint(new Vector3(width / 2, height / 2), true); startPoint1.kind = 5; startPoint1.sizeKoeff = 2; foreach (var StartPointer in startingPoints) { foreach (var quat in StartPointer.quat) { streetCounter++; var _street = new Street(); spawnedStreets.Add(_street); _street.Name = spawnedStreets.Count.ToString(); GenerateRoad(StartPointer.position, quat, rt, _street, true, 0 /*, true*/); } } }