#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <math.h>
#include <vector>

#include <OpenGL/gl.h>
#include <GLUT/glut.h>

#include "ColorWheel.h"

//static GLfloat spin = 0.0;
//static bool running = false;
static int screenW = 0, screenH = 0;
static int lod = 500;

struct point2d {
	float x;
	float y;
};

std::vector<point2d> parabola1;
std::vector<point2d> parabola2;
std::vector<point2d> parabola3;


// y1 and y2 have to be zero, are roots
// x is first root, q is second root
// a is steepness of curve
std::vector<point2d> parabolaVertex(float x, float q, float a)
{
	/*
	 * the vertex form of a parabola is
	 *		y = a(x - h)² + k
	 */
	 
	std::vector<point2d> xpoints;
	// a((x1 + h)*(x1 + h)) + k = a((x2 + h)*(x2 + h)) + k
	//float h = (a *
	
	float h = q/2;
	float k = -(a * ( (x + h) * (x + h) ) );	
	//std::cout << k << "\n";


	  	
	
	// TRUE - works
	//float y = (a * (q * q)) - (2 *a * q * h) + (a * (h * h)) + k;
	
	//std::cout << "Y is: " << y << "\n";
	//std::cout << "K is: " << k << "\n";
	//std::cout << "H is: " << h << "\n";
	
	point2d temp;
	for (int i=0; i <= (int)lod; i++) {
		float pointX = i / (lod / q);
		//std::cout << pointX << "\n";
		float pointY = (a * pointX * pointX) - (2 * a * pointX * h) + (a * h * h) + k;

		
		//std::cout << pointX << "," << pointY << "\n";
		
		temp.x=pointX;
		temp.y=pointY;
		
		xpoints.push_back(temp);
//		ypoints.push_back(pointY);
	}
	
	return xpoints;
	
}

std::vector<point2d> parabolaStandard(float a, float b, float c, float w)
{
	 
	/* 
	 * the standard form is
	 * ax² + bx + c 
	 */


	std::vector<point2d> xpoints;

	point2d temp;
	for (int i=0; i <= (int)lod; i++) {
		float pointX = i / (lod / w);
		float pointY = (a * pointX * pointX) + (b * pointX) + c;
		
		//std::cout << pointX << "," << pointY << "\n";
		
		temp.x=pointX;
		temp.y=pointY;
		
		xpoints.push_back(temp);
	}
	
	return xpoints;
	
}



void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);

	/*
	parabola1 = parabolaVertex(0.0f,100.0f,-0.05f);
	parabola2 = parabolaVertex(1.0f,100.0f,-0.05f);
	parabola3 = parabolaVertex(2.0f,100.0f,-0.10f);
	*/

	parabola1 = parabolaStandard(0.03f, 1.0f, 1.0f, 540.0f);
	parabola2 = parabolaStandard(0.03f, 1.0f, 20.0f, 1110.0f);
	parabola3 = parabolaStandard(-0.03f, 0.5f, 25.0f, 1000.0f);
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
}

void display(void)
{

	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	glTranslatef( screenW / 2, screenH / 2, 0 );

	glColor4f(0.7f, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (int i=0; i <= lod; i++) {
		glVertex2i(parabola1[i].x, parabola1[i].y);
	}
	glEnd();

	glColor4f(0.0f, 0.7f, 0.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (int i=0; i <= lod; i++) {
		glVertex2i(parabola2[i].x, parabola2[i].y);
	}
	glEnd();
	
	glColor4f(0.0f, 0.0f, 0.7f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (int i=0; i <= lod; i++) {
		glVertex2i(parabola3[i].x, parabola3[i].y);
	}
	glEnd();



	glColor4f(1.0f, 1.0f, 1.0f,0.25f);
	// x axis
	glLineWidth(1.0f);
	glBegin(GL_LINE_STRIP);
			glVertex2f(-100.0f, 0.0f);
			glVertex2f(100.0f, 0.0f);
	glEnd();

	glColor4f(1.0f, 1.0f, 1.0f,0.25f);	
	// y axis
	glBegin(GL_LINE_STRIP);
			glVertex2f(0.0f, -100.0f);
			glVertex2f(0.0f, 100.0f);
	glEnd();
	
	// dots
	glPointSize(1.0);
	glColor4f(1.0f, 1.0f, 1.0f,0.90f);	
	glBegin(GL_POINTS);
		glVertex3f(-99.0f, -1.0f, 0.0f);
		glVertex3f(99.0f, -1.0f, 0.0f);
		glVertex3f(0.0f, -99.0f, 0.0f);
		glVertex3f(0.0f, 99.0f, 0.0f);
	glEnd();



	
//	glRotatef(spin, 0.0, 0.0, 1.0);
//	glColor3f(0.2, 0.2, 1.0);
//	glRectf(-25.0, -25.0, 25.0, 25.0);

//	gluLookAt(5,5,0,0,0,0,0,0,0);
//	glScalef(5.0,5.0,5.0);
	glPopMatrix();
	glutSwapBuffers();
}

void updateScene(int whatever)
{

//	boxScene->doScene();
	
	glutTimerFunc( 500, updateScene, 0);
	glutPostRedisplay();
}

/*
void spinDisplay( int value )
{
	if ( running )
	{
		spin = spin+1.0;
		if (spin > 360.0)
			spin = spin - 360.0;		
	}
	
	glutTimerFunc( 10, spinDisplay, 0 );
	glutPostRedisplay();
}
*/

void reshape(int w, int h)
{
	screenW = w;
	screenH = h;
	
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
	switch (button) {
		case GLUT_LEFT_BUTTON:
			if (state == GLUT_DOWN)
			{
				//running = !running;
			}
			break;

		default:
			break;
	}
}

// react to keys 
void keyboard(unsigned char k, int , int ) 
{ 
    switch(k) 
    { 
        case 27:    
        { 
            exit(0); 
        } 
        break; 
        
        case 'v': 
        {  
			parabola1 = parabolaVertex(0.0f,100.0f,-0.005f);
			parabola2 = parabolaVertex(0.0f,100.0f,-0.010f);
			parabola3 = parabolaVertex(0.0f,100.0f,-0.020f);
        } 
        break; 
        
        case 's':   
        { 
			parabola1 = parabolaStandard(0.03f, 1.0f, 1.0f, 540.0f);
			parabola2 = parabolaStandard(0.03f, 1.0f, 20.0f, 1110.0f);
			parabola3 = parabolaStandard(-0.03f, 0.5f, 25.0f, 1000.0f);

        } 
        break;
		 
    } 
} 



/* useless comment */

int main(int argc, char** argv)
{
	ColorWheel colorWheel = ColorWheel();
	colorWheel.pingPong();
	
	/*
	std::cout << "RGB (" << std::setprecision(1) << colorWheel.getRed() << ","
	 << std::setprecision(1) << colorWheel.getGreen() << "," 
	 << std::setprecision(1) << colorWheel.getBlue() << ")\n";	
	*/




	//exit(0);
	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(250, 250);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutKeyboardFunc(keyboard);

	glutTimerFunc(500, updateScene, 0);
	
	
	glutMainLoop();
	return 0;
}
 
parabola.txt · Last modified: 2010/02/17 10:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki