Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: LeChuck on Thu 03/04/2008 16:24:49

Title: Drawing random coordinate from a rawdraw line / region (SOLVED)
Post by: LeChuck on Thu 03/04/2008 16:24:49
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...
Title: Re: Drawing random coordinate
Post by: Radiant on Thu 03/04/2008 17:09:31
Please clarify what you mean by "drawing a coordinate"?

Something like RawDrawLine (Random (319), Random (199), Random (319), Random (199)) produces a random line.
Title: Re: Drawing random coordinate
Post by: LeChuck on Thu 03/04/2008 18:46:48
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?
Title: Re: Drawing random coordinate
Post by: Radiant on Thu 03/04/2008 21:50:43
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,


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.
Title: Re: Drawing random coordinate from a rawdraw line / region
Post by: Khris on Fri 04/04/2008 06:40:04
Or:
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.
Title: Re: Drawing random coordinate from a rawdraw line / region
Post by: LeChuck on Mon 07/04/2008 03:31:11
Quote from: KhrisMUC on Fri 04/04/2008 06:40:04
Or:
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?
Title: Re: Drawing random coordinate from a rawdraw line / region
Post by: Radiant on Mon 07/04/2008 08:50:45
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.
Title: Re: Drawing random coordinate from a rawdraw line / region (SOLVED)
Post by: Khris on Mon 07/04/2008 10:19:04
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.