Question: OpenGL: I need to use the code I am given to find the roots of the following functions : cos(x) - x 2 + 4x

OpenGL: I need to use the code I am given to find the roots of the following functions :

cos(x) - x2 + 4x = 0 x3 - 3x2 - 6x + 2 = 0

(Please run this code and see what it does first as currently it finds the intersection of two graphs. What it needs to be editted to do is find the roots of two graphs)

Here is the code: The part that needs to be edited should be in this code file and should be in the translated and inverse functions:

#include #include #include "Tools.h" GLfloat translated_quadratic(GLfloat x) { return 200 + (x - 300) * (x - 300) / 100; } GLfloat inverse1_translated_quadratic(GLfloat y) { if (y <= 200) return 300; return 300 + 10 * sqrt(y - 200); } GLfloat inverse2_translated_quadratic(GLfloat y) { if (y <= 200) return 300; return 300 - 10 * sqrt(y - 200); }

GLfloat translated_sin(GLfloat x) { return 350 + 50 * sin(((double)x - 250) / 64.0); } GLfloat inverse1_translated_sin(GLfloat y) { if (y >= 400) return 350.5; if (y <= 300) return 149.5; return 250 + 64 * asin(((double)y - 350) / 50.0); } GLfloat inverse2_translated_sin(GLfloat y) { if (y >= 400) return 350.5; if (y <= 300) return 551.5; return 250 + 64 * (3.141592 - asin(((double)y - 350) / 50.0)); } void draw_function_cartesian_system(GLfloat init_x, GLfloat final_x, GLfloat(*f)(GLfloat), GLfloat(*inverse)(GLfloat)) { GLfloat x, y, x_step = 0, y_step = 0, dx, dy; bool inc_x = final_x > init_x; bool inc_y = f(final_x) > f(init_x); for (x = init_x, y = f(init_x); ; x += x_step, y += y_step) { if (inc_x && x > final_x || !inc_x && x < final_x) break; glVertex2i(x, y); //Next choices: (x,y+1), (x,y-1), (x+1,y), (x+1,y+1), (x+1, y-1), (x-1,y), (x-1,y+1), (x-1,y-1) dx = inverse(y + (inc_y ? 1 : -1)) - x; dy = f(x + (inc_x ? 1 : -1)) - y; x_step = (dx >= .5) ? 1 : (dx > -.5) ? 0 : -1; y_step = (dy >= .5) ? 1 : (dy > -.5) ? 0 : -1; if (x_step == 0 && y_step == 0) break; } } Color getPixelColor(GLint x, GLint y) { Color color; glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color); return color; }

(Here is the main file)

#include #include #include "Tools.h" using namespace std;

void init() { glClearColor(1.0, 1.0, 1.0, 0.0); // glColor3f(0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); // glLoadIdentity(); gluOrtho2D(0, X_MAX, 0, Y_MAX); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }

void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor4f(1, 0, 0, 1); glBegin(GL_LINE_STRIP); //drawing the parabola f(x) = 200 + (x - 300) * (x - 300) / 100 in range (150,450) draw_function_cartesian_system(150, 300, translated_quadratic, inverse2_translated_quadratic); draw_function_cartesian_system(300, 450, translated_quadratic, inverse1_translated_quadratic); glEnd(); glLineWidth(2.0); glColor4f(0, 0, 1, .9); glBegin(GL_LINE_STRIP); //drawing the sin wave g(x) = 350 + 50 * sin((x - 250) / 64.0) in range (150,450) draw_function_cartesian_system(150, 350, translated_sin, inverse1_translated_sin); draw_function_cartesian_system(350, 450, translated_sin, inverse2_translated_sin); glEnd(); glFlush(); }

void onMouseClick(int button, int state, int x, int y) { y = Y_MAX - y; if (state != GLUT_DOWN || button != GLUT_LEFT_BUTTON) return; //A left click triggers a search for nearby intersection points. //Every intersection point is corresponding to a root of the equation for (int i = x - SEARCH_RANGE; i < x + SEARCH_RANGE; i++) for (int j = y - SEARCH_RANGE; j < y + SEARCH_RANGE; j++) { Color c = getPixelColor(i, j); if (c.g == 0 && c.b != 0) cout << "(" << i << "," << j << ")" << endl; } }

int main(int argc, char** argv) { /* The goal of this program is to provide a graphical tool * find all the roots of equation f(x) = g(x) where x varies in a given interval (min,max), f and g are two functions in this range. */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(X_MAX, Y_MAX); glutInitWindowPosition(X_POSITION, Y_POSITION); glutCreateWindow("Non-linear Equation Solver"); init(); glutDisplayFunc(display);//drawing plots of functions f(x) and g(x) glutMouseFunc(onMouseClick);//using mouse clicks to trigger a search for roots glutMainLoop(); return 0; }

(Tools.h file)

#ifndef TOOLS_H #define TOOLS_H #include #define X_MAX 640 #define Y_MAX 480 #define X_POSITION 200 #define Y_POSITION 200 #define SEARCH_RANGE 10 #define M_PI 3.141592 typedef struct { GLfloat x; GLfloat y; } Point; struct Color { GLfloat r; GLfloat g; GLfloat b; }; void draw_function_cartesian_system(GLfloat init_x, GLfloat final_x, GLfloat(*f)(GLfloat), GLfloat(*inverse)(GLfloat)); GLfloat translated_quadratic(GLfloat x); GLfloat inverse1_translated_quadratic(GLfloat y); GLfloat inverse2_translated_quadratic(GLfloat y); GLfloat translated_sin(GLfloat x); GLfloat inverse1_translated_sin(GLfloat y); GLfloat inverse2_translated_sin(GLfloat y); Color getPixelColor(GLint x, GLint y); #endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!