Cem - 2 months ago 12

C# Question

I have datapoints in a MS chart and I would like to connect all these datapoints with lines, where none of the lines cross each other. Each datapoint will be connected to 2 other datapoints.

The plot should look like a closed loop, it is very important that there will be no lines that cross on top of another.

Is there an easy way to achieve this?

Answer Source

OK, I misspoke: It actually is not that hard, that is unless one wants more restrictions than what you gave. Here is a method that ought to work:

- Let's start with a list of points
- From it we create a
`List<Tuple<PointF, float, float>>`

- now we find the minimum and maximum x and y values and from these the center point
`c`

- Next we fill each Tuples' two floats by calculating the angel and the distance between each point and the center
`c`

- Then we order by angle and then by distance
- Now we can traverse the list and create
`DataPoints`

from it we add to our Series. - Finally we add the first one again at the end.

This ought to work without any crossings but I believe some results will look uglier than what a hand-crafted solution would be. In fact most examples will look like an explosion ;-)

Here is an example with 100 random points.