static float GetLineThroughProvinceColorDistance( int resolution, Texture2D provinceTex, Vector2Int[] lineSamples, Color32 operativeColorID) { Vector2Int firstEdge = Vector2Int.zero; Vector2Int secondEdge = Vector2Int.zero; bool foundFirstEdge = false; //Raycast both lines to determine which is longer. //raycast horizontal for (int i = 0; i < resolution; i++) { //See if PixelsXInt contains the operative sampleX Color32 col = provinceTex.GetPixel(lineSamples[i].x, lineSamples[i].y); if (MapTools.ColorIDEquals(col, operativeColorID)) { if (foundFirstEdge) { secondEdge = lineSamples[i]; } else { firstEdge = lineSamples[i]; foundFirstEdge = true; } } } return(Vector2Int.Distance(firstEdge, secondEdge)); }
static bool TryGetLineMidpointOverProvince(int resolution, Vector2Int[] lineSamples, Texture2D provinceTex, Color32 operativeColorID, out float distance, out Vector2Int midpoint) { Vector2Int firstEdge = Vector2Int.zero; Vector2Int secondEdge = Vector2Int.zero; bool foundFirstEdge = false; List <float> distances = new List <float>(); List <Vector2Int> firstEdges = new List <Vector2Int>(); List <Vector2Int> secondEdges = new List <Vector2Int>(); for (int i = 0; i < resolution; i++) { //See if PixelsXInt contains the operative sampleX Color32 col = provinceTex.GetPixel(lineSamples[i].x, lineSamples[i].y); //GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); //go.transform.localScale /= 30f; //go.transform.position = new Vector3(lineSamples[i].x * (100f / 8192f), 0f, lineSamples[i].y * (100f / 8192f)); if (MapTools.ColorIDEquals(col, operativeColorID)) { if (foundFirstEdge) { secondEdge = lineSamples[i]; } else { firstEdge = lineSamples[i]; foundFirstEdge = true; } } else { if (foundFirstEdge) { if (secondEdge != Vector2Int.zero) { distances.Add(Vector2.Distance(secondEdge, firstEdge)); firstEdges.Add(firstEdge); secondEdges.Add(secondEdge); foundFirstEdge = false; } else { distances.Add(0f); firstEdges.Add(firstEdge); secondEdges.Add(firstEdge); foundFirstEdge = false; } } } } if (distances.Count == 0 && firstEdge != Vector2Int.zero && secondEdge != Vector2Int.zero) { distances.Add(Vector2.Distance(secondEdge, firstEdge)); firstEdges.Add(firstEdge); secondEdges.Add(secondEdge); } int longestIndex = 0; float maxDistance = 0f; for (int i = 0; i < distances.Count; i++) { if (distances[i] > maxDistance) { maxDistance = distances[i]; longestIndex = i; } } //for (int i = 0; i < distances.Count; i++) //{ // GameObject go1 = GameObject.CreatePrimitive(PrimitiveType.Sphere); // go1.transform.localScale /= 10f; // go1.transform.position = new Vector3(firstEdges[i].x * (100f / 8192f), 0f, firstEdges[i].y * (100f / 8192f)); // GameObject go2 = GameObject.CreatePrimitive(PrimitiveType.Sphere); // go2.transform.localScale /= 10f; // go2.transform.position = new Vector3(secondEdges[i].x * (100f / 8192f), 0f, secondEdges[i].y * (100f / 8192f)); //} if (distances.Count > 0) { distance = distances[longestIndex]; midpoint = (firstEdges[longestIndex] + secondEdges[longestIndex]) / 2; //Debug.Log(firstEdges[longestIndex].ToString() + " " + secondEdges[longestIndex].ToString()); return(true); } else { distance = 0f; midpoint = Vector2Int.zero; return(false); } }
static void ImproveCenteringOfTwoPointCase(Vector2Int pA, Vector2Int pC, Texture2D provinceTex, Color32 colorId, out List <Vector2Int> newPoints, int resolution = 30) { newPoints = new List <Vector2Int>(3); List <Vector2Int> firstEdges = new List <Vector2Int>(); List <Vector2Int> secondEdges = new List <Vector2Int>(); List <float> distances = new List <float>(); bool firstEdgeFound = false; bool secondEdgeFound = false; Vector2Int firstEdgeTemp = Vector2Int.zero; Vector2Int secondEdgeTemp = Vector2Int.zero; Color32 col; Vector2Int vecAC = pC - pA; Vector2Int vecAB = vecAC / 2; for (int i = 0; i < resolution; i++) { float interpolate = -1f + (2f * ((float)i / (float)resolution)); Vector2Int newPointVec = pA + vecAB + new Vector2Int(Mathf.RoundToInt(vecAC.x * interpolate), Mathf.RoundToInt(vecAC.y * interpolate)); col = provinceTex.GetPixel(newPointVec.x, newPointVec.y); if (MapTools.ColorIDEquals(colorId, col)) { if (firstEdgeFound) { secondEdgeFound = true; secondEdgeTemp = newPointVec; } else { firstEdgeFound = true; firstEdgeTemp = newPointVec; } } else { if (firstEdgeFound) { if (!MapTools.ColorIDEquals(col, Color.black)) { if (secondEdgeFound) { //end the line drawing firstEdgeFound = false; secondEdgeFound = false; firstEdges.Add(firstEdgeTemp); secondEdges.Add(secondEdgeTemp); distances.Add(Vector2.Distance(secondEdgeTemp, firstEdgeTemp)); } else { //end the line drawing firstEdgeFound = false; firstEdges.Add(firstEdgeTemp); secondEdges.Add(firstEdgeTemp); distances.Add(0f); } } } } } //if we're done, but we were in the middle of drawing a line, finish that line if (firstEdgeFound) { firstEdges.Add(firstEdgeTemp); secondEdges.Add(secondEdgeTemp); distances.Add(Vector2.Distance(secondEdgeTemp, firstEdgeTemp)); } int longestIndex = 0; float maxDistance = 0f; for (int i = 0; i < distances.Count; i++) { if (distances[i] > maxDistance) { longestIndex = i; maxDistance = distances[i]; } } newPoints.Add(firstEdges[longestIndex]); newPoints.Add((secondEdges[longestIndex] + firstEdges[longestIndex]) / 2); newPoints.Add(secondEdges[longestIndex]); }