c# - Single loop to get coordinates between two points -


the input consists of 2 coordinates (x|y) have same x or same y (meaning on same column or row).

i'd coordinates in between.

example input:

(2|2), (2|5)

example output:

(2|2), (2|3), (2|4), (2|5)

another example:

(2|2), (5|2)

example output:

(2|2), (3|2), (4|2), (5|2)

my approach check 4 conditions (whether y1 > y2 or x1 > x2 or y1 < y2 or x1 < x2) , use 4 separate loops.

here pseudo-code:

if (x1 > x2)     decrement x1 until x1 = x2, list coordinates else if (x1 < x2)     increment x1 until x1 = x2, list coordinates else if (y1 > y2)     decrement y1 until y1 = y2, list coordinates else if (y1 < y2)     increment y1 until y1 = y2, list coordinates 

i'm looking way in 1 loop though.

you finding out of variables needs increase , use max , min functions in loop:

int x1 = 1; int x2 = 6; int y1 = 3; int y2 = 5;  int min, max;  if (x1 == x2) {     min = math.min(y1,y2);     max = math.max(y1,y2); } else {     min = math.min(x1,x2);     max = math.max(x1,x2); }  (int = min; < max; ++i) {     if (x1 == x2) {         console.writeline(x1 + "/" + i);     else {         console.writeline(i + "/" + y1);     } } 

please note: havent done c# in long time , have no compiler here test, please edit code according c#. note: if coordinates same, program wont anything, there no error checking in above code! example code!


Comments