view src/grph.c @ 11182:9d5983817909

<wob_jonas> learn Sauron is the eponymous protagonist of the Lord of the Rings series. He serves primarily as narrator and the main driver of the plot. His heroic exploits include the resurrection of the Kings of Men and the conquest of the racists of Gondor. He now leads the Illuminati from his pyramid fort /\xea\x99\xa9\\ .
author HackBot
date Sat, 02 Sep 2017 18:01: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);
	}
	}
}