Drawing random coordinate from a rawdraw line / region (SOLVED)

Started by LeChuck, Thu 03/04/2008 16:24:49

Previous topic - Next topic

LeChuck

I'm just wondering what the best way of drawing a random coordinate that lies on a Rawdraw line is? Is there a way for the engine to select a random coordinate from a region also? I've tried for quite some time but I don't quite know where to begin.

Edited to clear things up a bit...

Radiant

Please clarify what you mean by "drawing a coordinate"?

Something like RawDrawLine (Random (319), Random (199), Random (319), Random (199)) produces a random line.

LeChuck

Ah, sorry about not being clear enough. Let's say there's a rawdraw line going across the screen. Now, if I want AGS to select a random coordinate on this rawdraw line, how do I proceed? Also, is there a similar way to draw a random coordinate from a region?

Radiant

Quote from: LeChuck on Thu 03/04/2008 18:46:48
Now, if I want AGS to select a random coordinate on this rawdraw line,

Code: ags

function RandomPointOnLine (int x, int y, int x2, int y2) {
  int r = Random (100);
  int rx = (((x2 - x) * r) / 100) + x;
  int ry = (((y2 - y) * r) / 100) + y;
  // do something with point (rx, ry);
}


Quote
Also, is there a similar way to draw a random coordinate from a region?
Yes. If the region is a straight rectangle, same method but pick two different random numbers, one for x and one for y. If the region is crooked, that would involve advanced mathematics.

Khris

Or:
Code: ags
function RandomPointOnRegion(int reg) {
  int x = -1;
  int y = -1;
  while(Region.GetAtRoomXY(x, y) != reg) {
    x = Random(319);
    y = Random(199);
  }
  // use x, y
}


Edit: removed "int"s in loop.

LeChuck

Quote from: KhrisMUC on Fri 04/04/2008 06:40:04
Or:
Code: ags
function RandomPointOnRegion(int reg) {
  int x = -1;
  int y = -1;
  while(Region.GetAtRoomXY(x, y) != reg) {
    int x = Random(319);
    int y = Random(199);
  }
  // use x, y
}


Thanks a lot guys for helping out. Question for you though KhrisMUC: Won't your code slow the engine down? Especially when running your code with small regions?

Radiant

Quote from: LeChuck on Mon 07/04/2008 03:31:11
Thanks a lot guys for helping out. Question for you though KhrisMUC: Won't your code slow the engine down? Especially when running your code with small regions?

Yes, that is likely. The solution is to hard-code two points that mark the absolute edges of the region.

Khris

Additionally, one could use only even coords:

x = Random(159)*2;
y = Random(99)*2;

If the function is used frequently, this will increase its performance by the factor 4.

SMF spam blocked by CleanTalk