Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

SiCrane

Member Since 09 Dec 1999
Offline Last Active Yesterday, 07:46 PM
*****

#4828866 c++ circular iteration

Posted by SiCrane on 28 June 2011 - 05:29 PM

One useful trick for these kinds of situations is putting the special case as the first iteration. Ex:
  for (int i = v.size() - 1, j = 0; j < v.size(); i = j, ++j) {
    foo(v[i], v[j]);
  }



#4828673 C++: When to inline?

Posted by SiCrane on 28 June 2011 - 08:22 AM

My general rule of thumb is to never make performance decisions without the aid of a profiler (aside from no brainers like avoiding unnecessary copies). And profilers are generally pretty good about letting you know which functions should have been inlined but weren't and really bad about letting you know about which functions that shouldn't have been inlined but were. Therefore I tend to write code for maximum clarity rather than performance and leave decisions like whether or not code should be inlined until when explicit performance tuning is done.


#4828396 Cross product of two vectors not perpendicular!

Posted by SiCrane on 27 June 2011 - 02:40 PM

No, c is perpendicular to a and b. Check the dot products. Did you want (a - origin) x (b - origin) instead?


#4828295 I need advice on breaking a bad habit; 'if' & while reliance'...

Posted by SiCrane on 27 June 2011 - 09:57 AM

all the mainstream languages that offer operators like &&, || do short-circuit evaluation on them.

Assuming that they aren't overloaded anyways. In C++ if you overload && or || then short circuit evaluation no longer applies. Some languages don't allow operator overloading on logical and and logical or for this reason.


#4827899 timeBeginPeriod not working

Posted by SiCrane on 26 June 2011 - 08:18 AM

timeBeginPeriod() affects the system global time period. Windows will use the highest resolution (lowest value) requested by any process on the system. This basically means that you can reliably increase resolution, but not reliably decrease it.


#4827858 Where is my Window?

Posted by SiCrane on 26 June 2011 - 06:15 AM

Some comments. 1) You should really check the return value of all the functions you call. For Windows functions if there's an error, generally GetLastError() will tell you what went wrong. 2) Calling ZeroMemory() on a structure after setting member variables is counter productive. 3) Calling GetMessage() with an hWnd parameter will not catch WM_QUIT messages created by PostQuitMessage().


#4826765 Linked List/Entity Spawning

Posted by SiCrane on 23 June 2011 - 07:23 AM

The waste of time reinventing the wheel every time you need a linked list aside, that's something you're only allowed to say if you can write one correctly. Not only did you have to write four times the amount of code that using std::list would have taken, your code contains at least one major bug, requires more effort to use than std::list, and only allows one linked list in the entire program.

Edit: reponse to added text:

You can't remove from STL data structures while in a loop either

Bullshit. And the problem isn't removing in a loop, the problem is that your node removal code is broken.


#4826648 C++/OpenGL/Key Input

Posted by SiCrane on 22 June 2011 - 06:19 PM

Since you're using GLUT you might as well use the GLUT callbacks for input. For keyboard you probably first want to consider glutKeyboardFunc().


#4826633 C++/OpenGL/Key Input

Posted by SiCrane on 22 June 2011 - 05:34 PM

I'm not really sure those all constitute options. ncurses is designed for use with console windows, which OpenGL programs generally don't use and sendkeys is for sending input to a different window, not for receiving input. It would help if you mentioned which libraries you using/how you are creating your OpenGL window.


#4826553 Pointer to non-static member function?

Posted by SiCrane on 22 June 2011 - 01:35 PM

Then a better approach then rather depend on raw function pointers, use function objects. Ex: boost::function in conjunction with boost::bind.


#4826428 cos angle of tangent to circle

Posted by SiCrane on 22 June 2011 - 08:04 AM

You don't need any inverse functions to answer the questions. You have the right triangle and two of the sides so you can calculate the remaining side and the cosine, sine or tangent of the angles without needing to find the exact value of the angles.


#4826405 cos angle of tangent to circle

Posted by SiCrane on 22 June 2011 - 06:54 AM

Draw a line from the center of the circle to the tangent point. This line also has a length of g and since it's a tangent point then this forms a right triangle with the hypotenuse of length r. Adjacent to the angle alpha is one of the angles of the triangle. What is the cosine of that angle? Since alpha is 180 minus that angle, how does cosine of alpha relate to cosine of that angle?


#4826098 Build targets

Posted by SiCrane on 21 June 2011 - 12:51 PM

Go to the preprocessor definitions for your project under the project settings and add the identifier you want to the list of preprocessor definitions.


#4826044 Why must a variable be initialized ?

Posted by SiCrane on 21 June 2011 - 10:31 AM

The C++ standard has a wonderfully confusing set of definitions that often are at ninety degrees with common sense. Section 8.5 of the current version contains all the details, but when POD types are "default initialized" that initialization is as if they had been initialized with 0's. On the other hand, "default initialization" only happens in a limited set of occasions, so "default initialization" doesn't happen by default most of the time. Most of the time when a POD variable lacks an initializer it is uninitialized rather than "default initialized" and thus has an indeterminate value.

IIRC, this particular confusion was the result of templates being added to the language and the desire for the expression T() to have a reliable meaning for when T is a POD type. Before that, C++ avoided the confusing term of default initialization and only had zero initialization and uninitialized values. I don't care enough to fact check this paragraph, however.


#4822634 python idle is being incredibly unhelpful

Posted by SiCrane on 13 June 2011 - 12:33 AM

So is the code you posted by itself in it's own .py file? If so, then it looks like you're missing an import for the random module.




PARTNERS