Jump to content

  • Log In with Google      Sign In   
  • Create Account

fastcall22

Member Since 04 Feb 2008
Online Last Active Today, 06:18 PM

Posts I've Made

In Topic: Spreading value x randomly between x amount of elements

27 June 2016 - 02:23 PM

Here’s my approach: http://ideone.com/PBEL1x

While determining the portion, the fractional component is carried over to the next bin.
 
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <array>
#include <iterator>


int main(int, char*[]){
	using namespace std;
	srand(time(0));

	// setup bins
	int total = 5000;
	const int MAX_BINS = 25;
	array<int,MAX_BINS> bins;

	// determine weighted distribution
	array<float,MAX_BINS> weights;
	generate(weights.begin(), weights.end(), [](){
		return rand()*1.f/RAND_MAX;
	});
	float weight_sum = accumulate(weights.begin(), weights.end(), 0.f);

	// determine portions
	// use "extra" as an accumulator to determine when to add an extra 1 to the bin
	float extra = 0;
	int used = 0;
	for ( int idx = 0; idx < MAX_BINS; ++idx ) {
		float actual = weights[idx]/weight_sum * total + extra;
		int count = (int)actual;

		extra = fmodf(actual, 1.f);
		bins[idx] = count;
		used += count;
	}

	// any accumulation error is added to the first bin
	int error = error;
	bins[0] += total - used;

	// output results
	cout << "total:\t" << total
		<< "\nbins:\t";

	copy(bins.begin(), bins.end(), ostream_iterator<int>(cout, "\t"));
	cout << "\nerror:" << error
		<< "\nextra:" << total - accumulate(bins.begin(),bins.end(),0) << '\n';
}
Output:
total:	5000
bins:	119	24	253	186	176	121	283	85	332	51	350	34	304	7	50	371	338	52	333	363	280	372	92	140	284	
error:	0
extra:	0
And is flexible where there isn’t much to distribute:
total:	3
bins:	0	0	0	1	0	1	0	0	0	1	
error:	0
extra:	0
EDIT:
Actually, I just realized that this isn’t the best approach for small numbers over numerous bins, as the accumulator will favor bins toward the end.

In Topic: Do you usually prefix your classes with the letter 'C' or something e...

07 June 2016 - 12:08 AM

lowercase_underscores, because I find the extra horizontal space easier to read than CompactPascalCase or compactCamelCase. And makes acronyms, however rare, easier to read: TWAIN_adapter instead of TWAINAdapter (TWAINA dapter?) or TWAINadapter (TWAI Nadapter?)...

I could do Pascal_Case_Underscores, but that’s a little 2crazy4me...

In Topic: Can't Get Bullets to Shoot

04 June 2016 - 07:02 PM

if (event.key.keysym.sym == SDLK_SPACE)
{
    gBullet.isAlive = true;
    gBullet.Y_Vel = 0.05f;                  // (2)
    gBullet.Y -= gBullet.Y_Vel * DeltaTime; // (1)
    gBullet.Rect.y = (int)gBullet.Y;
}


This code only moves the bullet (1) a single step, but only after pressing the space bar. Additionally, if these units are in pixels, then the bullet will take 20 seconds to move a single pixel (2). Obviously, the bullet should move regardless of whether of not the space bar was pressed, so it should be updated independently:
 
// Update time
ThisTime = SDL_GetTicks();
DeltaTime = (float)(ThisTime - LastTime) / MAX_TICKS;
LastTime = ThisTime;

// Process events
SDL_Event ev;
while ( SDL_GetEvent(&ev) )
{
    // ...

    if (event.key.keysym.sym == SDLK_SPACE)
    {
        gBullet.isAlive = true;
        gBullet.Y_Vel = 300.0f;  // pixels per second
    }
}

// Update entities
if ( gBullet.isAlive )
{
    gBullet.Y -= gBullet.Y_Vel * DeltaTime;  
    gBullet.Rect.y = (int)gBullet.Y;
}    

In Topic: c++: sprintf but return chars. Possible?

01 June 2016 - 10:42 AM

Use StringBuilder:

class string_builder {
public:
	template<typename T>
	string_builder& operator << (const T& val) {
		s << val;
		return *this;
	}

	operator const std::string () const {
		return s.str();
	}

private:
	std::ostringstream s;
};


int main(int argc, char* argv[]) {
	try {
		// contrived example
		if ( argc <= 1 ) {
			throw std::runtime_error(
				string_builder() << "Not enough arguments, got " << argc
			);
		}
	}
	catch ( const std::exception& ex ) {
		std::cerr << "Exception caught: " << ex.what() << std::endl;
	}
}
Wait, this isn’t Java?

In Topic: My Texture to HBITMAP function leaks and I don't know why...

22 May 2016 - 05:33 PM

// assuming std::vector<uint8_t> CDirectXWrapper::image_buffer;

image_buffer.resize(textureDesc.Width * textureDesc.Height * 4); // may or may not trigger realloc
uint8_t* destination = image_buffer.data();

PARTNERS