Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Code optimization...

Author
Message
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 03:38
Need some help here. I can't figure out an efficient way to do this. I got the basic code that works done, but it has WAY too many if thens and it is generally an eyesore.

here it is.

The idea is that it will number spaces on a board. The pieces are rectangular and the corners are square (it is a monopoly board). I just can't seem to figure out a clean way to put the numbers in the center of the different spaces.

Thanks in advance!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Jan 2009 04:16
Well, I feel it on the tip of my brain but just can't solve it at the moment, if you could adjust the radius properly to find the limits in a rectangle, you could use polar math to get all the center positions:



Enjoy your day.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 04:24
I was thinking something along those lines, I just got stuck because of the straight movement. It seems like such a simple problem in my head, I just can't figure out an approach to it !

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Jan 2009 04:48
Going along this line, I think the 'cure' I'm trying is worse than the illness! Anyway, if we know the size of the whole board in terms of x and y, we can calculate the diagonals. If we take half of the diagonal (hD) and half of say y (hY) , we take the difference of hD-hY and divide that by half the number of board squares in the top row. We now have the increment amount for the radius. We increase the radius as it approaches hD then decrease it after it reaches hD until it equals hY or hX depending on whether we're drawing boxes on the x side or the y side.

Makes sense as I write it, but now to implement it... It seems like a lot of work but once it's done, you'd only have to supply the number of squares, an x radius, and a y radius, and you should be able to find equally distributed positions for any rectangle or square - in theory at this point!

Enjoy your day.
Kira Vakaan
17
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 23rd Jan 2009 06:42
How about this? I suppose it's kind of like your first idea with the conditionals... but it's kinda neat, nonetheless...

BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 06:52
Interesting approach Kira, I had completely forgotten about that trick! I will see if I can make work.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Quadrazar
17
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 23rd Jan 2009 10:42 Edited at: 23rd Jan 2009 11:06
this uses a binary patern to determine the direction to increment the current position

it uses less magic numbers (numbers not declared in a varable) and uses less computations.



on a monopoly board we go
fist in -X directon (start -> jail),
then -Y direction (jail -> free parking),
then +X directon (free parking -> go to jail)
then +Y direction (back to start)

this gives us a sequence of
-1 0
0 -1
1 0
0 1

In the code the binary starting value is 5. for each axis we ad 17. this creates a parern we can use.

000000101 in the first loop we ad 0001 0001 (decimal = 17)

0001 0110 + 17 (bin 0001 0001)
0010 0111 + 17
0011 1000 + 17
0100 1001

###X ##SY

X = Xdirectionbit
Y = Ydirectionbit
S = Positive/negative bit (0 to -1 and 1 to +1)
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 17:40
Genius Quadrazar. Just genius.

One question on the code, though. How does the math in this section work (or do I just need to review my Boolean operators)?


Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Jan 2009 17:59
Just a note: 'inc' is a keyword. Change it to something like posinc.

Enjoy your day.
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 23rd Jan 2009 20:01 Edited at: 23rd Jan 2009 20:02
BN2 you can try using a function to draw it instead, something like:


This way if you want to add more onto the board it's pretty easy too. This can also do diagonals as well.
Quadrazar
17
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 23rd Jan 2009 20:02 Edited at: 23rd Jan 2009 20:17
thanks,

sign = 1-(2 & directionBin)

(directionBin & 2) will return 0 or 2.
We want to convert it in -1 of +1. We simply substact the result of 1.

sign = 1 - (2) = -1
sign = 1 - (0) = 1


incX = sign * (directionBin & 16) / 16 * posInc

(directionBin & 16) will return 0 or 16.
( result 0 or 16 )
To convert it to 0 or 1, just divide it by 16.
( result 0 or 1 )
Multiply by the sign to see if the incX is to the left or the right.
( result 0 or +1 or -1 )
Multiply by the posInc to the get the distance.
( result 0 or +posInc or -posInc )

do the same for incY = sign * (directionBin & 1) * posInc, only you don't have to divide because (directionBin & 1) already returns 1 or 0.


limit = limit + 10
every ten squares, you have to change the incX and incY. This is done by using the limit variable. You could change the 10 into a variable like boardSize, it's still a magic number that slipped in.

