Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Feb 2011
Online Last Active Today, 09:58 PM

#5177319 Anttweakbar direction widget

Posted by Bacterius on Yesterday, 07:02 PM

Have you tried the axisx/axisy/axisz variable parameters? In the documentation (here at the bottom) it says:

axisx / axisy / axisz

For direction and quaternion variables only.

Change the default coordinate system used to represent quaternion and direction. By default the coordinate system is right-handed with axis x pointing to the right, axis y pointing to the top and axis z pointing to the front (ie., the viewer). If you are using a different coordinate system in your application, use these parameters to permute the axes. n represents the new axis; it is one of the following values: x, -x, y, -y, z or -z.


TwDefine(" mybar/Rotation axisx=x axisy=y axisz=-z ");  // Permute z axis to get a left-handed coordinate system

#5176177 Mapping Poker-Starting Hands to 0..1325

Posted by Bacterius on 26 August 2014 - 05:46 AM

There are 52 cards, so you need i * 52 + j, and the requirement that i < j is unnecessary. So:

> Hand {i, j} maps to index i * 52 + j


> index h maps to hand {h / 52, h % 52}

Where / is integer division.

EDIT: ninja'd, that said with integer division (which rounds down) you don't need to subtract j to get a multiple of 52, that's one of the nice features of integer division.

#5174904 Strange output when writing to a file object and printing it out

Posted by Bacterius on 19 August 2014 - 08:05 PM

the problem from the windows ! bug in windows

It's not a bug in Windows, it's a bug in your code, admittedly an obscure one that Python programmers do not necessarily know to expect. This link explains what is going on, and you could've found it, I only had to type "python file read garbage" in google and it was the first hit wink.png

I would personally recommend staying away from the "+" mode suffixes because of these peculiarities and just seek wherever you need to write to, or, better yet (but not applicable to your case since you are learning how to work with files in Python) use something like pickle or JSON to automatically write your objects directly to disk and read them back painlessly in only a few lines of code.

#5173676 Pi decimals as a random number generator

Posted by Bacterius on 14 August 2014 - 01:48 PM

