C# with DirectX
i need a coding for sorting the lines which arranged in different order but they had contact each开发者_JAVA技巧 other these lines are stored in list.
Or you use a SortedList, and then you wil have automatically your items sorted, or you use a LinkedList and then your list is linked.
You can implement also your own LinkedList with a method that will return a SortedList from the available LinkedList members.
In order to sort objects in the Sorted List, you will need to implement a comparer (IComparer)(a way to tell what object(line in your case) is upper/lower than an other similar one).
"I need an algorithm for sorting lines which are arranged in different order but are making contact with each other. These lines are stored in list."
I think ratty is talking about geometry, specifically a series of start-end line coords, some of which presumably intersect each other.
If ratty means that he has a list of the edges of a polygon, and wants to sort them to get the closed shape, then this would work once translated to c# and using ratty's storage system:
from math import sqrt
origpoly = [( (0,0),(7,8) ),
( (7,8),(1,2) ),
( (1,2),(3,0) ),
( (3,0),(0,0) )]
shuffledpoly = [origpoly[2],origpoly[0],origpoly[3],origpoly[1]]
def r(vertex):
return sqrt(vertex[0]^2 + vertex[1]^2)
# dict of starting vertices, and find starting vertex nearest 0,0
startmap = {}
minstate = ( r(shuffledpoly[0][0]) , 0 )
for idx,pair in enumerate(shuffledpoly):
startmap[pair[0]] = idx
thisr = r(pair[0])
if(thisr<minstate[0]):
minstate = ( thisr, idx )
# begin with vertex nearest 0,0
sortedpoly = []
coords = shuffledpoly[minstate[1]][0]
for l in shuffledpoly:
idx = startmap[coords]
line = shuffledpoly[idx]
sortedpoly.append(line)
coords = line[1]
for line in sortedpoly:
print line
Output:
>>> ((0, 0), (7, 8))
((7, 8), (1, 2))
((1, 2), (3, 0))
((3, 0), (0, 0))
The Stony Brook algorithm repository has a lot of algorithms like this. Here is one that might be of interest for your problem: Intersection Detection.
精彩评论