• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] Get evenly weighted, random point in rectangle

Status
Not open for further replies.
Level 6
Joined
Mar 7, 2011
Messages
124
I want to get an evenly weighted, random point in an angled rectangle. I was wondering if there are any libraries for shapes, particularly rectangles, available that can do that? Even better if it can also detect points being inside a rectangle. I don't have any big preference for how it defines rectangles internally. I don't think sampling random points and then rejecting ones outside the rectangle will work for performance reasons, but I could be overestimating things

The only thing I've found is Outside/Inside Polygon Detection System [Nhoek System] - Wc3C.net which might work, but I don't fully understand how it's implemented and the parts I do get make me a bit worried about how it'll do dynamically creating shapes. More importantly, I'm also not sure how I'd extend it's structure properly for my needs
 
Last edited:
[vJASS] - [System] Polygon
^Flux has included much polygon logics which you might use, when you want to rotate, move expand, intercect with other rects, etc.

I'm not a maths pro, but I wouldn't know a fast and efficient way to just instantly get a random location inside a polygon-- for rects I would maybe try to get a non-angled point, with simply maths logics, and then rotate it by angle theta so it gets random into my rotated rect.

If you want include own logics, and don't need polygons, but really only basic rect logics, then you might write something like PurplePoot used here: PurplePoot - Prismatic Spray
(under "Geometry" trigger -- he creates a trigometric rect, and then make straight forward checks if it contains a point -- tequnique I would use same as I said above, and I also do this rotation trick inside the LineSegmentEnumeration)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
The maths is very simple.
  1. Find evenly weighted random point in non rotated rectangle. This is just a random X and Y between the minimum and maximum extents.
  2. Rotate point around centre of the rectangle. This requires converting the point to be relative to the centre of the rectangle, multiplying it by a 2D rotation matrix and then offsetting it by the centre of the rectangle.
 
Level 11
Joined
Jun 2, 2004
Messages
849
Matrix math is not "very simple" for most people xD

Still relatively easy with some trigonometry though. Multiply the line length (from the starting point) by the sin of the angle of rotation for the y offset, and and by the cos of the angle for the x offset.
(trig was my worst math subject so apologies if I messed up the formula)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
Matrix math is not "very simple" for most people xD
The expansion of the rotation matrix with vector is used since it is only 2D and JASS does not support such maths natively.

As Wikipedia shows...
657b520ec337f95a996bc9e77f07401778d272af

ddafa97cf937c752708b51b3ba65d9e4e797e6c5

Or since the images do not work...
x' = x * cos(theta) - y * sin(theta)
y' = x * sin(theta) + y * cos(theta)

Both only need simple high school mathematics, taught when you are around 12-13.
 
Status
Not open for further replies.
Top