Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. A more effective way to find the number of points with a given distance between them on a line?

A more effective way to find the number of points with a given distance between them on a line?

Scheduled Pinned Locked Moved Algorithms
helpquestion
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik Rude
    wrote on last edited by
    #1

    Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.

    public int FindMaxM(double minx, double maxx, double delta)
    {
    int m;
    while (minx + m * delta < maxx)
    {
    m++;
    }
    return m;
    }

    My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?

    public int FindMaxM(double minx, double maxx, double delta)
    {
    int m = (int) (((maxx - minx)/delta));
    //if the division is resulting in an actual integer reduce the gridsize
    if (minx + m * delta >= maxx)
    {
    m--;
    }
    return m;
    }

    Richard Andrew x64R I L 3 Replies Last reply
    0
    • E Erik Rude

      Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.

      public int FindMaxM(double minx, double maxx, double delta)
      {
      int m;
      while (minx + m * delta < maxx)
      {
      m++;
      }
      return m;
      }

      My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?

      public int FindMaxM(double minx, double maxx, double delta)
      {
      int m = (int) (((maxx - minx)/delta));
      //if the division is resulting in an actual integer reduce the gridsize
      if (minx + m * delta >= maxx)
      {
      m--;
      }
      return m;
      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Just off the cuff I would say:

      m = (int)((maxx-minx) / delta);

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • E Erik Rude

        Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.

        public int FindMaxM(double minx, double maxx, double delta)
        {
        int m;
        while (minx + m * delta < maxx)
        {
        m++;
        }
        return m;
        }

        My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?

        public int FindMaxM(double minx, double maxx, double delta)
        {
        int m = (int) (((maxx - minx)/delta));
        //if the division is resulting in an actual integer reduce the gridsize
        if (minx + m * delta >= maxx)
        {
        m--;
        }
        return m;
        }

        I Offline
        I Offline
        IdUnknown
        wrote on last edited by
        #3

        I am not paid by lines, so I might do something like this.

        return --((int)Math.Ceiling((maxx - minx) / delta));

        1 Reply Last reply
        0
        • E Erik Rude

          Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.

          public int FindMaxM(double minx, double maxx, double delta)
          {
          int m;
          while (minx + m * delta < maxx)
          {
          m++;
          }
          return m;
          }

          My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?

          public int FindMaxM(double minx, double maxx, double delta)
          {
          int m = (int) (((maxx - minx)/delta));
          //if the division is resulting in an actual integer reduce the gridsize
          if (minx + m * delta >= maxx)
          {
          m--;
          }
          return m;
          }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          The answers so far probably don't satisfy the "without touching the maxx" requirement; it may depend on your definition of "touching". You might get away with it by subtracting a tiny fraction of delta from maxx before using the division approach... :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups