Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics.h>
- #include <conio.h>
- #include <stdio.h>
- #include <math.h>
- const double PI = 3.141592653589;
- double x_min = PI / 2; // Setting up our unique values for x min and x max
- double x_max = PI * 7;
- int SMOOTHNESS = 1000;
- double scale = 25;
- // returns y(x)
- double function(double x) {
- return (sin(x) * sin(x) - cos(x) * cos(x));
- }
- int main() {
- int graph_driver = DETECT;
- int graph_mode;
- int graph_error_code;
- initgraph( & graph_driver, & graph_mode, "C:\\TURBOC3\\BGI");
- // Exception processing
- graph_error_code = graphresult();
- if (graph_error_code != grOk) {
- char * msgError = grapherrormsg(graph_error_code);
- printf("%s\n", msgError);
- return graph_error_code;
- }
- // Setting up the (0,0) on the screen
- double centerCordX = getmaxx() / 20;
- double centerCordY = getmaxy() / 2;
- setviewport(centerCordX, centerCordY, getmaxx(), getmaxy(), 0);
- // Start position and step init
- double step = (x_max - x_min) / SMOOTHNESS;
- double currentX = x_min, currentY = function(currentX);
- double previousX = currentX, previousY = currentY;
- settextstyle(DEFAULT_FONT, VERT_DIR, 1);
- line(0, -getmaxy(), 0, getmaxy());
- line(-getmaxx(), 0, getmaxx(), 0);
- double maxY = currentY;
- while (currentX < x_max) {
- currentX += step;
- currentY = function(currentX);
- maxY = currentY > maxY ? currentY : maxY;
- line(previousX * scale, -previousY * scale, currentX * scale, -currentY * scale);
- if (fmod(currentX, 1) < step) {
- char title[8];
- sprintf(title, "%3.1f", currentX);
- outtextxy(previousX * scale, 0, title);
- }
- previousX = currentX;
- previousY = currentY;
- }
- printf("Max: %f\n", maxY);
- getch();
- closegraph();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement