Evan Parsons - 3 months ago 22

C# Question

Okay, I am now getting the correct information from my current algorithm! However, with 700,000 polygons to check, it's just way too slow! The previous issue is fixed (My Line2D intersectsWith method was incorrect)

Now it's a matter of identifying my bottleneck! This algorithm is suppose to be O(nlog-n) so it should be much quicker. My intersectsWith method looks like it can't get any faster, however I will post its code, in case I'm wrong

**EDIT: Added IComparable interface**

My method for reading line segment intersections. Some code has been omitted for readability.

`public class Line2D : IComparable`

{

public Line2D(XYPoints p1, XYPoints p2)

{

}

public bool intersectsLine(Line2D comparedLine)

{

if ((X2 == comparedLine.X1) && (Y2 == comparedLine.Y1)) return false;

if ((X1 == comparedLine.X2) && (Y1 == comparedLine.Y2)) return false;

if (X2 == comparedLine.X1 && Y2 == comparedLine.Y1)

{

return false;

}

if (X1 == comparedLine.X2 && Y1 == comparedLine.Y2)

{

return false;

}

double firstLineSlopeX, firstLineSlopeY, secondLineSlopeX, secondLineSlopeY;

firstLineSlopeX = X2 - X1;

firstLineSlopeY = Y2 - Y1;

secondLineSlopeX = comparedLine.getX2() - comparedLine.getX1();

secondLineSlopeY = comparedLine.getY2() - comparedLine.getY1();

double s, t;

s = (-firstLineSlopeY * (X1 - comparedLine.getX1()) + firstLineSlopeX * (getY1() - comparedLine.getY1())) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);

t = (secondLineSlopeX * (getY1() - comparedLine.getY1()) - secondLineSlopeY * (getX1() - comparedLine.getX1())) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)

{

return true;

}

return false; // No collision

}

int IComparable.CompareTo(object obj)

{

//return Y1.GetHashCode();

Line2D o1 = this;

Line2D o2 = (Line2D)obj;

if (o1.getY1() < o2.getY1())

{

return -1;

}

else if (o1.getY1() > o2.getY2())

{

return 1;

}

else

{

if (o1.getY2() < o2.getY2())

{

return -1;

}

else if (o1.getY2() > o2.getY2())

{

return 1;

}

else

{

return 0;

}

}

}

}

The bulk of my algorithm implementation, I realize a List isn't the fastest for an algorithm, however I need indexing!:

`//Create a new list, sort by Y values.`

List<AlgEvent> SortedList = events.OrderBy(o => o.getY()).ToList();

List<Line2D> sweepline = new List<Line2D>();

for (var g = 0; g < SortedList.Count; g++)

{

if (SortedList[g].isStart)

{

Line2D nl = SortedList[g].line;

Line2D above;

/* Start generating above */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

if (index == -1)

{

above = null;

}

else

{

above = sweepline[index + 1];

}

}

catch (ArgumentOutOfRangeException)

{

above = null;

}

/* End generating above */

if (above != null)

{

if (above.intersectsLine(nl))

{

return true;

}

}

Line2D below;

/* Start generating below */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

below = sweepline[index - 1];

}

catch (ArgumentOutOfRangeException)

{

below = null;

}

/* End generating below */

if (below != null)

{

if (below.intersectsLine(nl))

{

return true;

}

}

sweepline.Add(nl);

sweepline = sweepline.OrderBy(o => o.getY1()).ToList();

}

else

{

Line2D nl = SortedList[g].line;

Line2D above;

Line2D below;

/* Start generating above */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

Console.Out.WriteLine("index:" + index);

//add 1 to get above line

above = sweepline[index + 1];

}

catch (ArgumentOutOfRangeException)

{

above = null;

}

/* End generating above */

/* Start generating below */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

below = sweepline[index - 1];

}

catch (ArgumentOutOfRangeException)

{

below = null;

}

/* End generating below */

sweepline = sweepline.OrderBy(o => o.getY1()).ToList();

sweepline.Remove(nl);

if (above != null && below != null)

{

if (above.intersectsLine(below))

{

return true;

}

}

}

Console.WriteLine("");

}

} // end numofparts for-loop

return false;

============================================

UPDATE: September 12th:

Implemented the TreeSet from C5, implemented IComparable to my classes, and slowed it down even more? I am still indexing it if that matters?

http://www.itu.dk/research/c5/

Code using TreeSet:

`TreeSet<Line2D> sweepline = new TreeSet<Line2D>();`

for (var g = 0; g < SortedList.Count; g++)

{

if (SortedList[g].isStart)

{

Line2D nl = SortedList[g].line;

Line2D above;

/* Start generating above */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

above = sweepline[index + 1];

}

catch (IndexOutOfRangeException)

{

above = null;

}

/* End generating above */

if (above != null)

{

if (above.intersectsLine(nl))

{

return false;

}

}

Line2D below;

/* Start generating below */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

below = sweepline[index - 1];

}

catch (IndexOutOfRangeException)

{

below = null;

}

/* End generating below */

if (below != null)

{

if (below.intersectsLine(nl))

{

return false;

}

}

sweepline.Add(nl);

//sweepline = sweepline.OrderBy(o => o.getY1()).ToList();

}

else

{

Line2D nl = SortedList[g].line;

Line2D above;

Line2D below;

/* Start generating above */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//Console.Out.WriteLine("index:" + index);

//add 1 to get above line

above = sweepline[index + 1];

}

catch (IndexOutOfRangeException)

{

above = null;

}

/* End generating above */

/* Start generating below */

