Now, I have pseudovertices being generated and stored in arrays like this:

pseudovertexX[edgeID][pseudovertexID]

pseudovertexY[edgeID][pseudovertexID]

Lets take an example edge, edge 5.

pseudovertexX[5][pseudovertexID]

pseudovertexY[5][pseudovertexID]

This will contain a number of different points that occur along the edge.

Suppose the edge has the equation y = 2x + 3

So some sample points would be:

0, 3

-1.5, 0

2, 7

4, 11

-3, -3

Prior to sorting, they might appear like this:

pseudovertexX[5][pseudovertexID] = { 0, -1.5, 2, 4, -3 }

pseudovertexY[5][pseudovertexID] = { 3, 0, 7, 11, -3 }

Each contains 5 elements.

When I use **table.sort** or similar to sort these elements into the right order, they would be sorted independently. However because we are dealing with a straight line, this should not matter; they should naturally stay in sync with each other

pseudovertexX[5][pseudovertexID] = { -3, -1.5, 0, 2, 4 }

pseudovertexY[5][pseudovertexID] = { -3, 0, 3, 7, 11 }

So the elements then line up like this:

-3, -3

-1.5, 0

0, 3

2, 7

4, 11

So even though they were sorted independently, they didn't get mixed up.

This is always true for all possible lines in the engine.

It's even true of a horizontal or vertical.

Consider the line y = 4, which is horizontal.

A possible set:

pseudovertexX[5][pseudovertexID] = { 0, -1.5, 2, 4, -3 }

pseudovertexY[5][pseudovertexID] = { 4, 4, 4, 4, 4}

Sorted, becomes:

pseudovertexX[5][pseudovertexID] = { -3, -1.5, 0, 2, 4 }

pseudovertexY[5][pseudovertexID] = { 4, 4, 4, 4, 4}

In such a case, all the Y values are identical... so it doesn't matter that this situation mixes their indexes up.

Now, the question arises... can I sort pseudovertexY[edgeID][pseudovertexID] and only sort the pseudovertex ID's? Hmm, I suspect I will have to copy it to a seperate array first...

So that would be done in this way:

`function SortVertices()`

for edgeID = 0, numberofedges do

-- calculate number of pseudovertices in this edge

numverts = -1

sortedX = {}

sortedY = {}

for pvID = 0,100 do -- maybe change 100 to maxpseudovertices or whatever that variable is called... i set it in StarfieldInit()

if pseudovertexX[edgeID][pvID] ~= nil then

sortedX[pvID] = pseudovertexX[edgeID][pvID]

sortedY[pvID] = pseudovertexY[edgeID][pvID]

numverts = numverts + 1

else

break

end

end

-- numverts is now equal to the number of pseudovertices

-- ie if numverts = 0, there is one pseudovertex

-- if numverts = 1, there are two pseudovertices, etc

-- first check that there is at least two pseudovertices - if there are 1 or 0, there's nothing to sort.

if numverts > 0 then

table.sort(sortedX)

table.sort(sortedY)

for pvID = 0, numverts do

pseudovertexX[edgeID][pvID] = sortedX[pvID]

pseudovertexY[edgeID][pvID] = sortedY[pvID]

end

end

end

end

And that should sort the pseudovertices... hopefully..? :>