Generate random boxed co-ordinates in c# -
i need find random co-ordinates, bound square. defined values 70, 55, 175, 175 furthest points want go to:
north = utility.generaterandomnumber(utility.directions.north, 70); south = utility.generaterandomnumber(utility.directions.south, 55); east = utility.generaterandomnumber(utility.directions.east, 175); west = utility.generaterandomnumber(utility.directions.west, 175);
my generator below, have declare global static param:
public static random random = new random();
directions enumerator.
public static int generaterandomnumber(directions direction, int to) { if ((direction == directions.south) || (direction == directions.west)) return random.next(to * -1, 0); else return random.next(0, to); }
the function works fine , retrieve co-ordinates below:
north: 52 south: -13 east: 82 west: -105 north: 27 south: -45 east: 172 west: -117 north: 0 south: -37 east: 161 west: -160 north: 43 south: -39 east: 26 west: -174 north: 29 south: -7 east: 75 west: -125 north: 19 south: -51 east: 93 west: -49 north: 28 south: -20 east: 26 west: -28
the issue box built around (0,0,0,0) co-ordinate , i'm not sure how out of whilst ensuring north greater south, west left map , east right map.
i suggest change approach:
- generate point north/west corner, anywhere in appropriate range
- generate width , height, ensuring they're positive
- set east = west + width, , south = north - height
Comments
Post a Comment