try

{

//grab index in sweepline

int index = sweepline.IndexOf(nl);

//add 1 to get above line

below = sweepline[index - 1];

}

catch (IndexOutOfRangeException)

{

below = null;

}

/* End generating below */

//sweepline = sweepline.OrderBy(o => o.getY1()).ToList();

sweepline.Remove(nl);

if (above != null && below != null)

{

if (above.intersectsLine(below))

{

return false;

}

}

}

//Console.WriteLine("");

}

Answer

First, regarding the line intersection: you do not need the actual point of intersection, only to know if they intersect. See http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ for an algorithm that does just that.

About the `List`

implementation:

In your implementation using `List`

s, you call `indexOf`

on the sweepline to find `nl`

. This searches the sweepline from start to end. See `List(T).IndexOf`

. If you were to use the `BinarySearch`

method, that ought to speed up the search considerably.

List's documentation has a paragraph called performance considerations. They urge you to use a value type that implements `IEquatable<T>`

and `IComparable<T>`

. So, your `Line2D`

should probably be a `struct`

and implement these interfaces.

If you follow that advice, retrieval of the endpoint from the sweepline should be O(log n), which is sufficient for your purpose, and memory should be used more efficiently.

Insertion and removal are O(n) for Lists, cause the underlying array needs to be moved around in memory. A `SortedSet`

has faster insertion and removal, but I don't quite see how to find an item's neighbors in O(log n) there. Anyone? (See also Why SortedSet<T>.GetViewBetween isn't O(log N)?)

Anyways, the C5 `TreeSet`

should solve this.

I looked up the performance of IndexOf and [i] in the user guide and they're both listed as O(log n). So that is not supposed to be the issue. It's still probably somewhat faster, but no more than a fixed factor, to call the specific methods for finding the neighbors on the sweepline, i.e. Successor and Predecessor, which are also O(log n).

So

```
[...]
try
{
Line2D above = sweepline.Successor(nl);
if (above.intersectsLine(nl))
{
return false;
}
}
catch (NoSuchItemException ignore) { }
[...]
```

I don't like that they do not have a method that doesn't throw the exception, since throwing exceptions is very expensive. Your sweep line will be pretty full generally so my best guess is that failure to find one will be rare and calling `Successor`

is the most efficient way. Alternatively, you could keep calling `IndexOf`

like you do now, but check if it equals `Count`

minus one before retrieving `[index + 1]`

, and prevent the exception from being thrown at all:

```
[...]
int index = sweepline.IndexOf(nl);
if( index < sweepline.Count-1 )
{
Line2D above = sweepline[index + 1];
if (above.intersectsLine(nl))
{
return false;
}
}
[...]
```

Chapter two of the manual describes equality and comparison for C5 collections. Here, too, at the very least you must implement `IEquatable<T>`

and `IComparable<T>`

!

One more thought: you report feeding the algorithm 700000 lines. Could you start with timing for example 1000, 2500, 5000, 10000 lines and seeing how the algorithm scales for cases where they do not intersect?

On how to compare the lines on the sweepline:

You need to find some sort of natural ordering for the Line2Ds on the Sweepline TreeSet, since the CompareTo method asks you to compare one Line2D to another. One of the Line2Ds already sits in the Sweepline TreeSet, the other has just been encountered and is being added.

Your sweepline runs from bottom to top, I think:

```
List<AlgEvent> SortedList = events.OrderBy(o => o.getY()).ToList();
```

So let's say segment S1 got added to the TreeSet at event 1, and we wish to compare it to S2, which is being added at event 2, right now.

The line segments could possibly intersect at some point, which would change the ordering, but the algorithm will check for this right after inserting them, in the above and below checks. Which would perhaps better be called the left and right checks, come to think of it.

Anyways.. so the easiest would be to compare the bottom endpoints of both line segments. To the left is smaller, to the right is bigger. However, we need to look at the ordering at the position of the sweepline and they may have changed positions since then, like in the picture.

So we need to compare the bottom endpoint of S2 to the red point on S1, and see if it lies to the left or to the right of that point. It lies to the left so S2 is considered smaller than S1.

Usually it's simpler than this: If all of S1 lies to the left of S2's bottom endpoint, S1 is smaller than S2. If all of S1 lies to the right of S2's bottom endpoint, S1 is larger than S2.

I think you're looking for the typesafer version of the interface:

```
public class Line2D : IComparable<Line2D>
```

Assuming two properties `BottomY`

, the lowest of the two Y values, and `BottomX`

, the X value of the lowest endpoint, a somewhat tested attempt:

```
int IComparable<Line2D>.CompareTo(Line2D other)
{
if( BottomY < other.BottomY )
{
return -other.CompareTo(this);
}
// we're the segment being added to the sweepline
if( BottomX >= other.X1 && BottomX >= other.X2 )
{
return 1;
}
if( BottomX <= other.X1 && BottomX <= other.X2 )
{
return -1;
}
if( other.Y2 == other.Y1 )
{
// Scary edge case: horizontal line that we intersect with. Return 0?
return 0;
}
// calculate the X coordinate of the intersection of the other segment
// with the sweepline
// double redX = other.X1 +
// (BottomY - other.Y1) * (other.X2 - other.X1) / (other.Y2 - other.Y1);
//
// return BottomX.CompareTo(redX);
// But more efficient, and more along the lines of the orientation comparison:
return Comparer<Double>.Default.Compare(
(BottomX - other.X1) * (other.Y2 - other.Y1),
(BottomY - other.Y1) * (other.X2 - other.X1) );
}
```