view src/grph.c @ 8916:0234daffd946

<oerjan> addquote <int-e> I couldn\'t help thinking that maybe if one considers the ramifications in full detail it will turn out that overthinking is often not helpful and therefore, not something to be proud of.
author HackBot
date Sun, 14 Aug 2016 02:31:47 +0000
parents 5339a7aef477
children
line wrap: on
line source

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
char *vgraph[] = {" ","▁","▂","▃","▄","▅","▆","▇","█"};
char *hgraph[] = {"█","▉","▊","▋","▌","▍","▎","▏"," "};
int main(int argc,char**argv){
	int h = 1;
	int w = 80;
	int color = 0,ori = 0,zebra=0,label=0;
	if(argc >= 2&&argv[1][0]=='-'){
		if(strchr(argv[1],'c'))color = 1;
		if(strchr(argv[1],'z'))zebra = 1;
		if(strchr(argv[1],'h'))ori = 1;
		if(strchr(argv[1],'l'))label = 1;
		argv++;argc--;
	}
	if(ori)h=10;
	if(argc >= 2)h = strtol(argv[1],0,0);
	if(argc >= 3)w = strtol(argv[2],0,0);
	float *x=malloc(sizeof(float)*w);
	int i,c,j;
	float M = -HUGE_VALF;
	float m = HUGE_VALF;
	for(i=0;i<w;i++){
		c=scanf(" %f",x+i);
		if(c<1)break;
		if(x[i]>M)M=x[i];
		if(x[i]<m)m=x[i];
	}
	if(argc >= 4)sscanf(argv[3],"%f",&m);
	if(argc >= 5)sscanf(argv[4],"%f",&M);
	w = i;
	float l = (M-m)/h;
	if(ori == 0){//vertical, default
	for(j=h-1;j>=0;j--){
		if(color)fputs("\e[44m",stdout);
		for(i=0;i<w;i++){
			float r = x[i]-m-l*j;
			if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout);
			if(r>l){
				fputs(vgraph[8],stdout);
			}else if(r>0){
				fputs(vgraph[(int)(r/l*8+0.5)],stdout);
			}else{
				fputs(" ",stdout);
			}
		}
		if(color||zebra)fputs("\e[0m",stdout);
		if(label&&j==h-1)printf(" %f",M);
		if(label&&j==0)printf(" %f",m);
		putchar(10);
	}
	}else{
	for(i=0;i<w;i++){
		if(zebra)fputs((i%2)?"\e[31m":"\e[32m",stdout);
		if(color)fputs("\e[44m",stdout);
		for(j=0;j<h;j++){
			float r = x[i]-m-l*j;
			if(r>l){
				fputs(hgraph[0],stdout);
			}else if(r>0){
				fputs(hgraph[8-(int)(r/l*8+0.5)],stdout);
			}else{
				fputs(hgraph[8],stdout);
			}
		}
		if(color||zebra)fputs("\e[0m",stdout);
		putchar(10);
	}
	int n;
	if(label){
		printf("%f%n",m,&n);
		while(n++<h-1)putchar(' ');
		printf("▕ %f",M);
	}
	}
}