@Ultraporing Thanks for the response. I was looking at those lines as well but I don't know what to replace them with. Do you think you could show me the code? Here's the full code:
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <vector>
using namespace std;
//Point class for taking the points
class Point {
public:
float x, y;
void setxy(float x2, float y2)
{
x = x2; y = y2;
}
//operator overloading for '=' sign
const Point& operator=(const Point& rPoint)
{
x = rPoint.x;
y = rPoint.y;
return *this;
}
};
int SCREEN_HEIGHT = 500;
int points = 0;
Point Cursor;
vector<Point> pointsVector(100);
vector<Point> controlPoints(100);
int controlPointCount;
vector<Point> reverseControlPoints(100);
bool firstSegmentDrawn = new bool(false);
vector<Point> bezierPointsMyDisplay;
void myInit() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(3);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 500.0);
}
void drawDot(Point p1) {
glBegin(GL_POINTS);
glVertex2i(p1.x, p1.y);
glEnd();
}
void drawLine(Point p1, Point p2) {
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
}
vector<Point> final4Points()
{
vector<Point> Points;
Points.push_back(pointsVector[points - 1]); // 0
Points.push_back(reverseControlPoints[points - 1]); // 1
Points.push_back(reverseControlPoints[points]); // 2
Points.push_back(pointsVector[points]); // 3
return Points;
}
vector<Point> final4PointsReverseP1()
{
vector<Point> Points;
Points.push_back(pointsVector[points - 1]); // 0
Points.push_back(controlPoints[points - 1]); // 1
Points.push_back(reverseControlPoints[points]); // 2
Points.push_back(pointsVector[points]); // 3
return Points;
}
int getPt(int n1, int n2, float perc)
{
int diff = n2 - n1;
return n1 + (diff * perc);
}
void myMouse(int button, int state, int x, int y)
{
// If left button was clicked
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
Point point;
point.setxy(x, SCREEN_HEIGHT - y);
// Draw the red dot.
glColor3f(255, 0, 0);
drawDot(point);
if ((pointsVector[0].x == NULL) && (pointsVector[0].y == NULL))
{
pointsVector[0] = point;
}
points++;
if (points > 2)
{
firstSegmentDrawn = true;
}
pointsVector[points].setxy((float)x, (float)(SCREEN_HEIGHT - y));
}
else if (state == GLUT_UP)
{
Point Handle;
Handle.setxy(x, SCREEN_HEIGHT - y);
controlPoints[0] = pointsVector[0];
controlPoints[points].setxy(Handle.x, Handle.y);
glColor3f(0, 255, 0);
drawDot(Handle);
Point ReverseHandle;
reverseControlPoints[0] = pointsVector[0];
ReverseHandle.x = (2 * pointsVector[points].x) - Handle.x;
ReverseHandle.y = (2 * pointsVector[points].y) - Handle.y;
reverseControlPoints[points].setxy(ReverseHandle.x, ReverseHandle.y);
glColor3f(0, 255, 0);
drawDot(ReverseHandle);
glColor3f(0, 0, 255);
}
}
}
void passiveMotion(int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
Cursor.setxy(x, SCREEN_HEIGHT - y);
if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
{
drawLine(pointsVector[points], Cursor);
}
for (int i = 1; i < points; i++)
{
// draw main line & dot
glColor3f(255, 0, 0);
drawDot(pointsVector[i - 1]);
// draw tangent lines and tangent dots
glColor3f(0, 255, 0);
drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
glColor3f(0, 0, 255);
drawDot(controlPoints[i + 1]);
drawDot(reverseControlPoints[i + 1]);
vector<Point> finalPoints = final4Points();
vector<Point> finalPointsReverseP1 = final4PointsReverseP1();
Point p1;
Point p2;
if (firstSegmentDrawn == false)
{
p1.setxy(pointsVector[0].x, pointsVector[0].y);
}
else
{
//The first segment has been drawn.
p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
}
if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
{
if (firstSegmentDrawn == true)
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
p1 = p2;
}
}
else
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
//Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
p1 = p2;
}
}
}
}
for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
{
drawDot(bezierPointsMyDisplay[i]);
//drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
}
glutSwapBuffers();
}
void motion(int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
Point Cursor;
Cursor.setxy(x, SCREEN_HEIGHT - y);
Point reverseCursor;
reverseCursor.x = (2 * pointsVector[points].x) - Cursor.x;
reverseCursor.y = (2 * pointsVector[points].y) - Cursor.y;
glColor3f(0, 255, 0);
drawLine(pointsVector[points], Cursor);
drawLine(pointsVector[points], reverseCursor);
glColor3f(0, 0, 255);
drawDot(Cursor);
drawDot(reverseCursor);
vector<Point> finalPoints;
if (firstSegmentDrawn == true)
{
finalPoints = final4PointsReverseP1();
}
else
{
finalPoints = final4Points();
}
Point p1;
if (firstSegmentDrawn == false)
{
p1.setxy(pointsVector[0].x, pointsVector[0].y);
}
else
{
p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
}
if ((finalPoints[1].x != NULL) && (finalPoints[1].y != NULL))
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, reverseCursor.x, i);
int yb = getPt(finalPoints[1].y, reverseCursor.y, i);
int xc = getPt(reverseCursor.x, finalPoints[3].x, i);
int yc = getPt(reverseCursor.y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
//drawDot(p1);
p1 = p2;
}
}
for (int i = 1; i < points; i++)
{
// draw main line & dot
glColor3f(255, 0, 0);
drawDot(pointsVector[i - 1]);
glColor3f(0, 0, 0);
//drawLine(pointsVector[i], pointsVector[i + 1]);
// draw tangent lines and tangent dots
glColor3f(0, 255, 0);
drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
glColor3f(0, 0, 255);
drawDot(controlPoints[i + 1]);
drawDot(reverseControlPoints[i + 1]);
vector<Point> finalPoints = final4Points();
vector<Point> finalPointsReverseP1 = final4PointsReverseP1();
Point p1;
p1.setxy(NULL, NULL);
if (firstSegmentDrawn == false)
{
p1.setxy(pointsVector[0].x, pointsVector[0].y);
}
else
{
//The first segment has been drawn.
p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
}
if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
{
if (firstSegmentDrawn == true)
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
p1 = p2;
}
}
else
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
//drawDot(p1);
p1 = p2;
}
}
}
}
for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
{
drawDot(bezierPointsMyDisplay[i]);
//drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
}
glutSwapBuffers();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
{
drawLine(pointsVector[points], Cursor);
}
for (int i = 1; i < points; i++)
{
// draw main line & dot
glColor3f(255, 0, 0);
drawDot(pointsVector[i - 1]);
// draw tangent lines and tangent dots
glColor3f(0, 255, 0);
drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
glColor3f(0, 0, 255);
drawDot(controlPoints[i + 1]);
drawDot(reverseControlPoints[i + 1]);
vector<Point> finalPoints = final4Points();
vector<Point> finalPointsReverseP1 = final4PointsReverseP1();
Point p1;
if (firstSegmentDrawn == false)
{
p1.setxy(pointsVector[0].x, pointsVector[0].y);
bezierPointsMyDisplay.push_back(p1);
}
else
{
p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
bezierPointsMyDisplay.push_back(p1);
}
if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
{
if (firstSegmentDrawn == true)
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
//drawDot(p1);
p1 = p2;
bezierPointsMyDisplay.push_back(p2);
}
}
else
{
float i;
// draw bezier curve
for (float j = 0; j <= 10; j++)
{
i = j / 10;
// The Green Lines
int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);
// The Blue Line
int xm = getPt(xa, xb, i);
int ym = getPt(ya, yb, i);
int xn = getPt(xb, xc, i);
int yn = getPt(yb, yc, i);
// The Black Dot
int x2 = getPt(xm, xn, i);
int y2 = getPt(ym, yn, i);
Point p2;
p2.setxy(x2, y2);
drawLine(p1, p2);
//drawDot(p1);
p1 = p2;
bezierPointsMyDisplay.push_back(p2);
}
}
}
}
for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
{
//drawDot(bezierPointsMyDisplay[i]);
//printf("%f, %f -> %f, %f\n", bezierPointsMyDisplay[i - 1].x, bezierPointsMyDisplay[i - 1].y,
//bezierPointsMyDisplay[i].x, bezierPointsMyDisplay[i].y);
drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
}
glutSwapBuffers();
}
void timer(int)
{
glutTimerFunc(1000 / 60, timer, 0);
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 500);
glutInitWindowPosition(100, 150);
glutCreateWindow("Bezier Curve");
glutDisplayFunc(myDisplay);
glutIdleFunc(myDisplay);
glutTimerFunc(0, timer, 0);
glutMouseFunc(myMouse);
glutPassiveMotionFunc(passiveMotion);
glutMotionFunc(motion);
myInit();
glutMainLoop();
return 0;
}