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...
Please clarify what you mean by "drawing a coordinate"?
Something like RawDrawLine (Random (319), Random (199), Random (319), Random (199)) produces a random line.
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?
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.
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.
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?
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.
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.