求帮忙c++作业,老师作业给了带glut的bezier曲线的code。让修改成画Bspline曲线的code。我主要改的其中bezier.cpp的code,但是遇到问题不知道怎么下手。希望有具体思路。
第一次发帖,不知道传代码,所以只发改过的那个cpp。
这是原代码(其中的bezier.cpp):
// bezier.cpp
// Bezier class member function definition
#include <iostream>
#include <GL/glut.h>
#include <vector>
using namespace std;
#include "Bezier.h"
#include "Point2D.h"
const int number_points = 20;
// constructor
Bezier::Bezier( double *controlPoints_array, int d )
{
for (int i=0; i<d+1; i++ )
{
Point2D P;
P.setPx(controlPoints_array[2*i]);
P.setPy(controlPoints_array[2*i+ 1]);
B.push_back(P);
}
degree = d;
}
Bezier::Bezier()
{
degree = -1;
}
// destructor
Bezier::~Bezier()
{
}
void Bezier::setPx( int Number, double xValue )
{
(B[Number]).setPx(xValue);
}
double Bezier::getPx( int Number )
{
return (B[Number]).getPx();
}
void Bezier::setPy( int Number, double yValue )
{
(B[Number]).setPy( yValue );
}
double Bezier::getPy( int Number )
{
return (B[Number]).getPy();
}
void Bezier::draw(int mode)
{
double t, Px, Py;
glColor3f(0.0, 0.0, 0.0);
glPointSize(8.0);
glBegin(GL_POINTS);
for (unsigned i=0; i< B.size(); i++)
glVertex2f((B[i]).getPx(), (B[i]).getPy() );
glEnd();
glColor3d (0.0, 0.0, 1.0);
glLineWidth(3);
glBegin(GL_LINE_STRIP);
for (unsigned k = 0; k < B.size(); k++ )
glVertex2f( (B[k]).getPx(), (B[k]).getPy() );
glEnd();
glColor3f(1.0f, 0.0f, 0.8f);
glLineWidth(3);
glBegin(GL_LINE_STRIP);
for ( t = 0.0; t < 1.000001; t += 1.0 / ( number_points - 1 ) )
{
deCasteljau_algorithm( Px, Py, t );
glVertex2f(Px, Py);
}
glEnd();
}
// deCastejau_algorithm function
void Bezier::deCasteljau_algorithm( double &x, double &y, double t )
{
vector<Point2D> tempB = B;
for (unsigned r=1; r< B.size(); r++)
for (unsigned i=0; i<B.size()-r; i++)
{
tempB[i].setPx( (1.0-t)*tempB[i].getPx() + t*tempB[i+1].getPx() );
tempB[i].setPy( (1.0-t)*tempB[i].getPy() + t*tempB[i+1].getPy() );
}
x = tempB[0].getPx();
y = tempB[0].getPy();
}
int Bezier::getDegree()
{
return degree;
}
void Bezier::setDegree( int d)
{
degree = d;
}
这是现在处理后的代码:
// bezier.cpp
// Bezier class member function definition
#include <iostream>
#include <GL/glut.h>
#include <vector>
using namespace std;
#include "Bezier.h"
#include "Point2D.h"
const int number_points = 20;
// constructor
Bezier::Bezier( double *controlPoints_array, int d )
{
for (int i=0; i<d+1; i++ )
{
Point2D P;
P.setPx(controlPoints_array[2*i]);
P.setPy(controlPoints_array[2*i+ 1]);
B.push_back(P);
}
degree = d;
}
Bezier::Bezier()
{
degree = -1;
}
// destructor
Bezier::~Bezier()
{
}
void Bezier::setPx( int Number, double xValue )
{
(B[Number]).setPx(xValue);
}
double Bezier::getPx( int Number )
{
return (B[Number]).getPx();
}
void Bezier::setPy( int Number, double yValue )
{
(B[Number]).setPy( yValue );
}
double Bezier::getPy( int Number )
{
return (B[Number]).getPy();
}
void Bezier::draw(int mode)
{
double t, Px, Py;
glColor3f(0.0, 0.0, 0.0);
glPointSize(8.0);
glBegin(GL_POINTS);
for (unsigned i=0; i< B.size(); i++)
glVertex2f((B[i]).getPx(), (B[i]).getPy() );
glEnd();
glColor3d (0.0, 0.0, 1.0);
glLineWidth(3);
glBegin(GL_LINE_STRIP);
for (unsigned k = 0; k < B.size(); k++ )
glVertex2f( (B[k]).getPx(), (B[k]).getPy() );
glVertex2f((B[0]).getPx(), (B[0]).getPy());
glEnd();
curve1(Px, Py);
}
// deCastejau_algorithm function
void Bezier::deCasteljau_algorithm( double &x, double &y, double t )
{
vector<Point2D> tempB = B;
for (unsigned r=1; r< B.size(); r++)
for (unsigned i=0; i<B.size()-r; i++)
{
tempB[i].setPx( (1.0-t)*tempB[i].getPx() + t*tempB[i+1].getPx() );
tempB[i].setPy( (1.0-t)*tempB[i].getPy() + t*tempB[i+1].getPy() );
}
x = tempB[0].getPx();
y = tempB[0].getPy();
}
int Bezier::getDegree()
{
return degree;
}
void Bezier::setDegree( int d)
{
degree = d;
}
void Bezier::curve1(double &x, double &y){
//B:ABCD;
vector<Point2D> mid = B;
//mid:ABCD;
B.push_back(B[0]);
//B:ABCDA;
for (int i = 0; i < mid.size(); i++)
{
mid[i].setPx((B[i].getPx() + B[i + 1].getPx()) / 2);
mid[i].setPy((B[i].getPy() + B[i + 1].getPy()) / 2);
}
//mid:abcd;
mid.push_back(mid[0]);
//mid:abcda;
vector<Point2D> secA;
for (int i = 0; i < mid.size() - 1; i++)
{
secA[i].setPx((mid[i].getPx() + mid[i + 1].getPx()) / 2);
secA[i].setPy((mid[i].getPy() + mid[i + 1].getPy()) / 2);
}
//setA:B'C'D'A';
vector<Point2D> secB;
secB = secA;
for (int i = 0; i < secA.size() - 1; i++){
secB.push_back(secA[i]);
}
//setB:B'C'D'A'B'C'D';
vector<Point2D> secC;
for (int i = 0; i < (secB.size() + 1) / 2; i++){
secC.push_back(secB[i + (secB.size() + 1) / 2]);
}
//setC:A'B'C'D';
vector<Point2D> secD;
for (int i = 0; i < secC.size(); i++){
secD[i].setPx((B[i].getPx() + secC[i].getPx()) / 2);
secD[i].setPy((B[i].getPy() + secC[i].getPy()) / 2);
}
vector<Point2D> draw_curve;
for (int i = 0; i < 2 * secD.size(); i++)
{
draw_curve.push_back(secD[i]);
draw_curve.push_back(mid[i]);
}
glColor3f(1.0f, 0.0f, 0.8f);
glLineWidth(3);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < draw_curve.size(); i++)
{
glVertex2f(draw_curve[i].getPx(), draw_curve[i].getPy());
}
glEnd();
}
遇到这样的问题:
First-chance exception at 0x0040161E in HW4.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
有人说也许是指针的问题。但是我不太懂。求指导!
本人刚来国外读书,人生地不熟的,谷歌的解释也看不大懂。希望遇到大神。交个朋友!
5 个解决方案
#1
编译没错,运行出错?
#2
对 run的时候error那没任何报告 就有个[First-chance exception at 0x0040161E in HW4.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
If there is a handler for this exception, the program may be safely continued.
#3
对 run的时候error那没任何报告 就有个[First-chance exception at 0x0040161E in HW4.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
#4
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
#5
#1
编译没错,运行出错?
#2
对 run的时候error那没任何报告 就有个[First-chance exception at 0x0040161E in HW4.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
If there is a handler for this exception, the program may be safely continued.
#3
对 run的时候error那没任何报告 就有个[First-chance exception at 0x0040161E in HW4.exe: 0xC0000005: Access violation writing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.
#4
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。