首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试某个点是否在某个范围内的例程。

测试某个点是否在某个范围内的例程。
EN

Stack Overflow用户
提问于 2022-01-30 11:27:47
回答 1查看 61关注 0票数 0

有人能帮我吗?

对于C#

我需要实现一个例程来测试某个点是否在一个范围内(数组),C#

图中的范围是1-3,6-9,11-15。

我想使用二进制搜索,我使用了它,但有一个缺陷,因为它只能测试2个范围在这种情况下.如果我有2个范围,那么二进制搜索工作得很好,因为它给出了两个范围的IComparer中的两个事件。(1-3,6-9)

在我添加3个范围后,二进制搜索只给我两个范围,6-9,11-15

我用的是List> range,IComparer>

差不多吧

代码语言:javascript
复制
class RangeComparerPoint : IComparer<Tuple<int, int>>
    {
        public int Compare(Tuple<int, int> f1, Tuple<int, int> f2)
        {
            //for the sake of clarity
            int boundary_1 = f1.Item1;
            int boundary_2 = f1.Item2;
            int pos = f2.Item1;
            int currPos = f2.Item2;

            //EndSection 
            if (pos > currPos)
            {
                if (pos >= boundary_1 && currPos < boundary_1)
                {
                    //in the range
                    return 0;
                }
            }
            else
            {
                if ( boundary_1 > currPos )
                {
                    return -1;
                }

                if (pos <= boundary_1)
                {
                    //in the range
                    return 0;
                }
            }
            return -1;
        }
    }

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-30 13:54:16

你需要更精确地描述这个问题.

  1. 范围可能不重叠(或实现逻辑来合并它们)
  2. 范围都是封闭区间,即1..3表示1 <= x <= 3

而且你不考虑(例如)[1.4],意思是1 <= x<4

  • 整数,等等,

将这些假设和一些需要您搜索Tuple<a,a>a值(参见Comp()方法中的异常)作为假设,下面是如何实现您所追求的目标的示例.

代码语言:javascript
复制
using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp2
{
  class Program
  {
    // x represents a point and y a range 
    // (params are swapped if necessary, i.e. if y is a point and x a range)
    // returns 0 if x falls within the range represented by y
    // returns -1 if x falls before y on the number line, +1 if it falls after y
    public static int Comp(Tuple<int, int> x, Tuple<int, int> y)
    {
      var fSwap = 1;
      if (x.Item1 != x.Item2) 
      {
        (x, y) = (y, x); // swap so that x is always the value being searched for
        fSwap = -1;
      }
      if (x.Item1 != x.Item2) // ***
        throw new Exception("The value being sought MUST be new Tuple(a,b) where a == b");
      return fSwap * (x.Item1 < y.Item1 ? -1 : x.Item1 > y.Item2 ? +1 : 0);
    }

    static void Main(string[] args)
    {
      // must be non-overlapping and increasing
      var ranges = new List<Tuple<int, int>>
      {
        new Tuple<int, int>(1, 3),
        new Tuple<int, int>(6, 9),
        new Tuple<int, int>(11,15)
      };

      for (int i = 0; i < 20; i++)
      {
        var foundIn = ranges.BinarySearch(new Tuple<int, int>(i, i), Comparer<Tuple<int, int>>.Create(Comp));
        if (foundIn < 0)
          Console.WriteLine($"{i} does not fall in any range");
        else
          Console.WriteLine($"{i} falls within [{ranges[foundIn].Item1},{ranges[foundIn].Item2}]");
      }
      Console.ReadKey();
    }
  }
}

给结果..。

代码语言:javascript
复制
0 does not fall in any range
1 falls within [1,3]
2 falls within [1,3]
3 falls within [1,3]
4 does not fall in any range
5 does not fall in any range
6 falls within [6,9]
7 falls within [6,9]
8 falls within [6,9]
9 falls within [6,9]
10 does not fall in any range
11 falls within [11,15]
12 falls within [11,15]
13 falls within [11,15]
14 falls within [11,15]
15 falls within [11,15]
16 does not fall in any range
17 does not fall in any range
18 does not fall in any range
19 does not fall in any range
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70914161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档