loading
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define P 45
#define M 160 
#define N 90
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

struct point {
  double x, y;
};

struct point vals[P];
char         screen[N][M];

double function(double x)
{
  return (fabs(x)+x-2);
}

int nearest(double x)
{
  return ((int) (x+0.5));
}

void clear_screen()            
{
	int i,j;
	for(i=0;i<N;i++)
		for(j=0;j<M;j++)
			screen[i][j]=' ';
}

void plot_screen()
{
	int i,j;
	for(i=N-1;i>=0;i--)
	{
		for(j=0;j<M;j++)
			printf("%c",screen[i][j]);
		printf("\n");
	}
}

void calculate_values(double l, double r)
{
	int i;
	for(i=0;i<P;i++)
	{
		vals[i].x=l+i*(r-l)/(P-1);
		vals[i].y=function(l+i*(r-l)/(P-1));
	}
}

void horizontal(int row)
{
	int j;
	for(j=0;j<M;j++)
		screen[row][j]='-';
}

void vertical(int col)
{
	int i;
	for(i=0;i<N;i++)
		screen[i][col]='|';
}

void map_to_screen(double l, double r, double d, double u)
{
	int i,j,i1;
	double x,y;
	if(l<=0&&0<=r)
		horizontal(-d*(N-1)/(u-d));
    if(d<=0&&0<=u)
		vertical(-l*(M-1)/(r-1));
	for(i1=0;i1<N;i1++)
	{
		x=vals[i1].x;
		y=vals[i1].y;
		i=((y-d)*(N-1))/(u-d);
		j=((x-l)*(M-1))/(r-l);
		if(nearest(i)>=0&nearest(i)<N-1&&nearest(j)>=0&&nearest(j)<M-1)
			screen[i][j]='.';
	}
}

int main()
{
  clear_screen();
  calculate_values(-5, 5.0);
  map_to_screen(-5.0, 5.0, -5.0, 10.0);
  plot_screen();
  system("PAUSE");
  return 0;
}