Greetings readers. some time ago I made a program that draws 2 functions and paints their intersection, but it all works very slowly, it would be nice to modify the program to make it work faster, but how I don’t know honestly
program code:
//Using SDL, SDL_image, standard IO, and strings
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 640;
//Starts up SDL and creates window
bool init();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Create window
gWindow = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 0);
}
}
}
return success;
}
void close()
{
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
}
float x,y,x1,y1;
float offset = 100;
float scale = 8;
int xPointArr[1801];
int yPointArr[1801];
//Clear screen
SDL_RenderClear(gRenderer);
SDL_SetRenderDrawColor(gRenderer, 0, 255, 0, 255);
SDL_RenderDrawLine(gRenderer, 0, offset - 2 * scale, 900, offset - 2 * scale);
for (int i = 0, j = 1800; i < 1800; i++, j--)
{
x = i - 900;
y = -(sqrt(25 - x * x / 50));
xPointArr[i] = x + offset;
yPointArr[i] = scale * y + offset;
SDL_SetRenderDrawColor(gRenderer, 255, 0, 0, 255);
if (yPointArr[i] < offset - 2 * scale)
SDL_RenderDrawLine(gRenderer, x + offset, scale * y + offset, 100, scale * (-(sqrt(25 - (j - 900) * (j - 900) / 50))) + offset);
SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
SDL_RenderDrawLine(gRenderer, x + offset, scale * y + offset, (x + 1) + offset, scale * (-(sqrt(25 - (x + 1) * (x + 1) / 50))) + offset);
}
SDL_RenderSetLogicalSize(gRenderer, 180, 180);
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
//Update screen
SDL_RenderPresent(gRenderer);
}
}
//Free resources and close SDL
close();
return 0;
}