note: in order to get the zero at the startsquare first draw the text, then alter the positionX possitionY.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 21:03
Thanks, that helps a lot. Will integrate it later today and let you know how it works.

@Ashingda
Thanks, but unfortunately, the numbers were just so that I could SEE what it was doing. The actual code will be used for positioning the players.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Quadrazar
17
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 23rd Jan 2009 21:12 Edited at: 23rd Jan 2009 23:11
Nice one Ashingda,
I'wish I tought of that. Verry simple & reusable.

note: I think you can speed up execution by placing the sin(b)*lp outside the loop. (compute it once, store it in a varable, reuse the variable). Also I read on the forum (can't remember where) that (var^2) is slower then (var*var).



you could even write



since sin(x)+cos(x) alsways equals 1
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 23rd Jan 2009 21:35 Edited at: 23rd Jan 2009 21:43
@Quadrazar
Ohh had no idea (^) was slower than (*) thanks.

I read somewhere and tested it myself, Floats are much slower than Integers. Having incX = sin(b) would need incX to be a float.


@BN2
If you're positioning the players maybe you can try a WayPoint?

Call the Waypoint position depending on how far the player has moved/traveled.


[edit]

Wow it's true (*) is way faster than (^)
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Jan 2009 21:45
I am sort of using a waypoint system. I don't want to hard code all of the positions for the spaces into the code, though, so I want a function (or two if necessary) that will calculate the location so that I can put a waypoint there and then have the player move to it.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Quadrazar
17
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 23rd Jan 2009 23:57 Edited at: 24th Jan 2009 03:39
@Ashingda:
Quote: "Having incX = sin(b) would ..."


hm, made a mistake, code should be:


[EDIT] : the following is not true!
or even better
since sin(x) + cos (x) always equals 1.
[EDIT] : as BN2 pointed out (sin(x) * sin(x)) + (cos (x)*cos (x)) always equals 1.

..., prehaps we could just skip the triometry funcions and do it like this
. I altered the parameterlist a bit so watch out! space is replaced by numSteps.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 24th Jan 2009 02:05
Quote: "sin(x) + cos (x) always equals 1.
"


I thought that was sin^2 + cos^2 always equals 1.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 24th Jan 2009 02:43 Edited at: 24th Jan 2009 02:50
@Quadrazar
I see, your alter version is much faster but less accurate positioning because of the integer variable and the early calculations for the x,y.

I had the incXY variables be floats and drew the text first befor adding the inc and it works perfect and running at more than double the speed of my version.

I didn't like using floats knowing they are slower than integers but they can be faster than a long integer equation. Seems each operator will slow ya down a bit eh.
Quadrazar
17
Years of Service
User Offline
Joined: 7th Jan 2009
Location: onboard the Kobayashi Maru
Posted: 24th Jan 2009 03:28
@BN2:
Quote: "sin(x)^2 + cos (x)^2 always equals 1."

oops. My mistake.

@Ashingda:
Quote: " ... less accurate positioning because of the integer variable and the early calculations for the x,y."

Nice to know. Now I get why you placed them inside the loop.
Quote: "they can be faster than a long integer equation"

Very nice to know, thanks for the test and the info.

I've been trying to position a player. It worked. I've placed two players on the board, they go around in turns. after three turns player 2 decides to kick player 1 out of the game and starts to go around the board solo. lol.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 24th Jan 2009 04:16 Edited at: 24th Jan 2009 04:48
I've gotta sneak in! I've come up with a general function that eliminates the need to call the function 4 times - which should be a speed boost. It only loops through half of the number of rectangles and draws top and bottom at once, and sides at once which should also be a speed boost. Should be a bit faster - but haven't benched marked it:



There's a couple of things not being account for in this one. I'll have to make some improvements

I know there's a way to cut the iterations in half again... I'm just not seeing it yet.

[EDIT]
And here it is...



Enjoy your day.
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 24th Jan 2009 05:39
Wow latch according to my speed test your's is the fastest!


Ohh yeah what I tested befor was only the calulation speed without the text command.

Login to post a reply

Server time is: 2026-07-05 01:47:51
Your offset time is: 2026-07-05 01:47:51