Sine and cosine functions?
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Sine and cosine functions?
Is it just me, or are we missing functions for sine and cosine?
I'm writing a function to handle scattering grenadelike objects, as per the GURPS 4e rules. I need to somehow get from "6 cells in direction 120°" (the result of the GURPS rules) to something MapTools can use with Wiki: moveToken(), which wants X/Y grid compatable instructions.
Which means that, continuing the example, I need the results of sine(120°)*6 (for deltaY) and cosine(120°)*6 (for deltaX) to pass to Wiki: moveToken().
Help?
I'm writing a function to handle scattering grenadelike objects, as per the GURPS 4e rules. I need to somehow get from "6 cells in direction 120°" (the result of the GURPS rules) to something MapTools can use with Wiki: moveToken(), which wants X/Y grid compatable instructions.
Which means that, continuing the example, I need the results of sine(120°)*6 (for deltaY) and cosine(120°)*6 (for deltaX) to pass to Wiki: moveToken().
Help?
- CoveredInFish
- Demigod
- Posts: 3104
- Joined: Mon Jun 29, 2009 10:37 am
- Location: Germany
- Contact:
Re: Sine and cosine functions?
Hurray!CoveredInFish wrote:They are not in the macro code base but available as UDF here in the forums.
Look here
- CoveredInFish
- Demigod
- Posts: 3104
- Joined: Mon Jun 29, 2009 10:37 am
- Location: Germany
- Contact:
Re: Sine and cosine functions?
I posted a "scatter" macro months ago that might help you as example.
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Sine and cosine functions?
I noticed this a while ago too. Note that if you're just using angles that are increments of 30 degrees, the sines and cosines are easy (plus or minus 0, 1/2, sqrt(3)/2, 1). Otherwise, there are plenty of tables and approximations, as I'm sure the links folks posted above show.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Sine and cosine functions?
Hmm. Related question. When using a hex grid, it seems (from the result of getTokenX() and getTokenY() on a token that has Snap To Grid turned on) the center of a hex is always at integer x and y coordinates. How exactly does that work, and how are we supposed to use it when doing distance/position calculations?
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Sine and cosine functions?
By the way, I found a defect. This code:
Results in "Illegal argument type java.lang.Integer, expecting java.math.BigDecimal or java.lang.String", but this code:
works fine. Same result if getTokenX() is passed an argument of 0 or 1.
(1.3.b66)
Code: Select all
[h: x = getTokenX()+1]
Code: Select all
[h: x = getTokenX()]
[h: x = x+1]
(1.3.b66)
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
Re: Sine and cosine functions?
prestidigitator wrote:Hmm. Related question. When using a hex grid, it seems (from the result of getTokenX() and getTokenY() on a token that has Snap To Grid turned on) the center of a hex is always at integer x and y coordinates. How exactly does that work, and how are we supposed to use it when doing distance/position calculations?
Yeah, I'm discovering that this is going to be more complicated than I thought, and I think this is the root of the problem. I expected to see results along the six faces, but they're often off slightly.
Re: Sine and cosine functions?
Fixed for b67. Including Wiki: getTokenY() which had the same problem.prestidigitator wrote:By the way, I found a defect. (1.3.b66)
Re: Sine and cosine functions?
Depending on what you're trying to accomplish, the best way to reduce round-off is to do your math so that you don't produce any.Mrugnak wrote:Yeah, I'm discovering that this is going to be more complicated than I thought, and I think this is the root of the problem. I expected to see results along the six faces, but they're often off slightly.
For example, moving from point A to point B on a hex grid:
Calculate the straight line distance from A to B. Move along the line in that direction by the size of one grid cell (let's call it 43 for a 50-pixel hex), allowing the token to "snap" into position. Query the coordinates of the new position and store into A. Rinse and repeat.
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Sine and cosine functions?
I wish that worked. I've tried this macro with a vertical hex grid. Any idea why it works for all directions but North-East and South-East?Azhrei wrote:Mrugnak wrote:For example, moving from point A to point B on a hex grid:
Calculate the straight line distance from A to B. Move along the line in that direction by the size of one grid cell (let's call it 43 for a 50-pixel hex), allowing the token to "snap" into position. Query the coordinates of the new position and store into A. Rinse and repeat.
Code: Select all
[h: USE_UNITS = 0]
[h: GRID_SIZE = 50]
[h: abort(input("dir|North,North-West,South-West,South,South-East,North-East|Choose a direction|LIST"))]
[h, switch (dir), code:
case 0:
{
[h: dx = 0]
[h: dy = -1]
};
case 1:
{
[h: dx = -sqrt(3)/2]
[h: dy = -1/2]
};
case 2:
{
[h: dx = -sqrt(3)/2]
[h: dy = 1/2]
};
case 3:
{
[h: dx = 0]
[h: dy = 1]
};
case 4:
{
[h: dx = sqrt(3)/2]
[h: dy = 1/2]
};
case 5:
{
[h: dx = sqrt(3)/2]
[h: dy = -1/2]
};
default:
{
[h: dx = 0]
[h: dy = 0]
}
]
[h: dist = if(USE_UNITS, GRID_SIZE, 1)]
[h: x = getTokenX(USE_UNITS)]
[h: x = x+dist*dx]
[h: y = getTokenY(USE_UNITS)]
[h: y = y+dist*dy]
[h: moveToken(x, y, USE_UNITS)]
Moved <[r: dx], [r: dy]>
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
Re: Sine and cosine functions?
If you're not using units, then your fractions get rounded down to zero.prestidigitator wrote:EDIT: Oh. Nevermind, almost. I just tried it again. It DOES work for all directions as long as you DO use units (USE_UNITS = 1 above). If you DON'T use units, North-East and South-East don't work, but the rest do.
Most likely. I haven't actually looked at the code very carefully. Sorry.
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Sine and cosine functions?
That makes sense. I did also note that, while the values are integer when you use getTokenX/Y(), using units doesn't result in simple rectangular grid coordinates being returned by them on a hex grid. So I guess it's just a matter of increasing the grid size for the desired precision. For example, moving one hex south-east on a vertical hex grid with a size of 50 went from <332, 300> to <375, 325>, which is as close a difference as we can get to <50*cos(pi/6), 50*sin(pi/6)> using integer values. I'm reasonably satisfied.Azhrei wrote:If you're not using units, then your fractions get rounded down to zero.prestidigitator wrote:EDIT: Oh. Nevermind, almost. I just tried it again. It DOES work for all directions as long as you DO use units (USE_UNITS = 1 above). If you DON'T use units, North-East and South-East don't work, but the rest do.
Most likely. I haven't actually looked at the code very carefully. Sorry.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton
Re: Sine and cosine functions?
The problem is here you're not writing a macro, you're waving a token around with your mouse. My problem isn't strictly the Moving from point a to point b, but finding out where point B should be in the first place...Azhrei wrote:For example, moving from point A to point B on a hex grid:
Calculate the straight line distance from A to B. Move along the line in that direction by the size of one grid cell (let's call it 43 for a 50-pixel hex), allowing the token to "snap" into position. Query the coordinates of the new position and store into A. Rinse and repeat.