Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

Here is the code I was given please assist. The part that needs to be edited is this

(This is the file that needs changes in the translated and inverse sections)

#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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

=+8. Why is productivity important?

Answered: 1 week ago

Question

=+ 9. What is inflation and what causes it?

Answered: 1 week ago