System.Drawing.PieChart.PieChart3D.FindPieSliceUnderPoint C# (CSharp) Method

FindPieSliceUnderPoint() public method

Searches the chart to find the index of the pie slice which contains point given. Search order goes in the direction opposite to drawing order.
public FindPieSliceUnderPoint ( PointF point ) : int
point System.Drawing.PointF /// PointF point for which pie slice is searched for. ///
return int
        public int FindPieSliceUnderPoint(PointF point)
        {
            // first check tops
            for (int i = 0; i < m_pieSlices.Length; ++i) {
                PieSlice slice = (PieSlice)m_pieSlices[i];
                if (slice.PieSliceContainsPoint(point))
                    return (int)m_pieSlicesMapping[i];
            }
            // split the backmost (at 270 degrees) pie slice
            ArrayList pieSlicesList = new ArrayList(m_pieSlices);
            PieSlice[] splitSlices = m_pieSlices[0].Split(270F);
            pieSlicesList[0] = splitSlices[1];
            if (splitSlices[0].SweepAngle > 0F) {
                pieSlicesList.Add(splitSlices[0]);
            }
            PieSlice[] pieSlices = (PieSlice[])pieSlicesList.ToArray(typeof(PieSlice));
            int indexFound = -1;
            // if not found yet, then check for periferies
            int incrementIndex = 0;
            int decrementIndex = pieSlices.Length - 1;
            while (incrementIndex <= decrementIndex) {
                PieSlice sliceLeft = pieSlices[decrementIndex];
                float angle1 = 270 - sliceLeft.StartAngle;
                PieSlice sliceRight = pieSlices[incrementIndex];
                float angle2 = (sliceRight.EndAngle + 90) % 360;
                Debug.Assert(angle2 >= 0);
                if (angle2 < angle1) {
                    if (sliceRight.PeripheryContainsPoint(point))
                        indexFound = incrementIndex;
                    ++incrementIndex;
                }
                else {
                    if (sliceLeft.PeripheryContainsPoint(point))
                        indexFound = decrementIndex;
                    --decrementIndex;
                }
            }
            // check for start/stop sides, starting from the foremost
            if (indexFound < 0) {
                int foremostPieIndex = GetForemostPieSlice(pieSlices);
                // check for start sides from the foremost slice to the left
                // side
                int i = foremostPieIndex;
                while (i < pieSlices.Length) {
                    PieSlice sliceLeft = (PieSlice)pieSlices[i];
                    if (sliceLeft.StartSideContainsPoint(point)) {
                        indexFound = i;
                        break;
                    }
                    ++i;
                }
                // if not found yet, check end sides from the foremost to the right
                // side
                if (indexFound < 0) {
                    i = foremostPieIndex;
                    while (i >= 0) {
                        PieSlice sliceLeft = (PieSlice)pieSlices[i];
                        if (sliceLeft.EndSideContainsPoint(point)) {
                            indexFound = i;
                            break;
                        }
                        --i;
                    }
                }
            }
            // finally search for bottom sides
            if (indexFound < 0) {
                for (int i = 0; i < m_pieSlices.Length; ++i) {
                    PieSlice slice = (PieSlice)m_pieSlices[i];
                    if (slice.BottomSurfaceSectionContainsPoint(point))
                        return (int)m_pieSlicesMapping[i];
                }
            }
            if (indexFound > -1) {
                indexFound %= (m_pieSlicesMapping.Count);
                return (int)m_pieSlicesMapping[indexFound];
            }
            return -1;
        }