May 2, 2025

Buckets (C++)

A simple allocation of primes along the number line

DownloadOpen

Buckets is a short piece of code designed to generate a heat map of primes across the number line. The screen is segmented into bins of 10x10 pixels and primes are allocated according to that.

This iteration could be improved with a more sophisticated allocation of the primes. In fact this is most likely as unsophisticated as one could be but it is a good starting point for understanding.

The code returns something like this with prime frequency concetrated around the smaller integers.

#include <SDL2/SDL.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include "primes.h"
#include "drawing.h"

// Function to count primes in each bin
std::vector<std::vector<int>> countPrimesInBins(const std::vector<int>& primes, int width, int height, int binSize) {
    std::vector<std::vector<int>> bins(width / binSize, std::vector<int>(height / binSize, 0));
    for (int prime : primes) {
        int x = prime % width;
        int y = prime / width;
        bins[x / binSize][y / binSize]++;
    }
    return bins;
}

// Function to map prime count to a color
SDL_Color getBinColor(int primeCount, int maxCount) {
    int intensity = static_cast<int>(255.0 * primeCount / maxCount);
    return SDL_Color{255, static_cast<Uint8>(intensity), static_cast<Uint8>(intensity), 255}; // Color ranges from red to white
}

int main() {
    const int WINDOW_WIDTH = 800;
    const int WINDOW_HEIGHT = 600;
    const int BIN_SIZE = 10; 
    int numberOfPrimes;

    std::cout << "Enter the number of prime numbers to generate: ";
    std::cin >> numberOfPrimes;

    std::vector<int> primes = generatePrimes(numberOfPrimes);
    std::vector<std::vector<int>> bins = countPrimesInBins(primes, WINDOW_WIDTH, WINDOW_HEIGHT, BIN_SIZE);

    // Find the maximum count of primes in any bin
    int maxCount = 0;
    for (const auto& row : bins) {
        maxCount = std::max(maxCount, *std::max_element(row.begin(), row.end()));
    }

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Prime Heatmap", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
    if (window == NULL) {
        std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    // Main loop
    bool running = true;
    SDL_Event event;
    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
        }

        // Clear the renderer
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set draw color
        SDL_RenderClear(renderer);

        // Drawing the heatmap
        for (int x = 0; x < WINDOW_WIDTH; x += BIN_SIZE) {
            for (int y = 0; y < WINDOW_HEIGHT; y += BIN_SIZE) {
                int binCount = bins[x / BIN_SIZE][y / BIN_SIZE];
                SDL_Color color = getBinColor(binCount, maxCount);
                SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
                SDL_Rect binRect = {x, y, BIN_SIZE, BIN_SIZE};
                SDL_RenderFillRect(renderer, &binRect);
            }
        }

        // Update the renderer
        SDL_RenderPresent(renderer);
    }

    // Cleanup
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

General notes on compiling C++

#To compile
g++ g++ -o [buckets] [buckets.cpp] 

#To run
./buckets 

Often you will get errors when compiling because C++ may not have the correct dependencies or the compiler might need things not on your machine. The simplest approach is just to copy the error into your favourite LLM. They are very good at disagnosing problems with C++.