A Random Position Algorithm

Started by Calin Leafshade, Mon 12/10/2009 17:16:42

Previous topic - Next topic

Calin Leafshade

This is not really an AGS question, more of a general programming question.

How could i write a function which returns a random position within a certain region. If the region is square or rectangular its easy but what if its an irregular shape?

Crimson Wizard

#1
Quote from: Calin Leafshade on Mon 12/10/2009 17:16:42
This is not really an AGS question, more of a general programming question.

How could i write a function which returns a random position within a certain region. If the region is square or rectangular its easy but what if its an irregular shape?
Well, most rough method that comes to mind is to take a bounding rectangle's metrics as a base for random numbers throwing and make an extra check for hitting actual region afterwards. If check fails, random throw made again.
(Or nearest region pixel taken)

EDIT: Another way is to throw not X/Y coordinates but a single linear coordinate, say A, and calculate its position inside region afterwards by decreasing it by lengths of rows of pixels inside region starting from topmost one, until you find a row of pixels in which the coordinate lies...
But I guess it is far more cumbersome

Ryan Timothy B

#2
Lets assume your region is nearly the entire screen.

I would have a while statement that checks the screen for a random location, then checks if that particular location is of the region.  Then it would end the while statement.

Something like this (untested):

Code: ags

  bool istrue;
  while (istrue==false) {
    int r1=random(319)+1;
    int r2=random(239)+1;
    if (Region.GetAtRoomXY(r1, r2) == region[1]) {
      character[0].Move(r1, r2, eBlock);
      istrue==true;
    }
  }


I believe this is what you were asking...

Khris

Are you talking about an AGS region or some arbitrary polygon?

Ryan: To get a number from 0 to 319, all you need is Random(319).

monkey0506

Whereas the values you actually supplied (Ryan) return r1 and r2 values in the ranges of (respectively) 1-321 and 1-241.

Calin Leafshade

Well in practice i was talking about an AGS region. Randomly moving animated sprites around representing splashes from heavy rain.

But i was also wondering if there was some magic algorithm for generating a cartesian coordinate within certain bounds from a random seed.

Ryan Timothy B

#6
QuoteRyan: To get a number from 0 to 319, all you need is Random(319).
Sorry.  I could have sworn Random was just like Arrays.  Where array[20] would actually be 0-19.

And you wouldn't want to check 0 for X or Y considering it's off-screen (I believe), which is why I add the 1.
Anyway, I corrected the script I supplied then. :P


QuoteWell in practice i was talking about an AGS region. Randomly moving animated sprites around representing splashes from heavy rain.
Sorry. :) You lost me.


EDIT: But from what I gather, I believe what I had supplied would work for this method.  If I understand correctly, that is.  Except it wouldn't be:
Code: ags
character[0].Move(r1, r2, eBlock);


It would be:
Code: ags
object[0].X=r1;
object[0].Y=r2;


and you'd also need to animate it.
Code: ags
object[0].Animate(loop, delay, eOnce, eNoBlock);


If you made the splashes objects, just make them so you can't click them and give the final frame in the loop a blank image.  That way when it's done splashing, you don't have to hide the object. :P

Scarab

Well I think that exactly how irregular the polygon is is important for this, because It wouldn't be that hard if it were a rectangle in perspective, like a rhombus or trapezium (which I think most regions are anyway, or at least can be broken into these components)

If this is the case, you can do it by designating the corners and Random() function based on the maximum X and Y co-ordinates for the given area.

Scarab

Khris

Quote from: Ryan Timothy on Tue 13/10/2009 02:31:43And you wouldn't want to check 0 for X or Y considering it's off-screen (I believe), which is why I add the 1.

No, visible pixels are 0-319 and 0-239.
As with all graphical elements (except Objects), the upper-left corner is pixel (0;0).

The use of the Random function's parameter is a bit misleading though as Random(1) generates two different outcomes, Random(2) generates three, etc.

Crimson Wizard


Quote from: Calin Leafshade on Tue 13/10/2009 02:00:34
But i was also wondering if there was some magic algorithm for generating a cartesian coordinate within certain bounds from a random seed.

This may yet be possible, if you have a figure which can be described by functions.
For example, for common circle:

//  (x-x0)^2 + (y-y0)^2 = R^2;
//  (y-y0)^2 = R^2 - (x-x0)^2;
//  y-y0 = sqrt(R^2 - (x-x0)^2);
//  y = sqrt(R^2 - (x-x0)^2) + y0;

For example, if you have a circle which has 101 pixels along both axes, let's say, stretch from 0 to 100 inclusive with centre at (50,50), you may do following:

X = Random(100);
Ymax = sqrt(50^2 - (X - 50)^2) + 50;
Y = Random(Ymax);

Hopefully I did not fail math anywhere above. Anyway, this is simply a sample, I guess idea is understandable.

SMF spam blocked by CleanTalk