Just a few things that I noticed that may or may not have a significant impact:
The Vector2 for the start of the line isn't changing, and so can be assigned as soon as S is assigned. So you'd have
Vector2 lineStartPoint = new Vector2(S.ProjectedPosition.x, S.ProjectedPosition.y);
and change
ls = LineIntersect(new Line(new Vector2(S.ProjectedPosition.x, S.ProjectedPosition.y), new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
to
ls = LineIntersect(new Line(lineStartPoint, new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
in both of the points it is called.
Secondly, there is potential for recalculating whether S is inside for every E, which doesn't need to be done since S doesn't change in the middle of the foreach(E) loop. It could be calculated just under S's assignment and stored in a boolean.
bool SInside = Inside(i, S.ProjectedPosition);
and replace }else if (Inside(i, S.ProjectedPosition)){
with }else if (SInside){
Not sure if those will result in any noticeable changes to the speed, but they could help. You also have some variables that are used a lot within the loop that get destroyed during scope changes. Those could be declared outside of the loop and altered within it to keep it from allocating and deallocating the variables on scope change.
Example of how you could rearrange things:
// variable assignments
Vertex[] Outputs = new Vertex[7];
int OutputAdd = 0, numOutputs;
List<Vertex> outputs, inputList;
Vertex S;
Vector2 ls, SLineStart;
bool SInside;
foreach(IEnumerable<int> triangle in Triangles.Batch(3)){
outputs = triangle.Select(x => vs[x]).ToList();
numOutputs = outputs.Count;
//List<Vertex> outputs = new List<Vertex>(triangle.Select(x => vs[x]));
for (int i = 0; i < numOutputs; ++i){
//for (int i = 0; i < outputs.Count; i++){
Outputs[i] = outputs[i];
}
OutputAdd = numOutputs;
//OutputAdd = outputs.Count;
for(int i = 0; i < 4; i++){ // for each edge
intputList = Outputs.Take(OutputAdd).ToList();
///List<Vertex> inputList = new List<Vertex>(Outputs.Take(OutputAdd));
OutputAdd = 0;
if (inputList.Any()){
S = inputList.Last();
SInside = Inside(i, S.ProjectedPosition);
SLineStart = new Vector2(S.ProjectedPosition.x, S.ProjectedPosition.y);
//Vertex S = inputList.Last();
foreach (Vertex E in inputList){
if (Inside(i, E.ProjectedPosition)){
if (!SInside){
//if (!Inside(i, S.ProjectedPosition)){
//Vector2 ls = Vector2.zero;
ls = LineIntersect(new Line(SLineStart, new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
//ls = LineIntersect(new Line(new Vector2(S.ProjectedPosition.x, S.ProjectedPosition.y), new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
//outputs.Add(CalculateVertexFrom(S, E, ls));
Outputs[OutputAdd] = CalculateVertexFrom(S, E, ls);
OutputAdd++;
}
//outputs.Add(E);
Outputs[OutputAdd] = E;
OutputAdd++;
}else if (SInside){
//} else if (Inside(i, S.ProjectedPosition)){
//Vector2 ls = Vector2.zero;
ls = LineIntersect(new Line(SLineStart, new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
//ls = LineIntersect(new Line(new Vector2(S.ProjectedPosition.x, S.ProjectedPosition.y), new Vector2(E.ProjectedPosition.x, E.ProjectedPosition.y)), Edges[i]);
//outputs.Add(CalculateVertexFrom(S, E, ls));
Outputs[OutputAdd] = CalculateVertexFrom(S, E, ls);
OutputAdd++;
}
S = E;
}
}
}
}
I'll have to look through it a little more carefully on my next break to see if I can come up with anything else that may be causing slow downs.