#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
const int xStart = 10;
const float start = 3.14 / 2;
const float end = 3.14 * 8;
const int numberOfSteps = 200;
struct point {
int X;
int Y;
};
point getCoords(float x, float y, float xValue, float yValue){
int maxY = getmaxy();
int avrgY = maxY / 2;
point result;
int currX = xStart;
float realValue = 0;
if (x < 0)
for (; realValue > x; realValue -= xValue, --currX);
else
for (; realValue < x; realValue += xValue, ++currX);
result.X = currX;
realValue = 0;
int currY = avrgY;
if (y < 0)
for (; realValue > y; realValue -= yValue, ++currY);
else
for (; realValue < y; realValue += yValue, --currY);
result.Y = currY;
return result;
}
float max(float* arr, int size) {
float result = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > result)
result = arr[i];
}
return result;
}
float min(float* arr, int size) {
float result = arr[0];
for (int i = 0; i < size; ++i)
if (arr[i] < result)
result = arr[i];
return result;
}
float myFunction(float x) {
return pow(sin(x), 3) - pow(cos(x), 2);
}
void putAxis(float xValue, float yValue) {
int maxX = getmaxx();
int maxY = getmaxy();
int avrgY = maxY / 2;
line(0, avrgY, maxX, avrgY);
line(xStart, 0, xStart, maxY);
for (int i = 1; i < 9; ++i)
{
point p1 = getCoords(3.14 * i, 0, xValue, yValue);
line(p1.X, p1.Y - 5, p1.X, p1.Y + 5);
char c[] = { i + '0', 'p', 'i', '\0' };
outtextxy(p1.X - 10, p1.Y + 20, c);
}
for (int j = 1; j <= 15; j += 2)
{
point p = getCoords(3.15 * j / 2, 0, xValue, yValue);
line(p.X, p.Y - 5, p.X, p.Y + 5);
char c[] = " pi/2";
c[1] = j % 10 + '0';
if (j >= 10)
c[0] = j / 10 + '0';
outtextxy(p.X - 15, p.Y + 15, c);
}
for (int k = 1; k < 10; ++k)
{
point p1 = getCoords(0, float(k) / 10, xValue, yValue);
point p2 = getCoords(0, -float(k) / 10, xValue, yValue);
line(p1.X - 5, p1.Y, p1.X + 5, p1.Y);
line(p2.X - 5, p2.Y, p2.X + 5, p2.Y);
char c1[] = "0. ";
char c2[] = "-0. ";
c1[2] = c2[3] = k + '0';
outtextxy(p1.X + 10, p1.Y, c1);
outtextxy(p2.X + 10, p2.Y, c2);
}
}
void drawGraph(float* X, float* Y, float xValue, float yValue) {
for (int i = 0; i < numberOfSteps - 1; ++i)
{
point p1 = getCoords(X[i], Y[i], xValue, yValue);
point p2 = getCoords(X[i + 1], Y[i + 1], xValue, yValue);
line(p1.X, p1.Y, p2.X, p2.Y);
}
point p = getCoords(3.14 / 2, 0.5, xValue, yValue);
}
int main(){
clrscr();
int grMode, grDriver;
grDriver = DETECT;
grMode = 0;
initgraph(&grDriver, &grMode, "C:\\TurboC3\\BGI");
float* X = new float[numberOfSteps];
float step = (end - start) / numberOfSteps;
float* Y = new float[numberOfSteps];
X[0] = 3.14 / 2;
Y[0] = myFunction(3.14 / 2);
for (int i = 1; i < numberOfSteps; ++i) {
X[i] = X[i - 1] + step;
Y[i] = myFunction(X[i]);
}
float xValue = end / getmaxx();
float yValue = (max(Y, numberOfSteps) - min(Y, numberOfSteps)) / getmaxy();
putAxis(xValue, yValue);
drawGraph(X, Y, xValue, yValue);
getch();
delete[] X;
delete[] Y;
closegraph();
return 0;
}