-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphcalc.c
170 lines (136 loc) · 3.55 KB
/
graphcalc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h> //for rand()
#include <unistd.h> //for 'usleep function'
#include "gfx2.h"
#include <math.h>
#include <string.h>
//1. need to figure out how to compute Taylor series approximation
//2. need to figure out how to draw axes
long double computeY(int i, long double x); //function to compute y-coordinates given x-coordinates
void plot(int i); //function to plot x,y coordinates
void drawAxes(void); //function to draw axes
char *num2str(int n); //converts numbers into strings
long double factorial(int i);
int main() {
int y;
char c;
int terms = 1, x; //terms = # of terms
gfx_open(600, 600, "Emily Graphing Calculator");
printf("Press + key to increase the number of terms, press - key to decrease the number of terms\n");
while (1) {
gfx_color(255, 255, 255); //set color of curve
plot(terms); //default number of terms is 1
drawAxes();
if (gfx_event_waiting()) {
c = gfx_wait();
if (c == '=') {
gfx_clear(); //clear screen before drawing new function
terms++;
} else if (c == '-') {
gfx_clear(); //clear screen before drawing new function
terms--;
if (terms <=0) {
printf("The number of terms cannot be lower than 0."); //ensure number of terms is always greater than or equal to 0
terms=1;
}
} else if (c == 'q') {
break;
}
}
printf("The number of terms is: %d\n", terms);
}
return 0;
}
long double computeY(int terms, long double x) { //function to compute the y-value for given x of Taylor Series approximation
long double value, sum = 0, num, dem, number; //i = number of terms
int i;
for (i=1; i<=terms; i++) {
if (i%2 == 0) {
num = pow(x, (2*i-1));
dem = factorial(2*i-1);
number = -(num/dem);
} else {
num = pow(x, (2*i-1));
dem = factorial(2*i-1);
number = (num/dem);
} sum = sum + number;
}
return sum;
}
long double factorial(int i) { //calculates factorial
int number;
long double total = 1;
for (number = 1; number <= i; number++) {
total = total*number;
}
return total;
}
void plot(int terms) {
long double x, y;
int pixx, pixy;
for (pixx=1; pixx<=600; pixx++) { //plot the x- and y-coordinates from the range x(1-600)
x = ((long double)pixx-301)/30;
y = computeY(terms, x);
pixy = -((y*30)-301);
gfx_flush();
gfx_point(pixx, pixy);
}
}
void drawAxes() { //function to draw the axes
int x, y, number;
char* numberString;
gfx_color(50, 100, 150); //set color of axes
//draw x-axis
gfx_line(1, 300, 620, 300);
//draw y-axis
gfx_line(300, 1, 300, 620);
//label x-axis
x = 1;
y = 310;
while (x <= 600) {
number = -10;
while (x <= 310) {
numberString = num2str(number);
gfx_text(x, y, numberString); //will print [-10, 0]
gfx_text(x, 305, "|");
x+=30;
// printf("1. %d\n", number);
number++;
}
number = 1;
while (x > 310 && x <= 620) {
numberString = num2str(number);
gfx_text(x, y, numberString); //will print [1, 10]
gfx_text(x, 305, "|");
x+=30;
// printf("2. %d\n", number);
number++;
}
}
//label y-axis
x = 310;
y = 13;
number = 10;
while (y <= 600) {
while (y <= 310) {
numberString = num2str(number);
gfx_text(x, y, numberString); //will print [10, 0]
gfx_text(295, y, "-");
y+=29;
// printf("3. %d\n", number);
number--;
} while (y > 310 && y <= 600) {
numberString = num2str(number);
gfx_text(x, y, numberString); //will print [1, -10]
gfx_text(300, y, "-");
y+=29;
// printf("4. %d\n", number);
number--;
}
}
}
char *num2str(int n) {
static char a[10], *p = a;
snprintf(p, 10, "%d", n);
return p;
}