Archive for the ‘C++’ Category


January 19, 2010 Leave a comment

So I’m hooked on SPOJ again. For this burst of enthusiasm I’m using C++, both to increase my skills and to clean up some of the problems that my Python solutions could not solve in the given time limit.

Of course this process does not produce many (any) pretty pictures, therefore not much content for this fledgling blog.

Still, maybe I’ll discover some sort of algorithm or something.

I was rather proud of myself for figuring out the relationship between the curved perimeter of a semi-circle and its area (yes, pretty advanced stuff, I know :-P). Where “p” is the curved part of the semi-circle’s perimeter, and “A” is the area:

A = \dfrac{p^2}{2\pi}

I don’t want to give away exactly which problem this was for, but it would sure be useful if you needed to enclose the maximal space possible against an infinitely long wall by adding a fence of a given length and arbitrary shape which touches the wall at exactly two points.. *cough* 😉

Categories: C++, Plans, Python Tags: , , ,

It’s Compilin’ Time!

January 10, 2010 Leave a comment

Further to my last post, I actually have coded a very simply Mandelbrot set renderer in C++.

I’m quite familiar with the basic syntax of C++, so at the outset I though the main challenges would be:

  1. dealing with complex numbers
  2. saving the results as an image file

After some mild Googling both of these challenges were surmountable.

C++ has a standard library header “complex,” which implements complex as a template class. Whatever that means. Anyway, import “complex” and you now have complex number support. See the code below for a demonstration.

For PNG support I used PNGwriter (also available in the Ubuntu repos). PNGwriter’s project site has plenty of example code, and the usage seems very straight forward. The only stumbling block (for me at least) was getting the compiling options right to “link” the PNGwriter library. As it turns out, some or all of the following command is required to compile this code:

g++ main.cpp -o my_program `freetype-config –cflags` -I/usr/local/include -L/usr/local/lib -lpng -lpngwriter -lfreetype

Aside from that bit of unpleasantness, it was really no harder to write this in C++ than in Python. Here it the finished code:

#include <iostream>
#include <complex>
#include <pngwriter.h>

using namespace std;

int main()
    int bailout = 256;
    int size = 800;
    pngwriter png1(size,size,0,"mandel2.png");

    for(int x=0;x<size;x++)
        for(int y=0;y<size;y++)
            int i = 0; //iterations
            complex<double> c(x/(size/4.)-2, y/(size/4.)-2);
            complex<double> z(0, 0);

            while(abs(z)<2 and i<bailout)
                // iterate through mandelbrot dynamic
                z = pow(z, 2) + c;

                // c is outside the mandelbrot set
                float value = (float) i / bailout;
                png1.plot(x, y, value, value, value);

        } // for y
    } //for x


    cout << "done!" << endl;

    return 0;

Remember, if you want to try running this code you will need the PNGwriter library.

The output is of course no different from the Python version that I posted previously, but here it is for the sake of completeness:

The Mandelbrot Set

The Mandelbrot Set

See Plus Plus

January 7, 2010 Leave a comment

These Buddhabrot scripts can sure be processor intensive. I would be better off programming them in C or C++, much as I do love Python. They are rather trivial, so it shouldn’t be too difficult.

One purpose of this blog is to openly record my musings of the various things that I think I ought to do—in the hope that I may remember to actually do them. This is but one such open musing.