Given that you can actually find every sequence of bytes in PI no matter how long, which I doubt, (you can't find PI inside PI, for instance, since it is a transcendental number, and if you could, you would actually have an irrational number, which is algebraic numbers) I wonder the amount of metadata you would have to write down to "store your file in PI".


Yes, that is the joke, the metadata must be at least as large as the data itself, as expected from a simple pigeonhole argument (but some people still believe you can get something for nothing, which makes for some hilarious bug reports/issues) smile.png

#5173505 Pi decimals as a random number generator

Posted by Bacterius on 13 August 2014 - 11:25 PM

You could, but it's not particularly efficient. If I want to generate a thousand random numbers, I have to expand the Taylor series a painfully long way, which gets into numerical precision problems and other nastiness. There exist much more effective ways to get pseudo-random numbers.


The digits of pi can be generated more efficiently (and accurately) than by expanding one of its Taylor series. But, yes, it is not a particularly good idea, because it is slow (it gets slower and slower as you go) and the digits of pi are not known to be uniformly distributed anyway. Do not use it as a pseudorandom number generator, it has zero advantages and many disadvantages compared to a multiply-and-carry generator or a Mersenne twister, or a counter-mode pseudorandom permutation.

#5173223 Calculate number sequence

Posted by Bacterius on 12 August 2014 - 06:04 PM

In code, you can do this using the bitshift operator as well instead of using pow or something, courtesy of binary:

n = 5 << (level - 1);

Watch out for overflow though. Of course, if later on you want to have partial levels, e.g. level = 2.5 somewhere between level 2 and level 3, then just using pow is the better solution, since you're using floats anyway.

#5170220 Web Programming Proffession [ Need Advice] -

Posted by Bacterius on 29 July 2014 - 08:41 PM

PS: Reputation doesn't make you right.


I think he meant check his reputation log before accusing him of downvoting you. Also, chill out. I'm sure it's possible to have a civil discussion about PHP and web programming without resorting to ad hominems and accusations (right?)

#5169635 Weird problem reading file into buffer

Posted by Bacterius on 27 July 2014 - 06:26 PM


EDIT: my bad, my previous answer was incorrect. New one:


I don't know what the %#x format specifier does, but try '%.2x' instead. That should work. This is clearly a sign issue when your byte values are negative and then interpreted as unsigned integers...


I tried changing it to %.2x but it still displays the same. Also that wouldn't help me later in my code I don't think when I'm doing assignments like gif->Packed = buffer[10];


Btw my typdef for BYTE is char, not sure if that matters... I do that since char is a byte long and all I need is a byte for each variable in my gif struct.



Oh, ok, so in fact my previous (edited out) answer was correct (I thought you were using the Win32 BYTE type, which is defined as unsigned char). The char type is signed, and the %x format specifier expects an unsigned integer, so it is interpreting your (signed) char as an unsigned integer which makes it wrap around to some large positive value which is then displayed in hexadecimal.


Replace your typedef with unsigned char. To store binary data, use unsigned char. It is also a byte, but is unsigned, and so goes from 0 to 255 as you rightly expect. In these kinds of low-level binary manipulations, signed integer types should be used sparingly, if ever, because they are evil, and you will encounter only problems working with them in this context most of the time (like, say, now).

#5169280 Paint method not called

Posted by Bacterius on 26 July 2014 - 06:08 AM

What do you mean you "couldn't get the paint method to be called"? What is exactly the "paint" method? I don't see any method called "paint" in your code.


Uh, the paintComponent method has a big fat println that says "Paint Method Called". I agree about posting more information, but in this case it seems pretty clear what the paint method is.


Morrowa, the issue is (seems to be, I haven't tested) that you are creating a basic JFrame, which doesn't include your paint method (which belongs to Main, the class inheriting from JFrame you are in). In other words, you are never instantiating your own implementation of JFrame, and so it is not being used. Try this:

JFrame window = new Main();

Or something to that effect. Also, sleeping is probably not the most effective way of keeping a GUI running, since I believe it pauses the main (GUI) thread. Just leave the window open and add some code that quits the program when you click on it or press a key or whatever, that way you can test it more easily without worrying about blocking.

#5168611 C++ files organisation

Posted by Bacterius on 23 July 2014 - 04:50 AM

They are not mutually exclusive. You can have a main src or include folder which then branches off into a deep (but not too deep) file tree, with your modules neatly organized in separate files and folders. This is what I tend to do myself. In any case, if you are not designing a library or other code that could be reused by people other than you, I would just use whatever works best for you, such organizational concerns are not usually a major problem except to grumpy packagers used to doing things "their way" smile.png . Probably many of the large open source projects that you have seen have bureaucratic or architectural requirements (by virtue of being very large, or having lots of users and contributors) that would be very inappropriate in a smaller project, so many of the things you see in them would seem very strange from your perspective (though src/include is not really among them, but just saying). I don't think there is a widely accepted standard in C++ anyway, as long as your build system does not grow uncontrollably in complexity underneath you, you should not worry about it too much. C++ doesn't have a universal style guide that almost everybody follows like Java or C# do, far from that.


Some styles I've seen are "headers in include, source code in src", "only public headers in include, private headers and source code in src", "everything in src", "code dump with no folders at all (perhaps with e.g. a visual studio solution which already encodes the folder structure)", and so on... to be fair I do mostly C and not much C++, and I am personally not too comfortable with the idea of putting actual implementation code inside an "include" folder like a lot of the C++ projects seem to be doing with the advent of header-only libraries and templates (yes, I know it's not strictly required if you forward declare the different templated types you'll be using, but few bother to do that). But it's really no big deal - we are not machines, and can adapt when things don't go 100% as we expect. Really, it just goes to show that there is really no consensus on the right way to do it.


In any case, I can give a few insights on what I expect from a freshly checked out code repository:


* as a user (for libraries and other)


- is there an obvious build/install script (e.g. a solution file for visual studio for windows, a makefile or cmake/scons/autohell script for linux, a codeblocks project, etc..)?

- if not, is there a readme or install.txt I can look at?

- no? well, I don't know how to use it, if it's small enough and license permitting I might copy the source and headers inside my own code.. provided I can find them, e.g. an include or src folder

- if not, I give up and check out another library


* as a developer (contributing/etc)


- if the build system is a bit complex or there are things I should know or configuration options, are there notes about that somewhere? (not needed for small programs or obvious instructions e.g. a plain makefile)

- is it easy to build the software after changing code? does it make sure to always rebuild what needs to be (and, preferably, only what needs to be)?

- does it build out of source, or at least doesn't spew .o/.obj files everywhere in the source folder?

- are there tests I can run after making nontrivial changes?


As long as your project package provides these things, I don't see any problem. I've certainly seen far worse and I'm sure others have too.

#5168088 Hash Distance & Angle To Produce Unique Value

Posted by Bacterius on 21 July 2014 - 12:47 AM

What about multiplying the distance and the angle, suitably quantized, and handling angle wraparound properly? Should work, I think, and it takes into account the fact that spacing between angles gets larger as distance increases (if you don't want that, take the square root of the distance instead). Otherwise, you could just convert the distance + angle to an actual 2D point and go from there, it might be easier.

#5167794 Need help with a fuzzy Scoring algorithm.

Posted by Bacterius on 19 July 2014 - 08:05 AM

This is simply (reverse) linear interpolation. It tells you the relative "standing" of the input value relative to a minimum and a maximum (how far it is from them) taking into account the distance between the minimum and maximum, that is, the range (if the range is smaller, the input value will be more heavily penalized for being far away). An output value of 50% means the input value is "half a range" to the right of the minimum, i.e. halfway between the minimum of the maximum. An output value of -300% means the input value is "three ranges" to the left of minimum, an output value of 250% means the input value is "two and a half ranges" to the right of minimum, etc... so that only input values in [0, 100] fall in the [min, max] interval.

                       MIN                                      MAX               
                        +                                        +                
                        |                                        |                
                        |                                        |                
                        |                                        |                
       |                |                   |                    |           |    
       |                |                   |                    |           |    
       +                +                   +                    +           +    
     -40%               0%                 50%                 100%        125%   

#5167506 Functions

Posted by Bacterius on 17 July 2014 - 07:53 PM

I haven't seen it explicitly mentioned here but thought it might be useful to the OP as it may not be necessarily obvious to newbies - the important thing you must be aware of is that you don't need to know how a function is implemented in order to be able to call it. This is possibly the first introductory example of abstraction through separation of the interface (prototype, what it is/does) and implementation (definition, how it does it).

#5167339 Funniest line of code ever ?

Posted by Bacterius on 17 July 2014 - 03:31 AM


What is wrong with this preprocessor line? It just means to do stuff if this is gcc and we're on an x86 system... I'm not getting the joke.


It's basically saying "if your computer is really, really, really old.... or really new"


If you have a 486, pentium, or any modern 32 bit processor, you are out of luck.




Not really. I don't know what software the codebase is from, but x86 isn't the only processor architecture. For instance, if you wanted to run some code on a raspberry pi, or an arduino, this preprocessor condition would evaluate to zero. And those are hardly mythical machines like the PDP. So, some context would have probably been appropriate in this case, e.g. "windows game" smile.png 

#5167332 Funniest line of code ever ?

Posted by Bacterius on 17 July 2014 - 03:10 AM

What is wrong with this preprocessor line? It just means to do stuff if this is gcc and we're on an x86 system... I'm not getting the joke.