C2 Math question

Get help using Construct 2

» Fri Feb 17, 2012 11:49 pm

Hello, I have a math question for you ;)

I have a rectangle within an object (so that the width/height of the rectangle are defined by object.width * k, object. width * k where k < 1)

I want to be able to pick a random position within the rectangle.

However, the object could be at any angle.

(the origin of the object is the centre point of the rectangle.)sqiddster2012-02-18 00:36:34
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

» Sat Feb 18, 2012 12:06 am

rectangle.x + int(random(-rectangle.width/2,rectangle.width/2))
rectangle.y + int(random(-rectangle.height/2,rectangle.height/2))newt2012-02-18 00:07:40
B
175
S
50
G
199
Posts: 8,625
Reputation: 124,026

» Sat Feb 18, 2012 12:36 am

@newt... this is what I normally do. The trick lies in the fact that the rectangle can be at any angle.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

» Sat Feb 18, 2012 12:52 am

Well haven't tested, but I would try
rectangle.x+ cos(rectangle.angle)*int(random(-rectangle.width/2,rectangle.width/2))

rectangle.y+ sin(rectangle.angle)*int(random(-rectangle.height/2,rectangle.height/2))
B
175
S
50
G
199
Posts: 8,625
Reputation: 124,026

» Sat Feb 18, 2012 1:48 am

Hmm, @newt, that doesn't seem to work...
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

» Sat Feb 18, 2012 2:34 am

newt's idea is to sample a value in an unrotated rectangle and then rotate the values arround the center.
Let see then :

sample values inside a rectangle :

[code]x = rectangle.x+random(-rectangle.Width/2,rectangle.Width/2)
y = rectangle.y+random(-rectangle.Height/2,rectangle.Height/2)[/code]

Then you rotate the value around the center

[code]xRot = rectangle.x + cos(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)
yRot = rectangle.y + sin(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)[/code]

We can simplify that by sampling the rectangle at 0,0 and then offsetting the restult

[code]x0 = random(-rectangle.Width/2,rectangle.Width/2)
y0 = random(-rectangle.Height/2,rectangle.Height/2)[/code]

And then
[code]xRot = rectangle.x + cos(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)
yRot = rectangle.y + sin(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)[/code]

So basically, sampling a random number in a rectangle is the same as chosing a random angle and distance from the center of this rectangle and applying a rotation.

In short it will be something like that:
[code]+On what you want
Local number randX=0 //random X position withing the rectangle
Local number randY=0 //random Y position withing the rectangle
Local number randA=0 //random angle before rotation
Local number randD=0 //random distance
-> System: set randX to random(-rectangle.Width/2,rectangle.Width/2)
-> System: set randY to random(-rectangle.Height/2,rectangle.Height/2)
-> System: set randA to angle(0,0,randX,randY)
-> System: set randD to distance(0,0,randX,randY)
-> System: set randX to rectangle.x + cos(randA + rectangle.angle) * randD
-> System: set randY to rectangle.y + sin(randA + rectangle.angle) * randD[/code]

Should work... Yann2012-02-18 02:37:36
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

» Sat Feb 18, 2012 8:08 am

Ah, so its get the rectangle's new angle, generate a random angle, get the difference between those, then generate an offset that is the same for both x, and y.
B
175
S
50
G
199
Posts: 8,625
Reputation: 124,026

» Sat Feb 18, 2012 5:12 pm

@newt
well, it's really generate a random point and then take it's angle AND distance from the origin.
It's not just a random angle
it's not just a random distance frome the origin
it's not event a random angle and a random distance, 'cause you have to stay within the rectangle boundaries. If you take a random angle and then a random distance from the origin (from 0 to how much?), you can't be sure you are within the rectangle. Angle and distance are linked to discribe a specific point.

So it's really a random point you convert into distance and angle from origin -> rotate -> translate.
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

» Sat Feb 18, 2012 6:00 pm

Humm, it's working, however at an entirely different point on the map.
Probably my fault, not Yann's ;)
I do rotate layers, but I doubt that really makes much difference.sqiddster2012-02-18 18:01:16
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

» Sat Feb 18, 2012 6:15 pm

Be carefull, the layerangle is a bit tricky. I would advise to not use it, especially if you use scrolling. It behave pretty weirdly.
I think that Ash did say somewhere that it's not the layer that rotate but something like, rotate layout -> paste Layer into main canvas -> rotate back. So event layerangle is reversed...
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

Next