Не правильно рисует график
05.10.2015, 23:59. Показов 606. Ответов 1
| 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
| #include <afxwin.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <glaux.h>
#include <math.h>
#include <GL\glut.h>
#include <conio.h>
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
GLint windW, windH;
using namespace std;
int n;
float oi,x1, y, e, a, b, qm, wm, f, q, w, r, vid, ab;
const double PI = 3.141592653589793238463;
void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15, 15, -15, 15);
glMatrixMode(GL_MODELVIEW);
}
void Draw(void)
{
const double PI = 3.141592653589793238463;
glClearColor(1.0, 1.0, 1.0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3ub(190, 190, 190);
for (int i = -4; i <= 4; i++)
{
glVertex2f(float(i), -6);//явное преобразование типа
glVertex2f(float(i), 15);
}
for (int i = -6; i <= 15; i++)
{
glVertex2f(-4, float(i));
glVertex2f(4, float(i));
}
glEnd();
glColor3ub(0, 0, 0);
glBegin(GL_LINES);
glVertex2f(-5, 0);
glVertex2f(5, 0);
glVertex2f(0, 16);
glVertex2f(0, -7);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3ub(0, 0, 255);
int n;
double a, b, dx,x, y;
a = oi;
b = 1.5;
n = 100;
dx = (b - a) / (n - 1);
x = a;
x = x*PI / 180;
y = 0;
for (int i = 1; i <= n; i++)
{
y = float(tan(x));
glVertex2d(x, y);
x = x + dx;
}
glEnd();
glFinish();
glutSwapBuffers();
}
long double fact(int N)
{
if (N < 0) // если пользователь ввел отрицательное число
return 0; // возвращаем ноль
if (N == 0) // если пользователь ввел ноль,
return 1; // возвращаем факториал от нуля
else // Во всех остальных случаях
return N * fact(N - 1); // делаем рекурсию.
}
// Биноминальный коэффициент Ньютона C(n/k)
double bink(long n, long k)
{
return 1.*fact(n) / fact(k) / fact(n - k);
}
// Числа Бернулли
double bernoulli(long n)
{
if (n <= 0) return 1;
else
{
double s = 0;
for (long k = 1; k <= n; k++) s += bink(n + 1, k + 1)*bernoulli(n - k);
return -1. / (n + 1)*s;
}
}
// Ряд тангенса
double m_tan(double x1)
{
double r = 0;
double r_n = 0;
for (long n = 1;; n++)
{
r_n = pow(2, 2 * n)*(pow(2, 2 * n) - 1)*fabs(bernoulli(2 * n)) / fact(2 * n)*pow(x1, 2 * n - 1);
if (fabs(r_n) < e)
{
break;
}
else
r += r_n;
}
return r;
}
int main(int argc, char **argv)
{
cin >> x1 >> e;
oi = x1;
x1 = x1*PI / 180;
y = tan(x1);
cout << y << endl;
a = x1;
while (fabs(a) >= 3.14)
if (a>0)
a = a - 3.14;
else
a = a + 3.14;
if (fabs(a)>3.14 / 2)
{
if (a<0)
{
a += 3.14;
f = 0;
}
else
{
a = 3.14 - a;
f = 1;
}
}
else
{
if (a<0)
{
a = -a;
f = 1;
}
else
f = 0;
}
if (fabs(a)>3.14 / 4)
{
a = 3.14 / 2 - a;
r = 1;
}
b = m_tan(a);
if (f == 1)
b = -b;
if (r == 1) b = 1 / b;
cout << b << endl;
ab = b - y;
vid = ab / y * 100;
cout << ab << " " << vid << "%" << endl;
int width = 100;
int height = 100;
windW = 800;
windH = 800;
glutInitWindowSize(600, 600); //Указываем размер окна
glutInitWindowPosition(100, 100); //Позиция окна
glutCreateWindow("tan(x) Demo");
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
glClearColor(0, 0, 0, 0);
glutMainLoop();
return 0;
} |
|
Не правильно рисует график для tg, помогите фиксануть
Добавлено через 7 минут
Кликните здесь для просмотра всего текста
| 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
| #include <afxwin.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <glaux.h>
#include <math.h>
#include <GL\glut.h>
#include <conio.h>
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
GLint windW, windH;
using namespace std;
int n;
float oi,x1, y, e, a, b, qm, wm, f, q, w, r, vid, ab;
const double PI = 3.141592653589793238463;
void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15, 15, -15, 15);
glMatrixMode(GL_MODELVIEW);
}
void Draw(void)
{
const double PI = 3.141592653589793238463;
glClearColor(1.0, 1.0, 1.0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3ub(190, 190, 190);
for (int i = -4; i <= 4; i++)
{
glVertex2f(float(i), -6);//явное преобразование типа
glVertex2f(float(i), 15);
}
for (int i = -6; i <= 15; i++)
{
glVertex2f(-4, float(i));
glVertex2f(4, float(i));
}
glEnd();
glColor3ub(0, 0, 0);
glBegin(GL_LINES);
glVertex2f(-5, 0);
glVertex2f(5, 0);
glVertex2f(0, 16);
glVertex2f(0, -7);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3ub(0, 0, 255);
int n;
double a, b, dx,x, y;
a = oi;
b = 1.5;
n = 100;
dx = (b - a) / (n - 1);
x = a;
x = x*PI / 180;
y = 0;
for (int i = 1; i <= n; i++)
{
y = float(tan(x));
glVertex2d(x, y);
x = x + dx;
}
glEnd();
glFinish();
glutSwapBuffers();
}
/*long double fact(int N)
{
if (N < 0) // если пользователь ввел отрицательное число
return 0; // возвращаем ноль
if (N == 0) // если пользователь ввел ноль,
return 1; // возвращаем факториал от нуля
else // Во всех остальных случаях
return N * fact(N - 1); // делаем рекурсию.
}/*/
/*// Биноминальный коэффициент Ньютона C(n/k)
double bink(long n, long k)
{
return 1.*fact(n) / fact(k) / fact(n - k);
}/*/
/*/ Числа Бернулли
double bernoulli(long n)
{
if (n <= 0) return 1;
else
{
double s = 0;
for (long k = 1; k <= n; k++) s += bink(n + 1, k + 1)*bernoulli(n - k);
return -1. / (n + 1)*s;
}
}/*/
/* Ряд тангенса
double m_tan(double x1)
{
double r = 0;
double r_n = 0;
for (long n = 1;; n++)
{
r_n = pow(2, 2 * n)*(pow(2, 2 * n) - 1)*fabs(bernoulli(2 * n)) / fact(2 * n)*pow(x1, 2 * n - 1);
if (fabs(r_n) < e)
{
break;
}
else
r += r_n;
}
return r;
}*/
int main(int argc, char **argv)
{
cin >> x1;
oi = x1;
/*x1 = x1*PI / 180;
y = tan(x1);
cout << y << endl;
a = x1;
while (fabs(a) >= 3.14)
if (a>0)
a = a - 3.14;
else
a = a + 3.14;
if (fabs(a)>3.14 / 2)
{
if (a<0)
{
a += 3.14;
f = 0;
}
else
{
a = 3.14 - a;
f = 1;
}
}
else
{
if (a<0)
{
a = -a;
f = 1;
}
else
f = 0;
}
if (fabs(a)>3.14 / 4)
{
a = 3.14 / 2 - a;
r = 1;
}
b = m_tan(a);
if (f == 1)
b = -b;
if (r == 1) b = 1 / b;
cout << b << endl;
ab = b - y;
vid = ab / y * 100;
cout << ab << " " << vid << "%" << endl;*/
int width = 100;
int height = 100;
windW = 800;
windH = 800;
glutInitWindowSize(600, 600); //Указываем размер окна
glutInitWindowPosition(100, 100); //Позиция окна
glutCreateWindow("tan(x) Demo");
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
glClearColor(0, 0, 0, 0);
glutMainLoop();
return 0;
} |
|
закоментировал не нужное
0
|