/// <summary> /// Smooths the provided vertexsequence. Will iterate round all the vertices in the contour /// and will average out vertex positions based on thepositions of its neighbours. This will /// smooth out the edges of the contour and remove sharp angles. /// </summary> /// <param name="vs">The vertex sequence to smooth</param> private void SmoothContour(VertexSequence vs) { VertexSequence smooth = new VertexSequence(); for (int i = 0; i < vs.Count; i++) { float x = 0; float y = 0; int w = 0; for (int j = -m_smoothWeights.Length / 2; j <= m_smoothWeights.Length / 2; j++) { x += vs[i + j].x * m_smoothWeights[j + (m_smoothWeights.Length / 2)]; y += vs[i + j].y * m_smoothWeights[j + (m_smoothWeights.Length / 2)]; w += m_smoothWeights[j + (m_smoothWeights.Length / 2)]; } x /= w; y /= w; smooth.Add(new Vector2() { x = x, y = y }); } vs.CopyFrom(smooth); }
public void AddVertex(T x, T y, uint cmd) { m_status = Status.Initial; if (Path.IsMoveTo(cmd)) { m_src_vertices.ModifyLast(new VertexDist <T>(x, y)); } else { if (Path.IsVertex(cmd)) { m_src_vertices.Add(new VertexDist <T>(x, y)); } else { m_closed = (uint)Path.GetCloseFlag(cmd); } } }
private VertexSequence FindContour(int x, int y, int g, GroundChunk owner, Ground ground) { //Contour VertexSequence contour = new VertexSequence(); //Found a dot to trace int startx = x; int starty = y; int curx = x; int cury = y; int prevx = -1; int prevy = -1; int nextx = -1; int nexty = -1; int bits = 0; bool addPoint = true; //Predict an error with Marching Squares here bits = MarchingValue(curx, cury, g, ground.Dots); if (bits == 6 || bits == 9) { UnityEngine.Debug.LogWarning("MARCHING MIGHT CRASH"); } if (bits == 15) { UnityEngine.Debug.LogWarning("MARCHING IS STARTING IN ERROR!!!"); } do { nextx = curx; nexty = cury; bits = MarchingValue(curx, cury, g, ground.Dots); addPoint = true; switch (bits) { case 1: nexty -= 1; break; case 2: nextx += 1; break; case 3: nextx += 1; break; case 4: nextx -= 1; break; case 5: nexty -= 1; break; case 6: if (prevx == curx && prevy == cury + 1) { nextx -= 1; } else { nextx += 1; } break; //What if prev is null as we have just started, we could actually go the wrong way case 7: nextx += 1; break; case 8: nexty += 1; break; case 9: if (prevx == curx - 1 && prevy == cury) { nexty -= 1; } else { nexty += 1; } break; //What if prev is null as we have just started, we could actually go the wrong way case 10: nexty += 1; break; case 11: nexty += 1; break; case 12: nextx -= 1; break; case 13: nexty -= 1; break; case 14: nextx -= 1; break; case 15: nextx -= 1; addPoint = false; break; default: throw new ArgumentException(string.Format("Value was {0} prev was {1}, {2}", bits, prevx, prevy)); } if (addPoint) { if (contour.Count == 0) { startx = curx; starty = cury; } if (ground.Dots[cury, curx].Value == g) { ground.Dots[cury, curx].Chunk = owner.ID; } contour.Add(new Vector2() { x = curx - 0.5f, y = cury - 0.5f }); prevx = curx; prevy = cury; } curx = nextx; cury = nexty; }while (curx != startx || cury != starty); return(contour); }