0

I have a bit hard to explain question but I'll try to do my best.
I need a function to check if a random coordinate is in a 'row'.

An image to explain:enter image description here

Let's say size = 20 and between = 100
There are a endless amount of rows. And the coordinates are always an int (so are size & between)

How to chenk if a random coordinate is in a row or in the 'between' zone?
I call this method very often on the main thread of a program (not allowed to use aSync), what is the best/most efficient way to do this?

Thanks for helping!

Banjer_HD
  • 190
  • 2
  • 12
  • 1
    What's your definition of coordinate? (Is it an `int`, a `double`, a pair of doubles?) And what do you mean by "row"? Those look like columns to me; I thought rows were horizontal. – DodgyCodeException Nov 21 '19 at 15:58
  • 2
    Perhaps it would be better if you provide multiple examples with corresponding expected results. – pom Nov 21 '19 at 15:59
  • @DodgyCodeException Thanks for helping! My coordinate's are int's, sorry for not explaining it in the question (will edit). And yes I mean columns. – Banjer_HD Nov 21 '19 at 16:02
  • @user3682563 Thanks! Someone else already added an answer which I will test, so if that answer is right, I will accept it but if it is not, I will make multiple examples – Banjer_HD Nov 21 '19 at 16:04

2 Answers2

3

Since you didn't specify, the following assumes that you want the boundaries of [0, size] to be included in the "row". If not, update the <= accordingly.

First we can handle the case of the first row:

boolean isInFirstRow = 0 <= i && i <= size;

Now we update to handle the repeating nature of the rows:

int positionWithinSection = i % (size + between);
boolean isInRow = 0 <= positionWithinSection && positionWithinSection <= size;

Minor optimization: Since we are using mod, we know positionWithinSection will always be positive. And since each "row" begins at the start of each section, we don't have to check the lower bound at all:

int positionWithinSection = i % (size + between);
boolean isInRow = positionWithinSection <= size;
0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Thanks for your answer! I changed `positionWithinSection <= size` to `positionWithinSection < size` and `i <= size` to `i < size` and now it works almost perfectly for me! The only thing I am trying to do is to let it also work with negative coordinates. Will post it if I did it :) – Banjer_HD Nov 21 '19 at 16:32
  • Got it fully working now! For the people wondering: `isInFirstRow` isn't needed and in Java % returns the remainder not the modulo so `if(positionWithinSection < 0) positionWithinSection += (size + between);` will work ([source](https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers)). – Banjer_HD Nov 21 '19 at 17:43
2

Basically you need to establish length of period which is size + between. Than you need to take a phase in this period by taking % from length. Finally by investigating the modulo part you will know where in cycle you are.

Am I between given (between, size, coordinate)?: lenght := size + between positionInCycle := coordinate % length if(size > positionInCicle) return false other case, return true

Also note that if you don't start with "size" but rather a portion of a gap you can manipulate the answer by simply "renumbering" coordinates saying coordinate = coordinate - startlength of gap

Also it is worth noting that if you want the code to work with coordinates lower than 0 it is worth checking how exactly modulo works for negative numbers. And also there is one border condition in which both the between and size are 0 in which case the % operation is not well defined.

cerkiewny
  • 2,761
  • 18
  • 36