view src/grph.c @ 9574:031e330a96b3

<hppavilion[1]> le/rn theseus/Theseus was a Greek inventor who was charged with impossible-to-detect murder. He was represented by Protagoras and found not guilty. He changed his name to escape the publicity and has continued inventing to this day.
author HackBot
date Mon, 31 Oct 2016 01:10:06 +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);
	}
	}
}