Domanda

I've been trying to do something that seems surprisingly challenging --- printing an equilateral triangle to the command line (Terminal for Mac OS X). I have a program that can compute the nth row of Pascal's triangle up to some user-specified constant. As is well known, if one takes the values of Pascal's triangle modulo two, there is a correlation between that and Sierpinski's triangle.

I have been setting odd values to be 1 and even values to be 0, and when I print the results on the Terminal and zoom out, it looks nice, apart from the fact that it's clearly not equilateral. Here is an example output of my program after zooming way out (so zeroes and ones look much different):

enter image description here

But I'm wondering ... is there a way to get this triangle to look equilateral? Or do I have to print the output somewhere else? I've been experimenting with different fonts, different line width levels, but I can't get anything to look close to equilateral, and even if it does, I don't have a reliable way of checking for this. Part of the problem is also that zooming in/out on the Terminal results in different line width and height scales.

My code takes in as input the number of rows to generate. Then, it takes that number into account when printing out each row. So the first row (which is just a single "1") would have n-1 spaces to print before printing the 1. Then the second row has to print n-2 spaces before printing its actual contents (which are "1 1"), which includes a space between each number, and so on. It's in C++, but I don't think that should matter.

I suspect that I'll need to find some other way of getting the image out, so any advice about libraries to use would be great.

Nessuna soluzione corretta

Altri suggerimenti

A good option is to render the triangle to an raster format of your choice, and use aalib or libcaca to render that image to the terminal.

I would try to (and I think you already have) figure out the actual width and height of what the image would ultimately be, and generate the 2D matrix defining that images dimensions. This matrix can be a 2D set of integers (no less than 24 bits wide giving space for 8 bit color components), or 3 separate 2D matrices, one for each color component. Set all of those values to whatever you want the background color to be.

Move through your algorithm setting the appropriate pixels to whatever OTHER color your want your actual triangle to show up as.

Look here for writing such a matrix out to a .bmp (or bitmap) file.

Writing BMP image in pure c/c++ without other libraries

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top